Practice question · Sort into groups
Choosing a fitting data representation is half the battle. Sort each quantity by the type that best models it.
Groups: A single number · A list · A dictionary
- A phone book from names to numbers
- The items in a shopping cart, in order
- A person's age
- The total price of the cart
Hints
- A single value is one number; ordered many-values suit a list; keyed lookups suit a dictionary.
- A phone book and a stock lookup are both keyed by a meaningful name or id.
Show the answer
A single number: A person's age, The total price of the cart
A list: The items in a shopping cart, in order
A dictionary: A phone book from names to numbers
Why
Ages and totals are single numbers; a cart's items and a class's scores are ordered lists; a phone book and id-keyed stock are dictionaries. Matching each quantity to a fitting representation is the modelling step that makes the later algorithm easy rather than painful.
Practise Simple Modelling with Code
The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on Simple Modelling with Code
- Modelling a phone book as a dictionary rather than a list of pairs changes almost nothing about what the…
- Select every practice that the lesson recommends when modelling a problem.
- Order the steps of modelling a problem before writing real code.
- Storing money as a floating-point number is a well-known mistake. What actually goes wrong?