Predict the output
What does this print?
text, words = "penapple", {"pen", "apple", "pena"}
ok = [True] + [False] * len(text)
for i in range(1, len(text) + 1):
ok[i] = any(ok[i - len(w)] and text[i - len(w):i] == w
for w in words if len(w) <= i)
print(ok[3], ok[4], ok[-1])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A True True True
Right: 'pen' and 'pena' are words, and 'pen' + 'apple' covers the whole string.
Several cut points can be reachable at once, and the table keeps them all instead of committing to one.
Why not the others?
- B True False True. 'pena' is in the word list.
- C False False True. 'pen' is the first three letters.