
Bug hunt
Anchors, Boundaries & Flags
Some pattern pieces match a position, not a character:
^start of the string,$end of the string\ba word boundary, the edge between a word character and a non-word character.r"\bcat\b"matchescatbut notcategory.
Flags change how a pattern behaves:
re.IGNORECASE(re.I): letters match either casere.MULTILINE(re.M):^and$match at every linere.VERBOSE(re.X): whitespace and# commentsinside the pattern are ignored, so long patterns can be laid out nicely
Combine flags with |. If you use a pattern a lot, compile it once:
import re
WORD = re.compile(r"\boil\b", re.IGNORECASE)
WORD.findall("Oil? OIL! Boil...") # ['Oil', 'OIL']
The bug
The harbor master's tools are broken:
count_word("Oil, oil, OIL! Boil the oil.", "oil")should be4(any case, but not insideBoil). It returns0.warnings(log)should return the message of every line that starts withWARN:. It returns[].
Assignment
Find and fix the two bugs. (re.escape(word) is fine: it makes any
special characters in word literal.)
Find and fix the two bugs. (re.escape(word) is fine: it makes any
special characters in word literal.)
main.py Python sleeping
Press Run to try your code, then Check to test it.