Lab: Tiny Search Engine
Due: Wednesday, November 11 at 11:59pm (one week after it is assigned) · Worth: 8 points
A search result is more useful when it tells us where a match came from. You’ll build a program that searches several documents and reports the filename and line number for each match. Along the way you’ll use enumerate, zip, and comprehensions.
Start
Download and extract the starter ZIP. Open the inner lab-search folder in VS Code, then initialize and publish your repository as lab-search. Open search.py. This lab needs no third-party libraries. The supplied run_search.py and documents/ let you try the complete program.
Build the search functions
Write the five functions in order. First number the lines in one document and find the matches. Then combine matches from several documents, keeping each filename with its results. The reading shows how to use enumerate, zip, unpacking, and comprehensions for these operations.
The starter’s docstrings contain examples. Do not change input lists. For empty inputs, the first four functions return [] and match_counts returns {}. Filenames are unique, and names and groups have equal lengths. Correct loop implementations also earn credit.
-
number_lines(lines): return(line_number, text)tuples starting at 1, including blank lines. -
matching_lines(lines, query): return numbered matching lines, ignoring case but preserving original text and positions. Match literal substrings. An empty query returns[]; spaces in other queries are ordinary characters. -
group_matches(documents, query): return one match list per document in input order, including an empty list for a document with no matches. -
flatten_matches(names, groups): return(filename, line_number, text)tuples. Preserve file order, line order, and repeated lines. -
match_counts(names, groups): return a dictionary counting matching lines for every file, including files with zero matches. Count a matching line once even if the query occurs twice.
Check and submit
Run:
python -m doctest search.py
python run_search.py library
python run_search.py "bring a friend"Check that repeated matching lines keep their different line numbers and that files with no matches still have a zero count.
Commit and sync search.py, run_search.py, and documents/. On Gradescope, choose GitHub and submit lab-search and the branch containing your work. The submission guide explains resubmission.