Kodewick
Start learning →
Mini challenge←→0 / 80

Weekend shopping

#077Mini challengeDictsSorting7 min

You're cooking a few dishes this weekend, and each recipe is {ingredient: how many}. Write shopping_list(recipes): add up each ingredient across the recipes, sort by ingredient name, write each one like 'egg x5', and return the list.

Examples
shopping_list([{"egg": 2, "flour": 1}, {"egg": 3, "milk": 1}])→['egg x5', 'flour x1', 'milk x1']
shopping_list([])→[]
main.py
def shopping_list(recipes):
    need = {}
    # your code here: for each recipe and each ingredient, add the amount into need
    # your code here: sort by name and, in one line, return the list of 'name xN'
    return []
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.