Class summary: Design Practice
Copyright (c) 2017 Kathi Fisler
Now that we’re done with hashtables, let’s step back and look at some practice problems.
1 Flights by day
Last class, we made a hashtable that mapped flights to lists of passengers on each flight. But airlines run the same flight on multiple days. What if we want the passenger lists per day? Propose a collection of dataclasses, hashtables, and lists for this problem.
Here’s the Flight dataclass again.
@dataclass(frozen=True) |
class Flight: |
num: int |
frm: str |
to: str |
Solution: It seems here that we want two hashtables, one keyed on dates and one keyed on flights. Here are three proposals – which do you prefer? What are the strengths and weaknesses of each?
One hashtable with flights as keys and passenger lists as values, and another with dates as keys and passenger lists as values.
A hashtable with dates as keys and hashtables as values. The inner hashtables have flights as keys as passenger lists as values.
A hashtable with flights as keys and hashtables as values. The inner hashtables have dates as keys and passenger lists as values.
The first is a poor idea because there flights and dates aren’t connected, which is part of the point of the problem.
The choice between the second and third depends on how you think about using the data. Are you more likely to want to look at all flights across the same date, or all dates for the same flight? Depending on which will be more common in your work tells you which of these organizations to use. Put differently, which inner hashtable is most useful to you?
As we did this example in class, many questions arose about how such an example could be set up in memory, especially if you wanted to maintain both a dates-based and a flights-based hashtable. I’ve provided both a Python file showing how you might set this up (so that you have one passenger list for each flight/date combo), and provided a powerpoint slidedeck illustrating how memory evolves as the code runs.
2 Finishing line of a run
You’re managing a local 5K race, and want to track the order in which runners cross the line. Prizes are given based on both overall finishing order, but also in age-based categories. Ignore the finishing time for now. How might you organize this data?
Given the last few lectures, you might have jumped to a hashtable, but that’s more than you need here. A simple sorted list should suffice, with the contents capturing runner’s names and ages in a dataclass:
@dataclass |
class Runner: |
name: str |
age: int |
|
finishers = [Runner("Ali", 35), |
Runner("Wayne", 23), |
Runner("Johan", 32), |
Runner("Avika", 31), |
Runner("Todd", 21), |
Runner("Rodney", 72), |
Runner("Janelle", 32), |
... |
] |
We’ll work more with this example in the next class.