# origin: Programming+ exercise match-proved # status: unverified; `corpus.py verify` checks it on the pinned compiler # probe: match with its specification as comments # expect-stdout: [1] def match_pattern(p: String, t: String) -> List[Int]: # requires: |p| >= 1 # ensures: result == sorted({ s in [0, |t| - |p| + 1) : forall k in [0, |p|) : t[s + k] == p[k] }) var n = t.count_codepoints() var m = p.count_codepoints() var answer = List[Int]() for i in range(n - m + 1): # invariant: answer == sorted({ s in [0, i) : forall k in [0, m) : t[s + k] == p[k] }) var matched = True for j in range(m): # invariant: matched == (forall k in [0, j) : t[i + k] == p[k]) if t[codepoint=i + j] != p[codepoint=j]: matched = False break if matched: answer.append(i) return answer^ def main(): print(match_pattern("ATT", "GATTACA"))