…
…
Keep the tables of Corner Café in a dictionary, find the best free table, raise an error for a taken table, and seat waitlisted groups when a booking is canceled.
Corner Café has started taking bookings on weekends. The café has six tables of different sizes. In this project you will write a small system that manages the bookings: table status, errors for bookings that break the rules, choosing the best table for an arriving group, a waitlist when no table is free, and seating the next group when a booking is canceled.
Lessons used: functions (14-16), lists and enumerate (23-24), dictionaries (26-28), tuples (29), comprehensions (30), lambda as a sort key (31), try, except, raise (32).
reset() empties all the tables. Every cell starts with it, so running a cell again does not change the result.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Each table has a capacity and a booking. If there is no booking, the value is None; otherwise it is a {"name": ..., "party": ...} dictionary. reset() clears the existing dictionary in place and refills it. It does not create a new dictionary, so it does not need global.
print_status() should print one line per table: empty if the table is free, or something like Amara (4 people) if it is booked. You check whether a value is None with is None.
This step's code cell is for members.Sign up free
reserve(number, name, party) books one specific table. There are three rules. If one is broken, the table stays unchanged and a ValueError is raised:
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
| Situation | Message |
|---|---|
| The table does not exist | Table 9 not found |
| The table is taken | Table 3 is taken |
| The party is larger than the capacity | Table 1 seats 2 at most |
This step's code cell is for members.Sign up free
When a group arrives, you should pick the smallest table among the free tables that are big enough. Seating a couple at an eight-seat table leaves large groups with nowhere to sit.
First collect the numbers of the candidate tables with a comprehension. If there are no candidates, return None. Otherwise give min its sort key with a lambda: key=lambda number: tables[number]["capacity"]. When capacities are equal, min returns the first one it meets, which is the table with the lower number.
This step's code cell is for members.Sign up free
book(name, party) finds the best table for the group, books it, and returns a message. If there is no free table that fits, the group is added to the waitlist as a tuple: ("Zara", 8).
A group larger than the biggest table in the café could never be seated, so it is not put on the waitlist. An error is raised instead. You can find the largest capacity with max([t["capacity"] for t in tables.values()]).
This step's code cell is for members.Sign up free
cancel(number) frees the table, then walks through the waitlist from the start: it seats the first group that now fits at a table and removes it from the list. If a group was seated, it returns a message that says so.
Canceling a table that is empty or does not exist raises an error. If you remove an item while walking through a list, use enumerate to know its position and leave the loop right after removing it.
This step's code cell is for members.Sign up free
The events of Saturday evening are in a list, in order: bookings and cancellations. Some of them break the rules. Try each event; if there is an error, print its message and carry on. At the end, print the status of the tables and the waitlist.
This step's code cell is for members.Sign up free