Mini-Grep: Matching Lines with Numbers

Medium ⏱ 12 min 48% acceptance ★★★★★ 4.5
Write find_lines(path, needle) returning a list of (line_number, line) tuples (1-based numbering, line stripped of its trailing newline) for every line containing needle case-insensitively.

Examples

Example 1
Input
find_lines('app.log', 'error')  # line 2 is 'DB Error: timeout'
Output
[(2, 'DB Error: timeout')]
Explanation

Case-insensitive match on line 2.

Constraints

  • 1-based line numbers.
  • Case-insensitive containment.

Topics

File Handlingsearch

Companies

GoogleCiscoAtlassian

Hints

Hint 1

enumerate(f, start=1) numbers the lines.

Hint 2

Compare lowercased haystack and needle.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.