-
-
Save stephengruppetta/4e79c0020d827d683483e6c885598530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import NamedTuple | |
shopping_list = [ | |
"Biscuits", | |
"Bread", | |
"Butter" # Note the missing comma | |
"Berries", | |
"Bananas", | |
"Bagels", | |
] | |
class SupermarketShelves(NamedTuple): | |
aisle: int | |
section: str | |
supermarket = { | |
"Bread": SupermarketShelves(3, "B"), | |
"Biscuits": SupermarketShelves(4, "D"), | |
"Butter": SupermarketShelves(1, "B"), | |
"Berries": SupermarketShelves(5, "C"), | |
"Bananas": SupermarketShelves(5, "C"), | |
} | |
def order_shopping_list(shopping_list, supermarket): | |
available_items = ( | |
item for item in shopping_list if item in supermarket.keys() | |
) | |
sorted_items = sorted( | |
available_items, | |
key=lambda item: supermarket[item].aisle, | |
) | |
unavailable_items = ( | |
item for item in shopping_list if item not in supermarket.keys() | |
) | |
return sorted_items, unavailable_items | |
items_to_buy, unavailable_items = order_shopping_list( | |
shopping_list, | |
supermarket, | |
) | |
print("Time to start your shopping:") | |
print(*items_to_buy, sep="\n") | |
print("\nI'm afraid these items are not stocked:") | |
print(*unavailable_items, sep="\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment