Codington
0 of 6 problems solved • 0%

Exercise prompt

Find Matching Lines

Example

Enumerate attaches a running position even to an empty element.

for number, item in enumerate(["pen", "", "pad"], start=1): if item.startswith("p"): print(number, item) # 1 pen # 3 pad

An open file is iterable too. Count physical lines while checking each one for the search term.

Your task

Implement matching_lines(path, term). Return the one-based line numbers of all lines containing the nonempty term as a case-sensitive substring. Return a line number only once even if the term appears twice on it.

# If notes.txt contains "wet\n\nnew wet trail\n": matching_lines("notes.txt", "wet") # [1, 3]