Better HN
topnewbestaskshowjobs
Log inSign up

thorwwaskeas

2 karma · joined Feb 3, 2022 (4y ago) · 2 submissions

Submissions

comment 4y ago · link · parent

Since they used the tests this is not something you can do if you don't have a rich battery of tests.

Perhaps many problems are something like finite automata and the program discover the structure of the finite automata and also an algorithm for better performance.

comment 4y ago · link · parent

  from collections import defaultdict
  def backspace(s1,s2):
      h = defaultdict(lambda:0)
      for x in s1:
          h[x] = h[x] + 1
      for x in s2:
        h[x] = h[x] - 1
      j = 0
      maxj = len(s2) - 1
      for x in s1:
        if x != s2[j]:
            h[x] -= 1
        elif j < maxj:
                j += 1
        else:
            break
    return j == maxj and all(y >= 0 for y in h.values())

  def random_backspace(s1):
    res = []
    for x in s1:
        if randint(0,1) == 0:
            res.append(x)
    return "".join(res)

  def backspaceTest(s1):
    return all(backspace(s1,random_backspace(s1)) for _ in  range(100))