Practice Problems¶
- Finish the function
add_quantity(item_dict, quantities)
that “zips” quantities onto their corresponding items inside each category, returning a nested dictionary: It takes a dictionary
item_dict
and a listquantities
as input, the lengths ofitem_dict
andquantities
are the same.For dict
item_dict
, keys are category names (strings), values are lists of unique item names (strings), each list contains at least one item (no empty lists).For list
quantities
, it contains the quantity for each item initem_dict
. Quantities are given in the same order as the items appear in item_dict when iterated in insertion order, category by category, left to right.The total number of quantities equals the total number of items in all categories combined.
The function should return a new nested dictionary where the outer dictionary keys are the category names, the inner dictionary keys are the item names, and the inner dictionary values are the quantities.
Example Input |
Expected Output |
---|---|
|
|
|
|
|
|
- Finish the function
get_average_score()
below: It takes one dictionary
student_information
representing student data, where keys are student names, and values are dictionaries containing information about the student, including their age and a list of course they took and the grades.You need to calculate the average grades for each student and then store the students whose average grade is higher than or equal to 80 in a dictionary.
It returns a dictionary
average_score
where keys are student names, and values are the average grades for each student.
Example Input |
Expected Output |
---|---|
|
|
|
|
|
|
- Finish the function
get_vegetarian_menu(menu_items):
below: It takes a list of tuples
menu_items
as input, each tuple contains(name, category, price, is_vegetarian)
.- It returns a new nested dictionary that only contains the items from
menu_items
whereis_vegetarian
isTrue
. The outer dictionary keys are
category
such as “Soup”, “Pizza”, “Pasta”, “Salad”.The inner dictionary keys are
name
and values areprice
for each vegetarian item of thatcategory
.
- It returns a new nested dictionary that only contains the items from
Example Input |
Expected Output |
---|---|
|
|
|
|
|
|
Write a function, get_order_totals()
, that takes a list of tuples and returns a nested dictionary with the same information. Each tuple includes 3 values; the first is the person’s name, the second is item name, and the third is the quantity.
Note that there may be more than one tuple for the same person and item - your dictionary should total all the quantities for the same person and item.
Example Input |
Expected Output |
---|---|
|
|
|
|
What to do next¶
Click on the following link to take the posttest: Posttest