Paul's Python CoursePPC

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.

About an hour and a half, in 13 steps.

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:

  1. find_part(parts, name) returns (position, looks): where the name is in the list, or -1 if it isn't there, and how many names were looked at. Write the binary search yourself, with a while loop, looking in the middle, (low + high) // 2, each time. On a list of a million names it looks at about twenty.
  2. starting_with(parts, prefix) returns every name that starts with prefix, in order. Use bisect to find where they start, so it stays quick on a huge list.
  3. containing(parts, word) returns (names, looks): every name with word anywhere in it, in order, and how many names it looked at, which is all of them.
  4. 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.
  5. search(parts, query) returns the answer to one query as a line of text:
    • +sprocket-90t adds a part: sprocket-90t: added as part 148, or sprocket-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, or zinc*: 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, ..., or turbo: 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.

What it prints
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

  1. Looking at each in turn
  2. One at a time
  3. Halving a sorted list
  4. Which ones does it look at?
  5. The bolt drawers
  6. Python's own binary search
  7. Where would it go?
  8. Where the bolts begin
  9. How Paul would find it
  10. Fix Caitlin's version
  11. Make the search engine
  12. Ship it