Predict the output
What does this print?
import re
words = ["bad", "dad", "mad"]
for q in ["pad", ".ad", "b..", "b."]:
print(q, any(re.fullmatch(q, w) for w in words))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A pad False .ad True b.. True b. False
Right: '.' is one letter, and the match must cover the whole word.
The trie version gets the same answers but only visits branches that still match, instead of testing every word.
Why not the others?
- B pad False .ad True b.. True b. True. fullmatch needs the whole word, and 'b.' has only two letters.
- C pad True .ad True b.. True b. False. No stored word is 'pad'.