29 lines
1006 B
Mojo
29 lines
1006 B
Mojo
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
|
|
|
# MATCH, with the specification from "Specifying the problem",
|
|
# and the two invariants that prove it.
|
|
def match_(p: String, t: String) -> List[Int]:
|
|
# REQUIRE LENGTH(P) >= 1
|
|
# ENSURE RESULT == SORTED({ S IN [0, LENGTH(T) - LENGTH(P) + 1) : FORALL K IN [0, LENGTH(P)) : T[S + K] == P[K] })
|
|
var n: Int
|
|
var m: Int
|
|
var matched: Bool
|
|
var answer = List[Int]()
|
|
n = t.count_codepoints()
|
|
m = p.count_codepoints()
|
|
# INVARIANT Answer == SORTED({ S IN [0, I) : FORALL K IN [0, M) : T[S + K] == P[K] })
|
|
for i in range(n - m + 1):
|
|
matched = True
|
|
# INVARIANT Matched == (FORALL K IN [0, J) : T[I + K] == P[K])
|
|
for j in range(m):
|
|
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
|
|
matched = False
|
|
break
|
|
if matched:
|
|
answer.append(i)
|
|
return answer.copy()
|
|
|
|
|
|
def main():
|
|
print(match_("ATT", "GATTACA"))
|