Project 2 of 15, for Neil
Search Engine for a Parts List
A search engine for Neil's list of kart and rocket parts: whole names by binary search, beginnings with bisect, words anywhere one name at a time, and the looks each one took.
Sign up free to open itLog in
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilThis is my parts list for the kart, the rockets and the drones, 188 parts and every one in alphabetical order.
- NeilWhen Caitlin borrows a part she reads the list from the top and usually gives up around the brake pads.
- NeilA list in order should only take seven looks instead of 188, and I'd like a search box that shows it.
- PaulThat's because it's in order, and order's worth a lot as long as you actually use it.
parts.txt is the parts list: one part a line, like bolt-M8 or tyre-slick. load_parts reads it into a list in alphabetical order. In parts_search.py, write:
find_part(parts, name)returns(position, looks): where the name is in the list, or-1if it isn't there, and how many names were looked at. Write the binary search yourself, with awhileloop, looking in the middle,(low + high) // 2, each time. On a list of a million names it looks at about twenty.starting_with(parts, prefix)returns every name that starts withprefix, in order. Usebisectto find where they start, so it stays quick on a huge list.containing(parts, word)returns(names, looks): every name withwordanywhere in it, in order, and how many names it looked at, which is all of them.add_part(parts, name)puts the name into the list in its place and returns its position. A name that's already there isn't added twice: its position is returned and the list is left alone.search(parts, query)returns the answer to one query as a line of text:+sprocket-90tadds a part:sprocket-90t: added as part 148, orsprocket-76t: already part 141.- A query ending in
*is a beginning:bolt-M1*: 5 parts: bolt-M10, ...with the names joined by,,nass*: 1 part: nassau-panel, orzinc*: no parts. - Anything else is a whole name:
tyre-slick: part 159, found in 7 looks. If there's no such part, it searches for the query as a word:carbon: no part by that name. 8 parts with it in the name (188 looks): bracket-carbon, ..., orturbo: no part by that name, and none with it in the name (188 looks).
how_many(count) gives 1 part or 8 parts, and the loop at the bottom, which asks Search? until the answer is blank, is written.
Parts list: 188 parts, air-filter to wire-6mm A name, the start of one with *, or +name to add one. Blank to stop. Search? tyre-slick tyre-slick: part 159, found in 7 looks Search? bolt-M1* bolt-M1*: 5 parts: bolt-M10, bolt-M10-titanium, bolt-M12, bolt-M12-titanium, bolt-M16 Search? turbo turbo: no part by that name, and none with it in the name (188 looks) Search? +sprocket-90t sprocket-90t: added as part 148 Search? sprocket-* sprocket-*: 8 parts: sprocket-76t, sprocket-78t, sprocket-80t, sprocket-82t, sprocket-84t, sprocket-86t, sprocket-88t, sprocket-90t Search?
What you learn
What's in it
- Looking at each in turn
- One at a time
- Halving a sorted list
- Which ones does it look at?
- The bolt drawers
- Python's own binary search
- Where would it go?
- Where the bolts begin
- How Paul would find it
- Fix Caitlin's version
- Make the search engine
- Ship it