From 00a617e0e5a31007b2534f4107d1a802d6eb8da7 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Fri, 5 Sep 2025 06:26:06 +0000 Subject: [PATCH] [Sync Iteration] python/mecha-munch-management/2 --- .../mecha-munch-management/2/dict_methods.py | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 solutions/python/mecha-munch-management/2/dict_methods.py diff --git a/solutions/python/mecha-munch-management/2/dict_methods.py b/solutions/python/mecha-munch-management/2/dict_methods.py new file mode 100644 index 0000000..ecaee3e --- /dev/null +++ b/solutions/python/mecha-munch-management/2/dict_methods.py @@ -0,0 +1,86 @@ +"""Functions to manage a users shopping cart items.""" + + +def add_item(current_cart, items_to_add): + """Add items to shopping cart. + + :param current_cart: dict - the current shopping cart. + :param items_to_add: iterable - items to add to the cart. + :return: dict - the updated user cart dictionary. + """ + + for item in items_to_add: + current_cart[item] = current_cart.get(item, 0) + 1 + return current_cart + + +def read_notes(notes): + """Create user cart from an iterable notes entry. + + :param notes: iterable of items to add to cart. + :return: dict - a user shopping cart dictionary. + """ + + shopping_list = {} + for item in notes: + if item in shopping_list: + continue + shopping_list[item] = 1 + return shopping_list + + +def update_recipes(ideas, recipe_updates): + """Update the recipe ideas dictionary. + + :param ideas: dict - The "recipe ideas" dict. + :param recipe_updates: iterable - with updates for the ideas section. + :return: dict - updated "recipe ideas" dict. + """ + + ideas.update(recipe_updates) + return ideas + + +def sort_entries(cart): + """Sort a users shopping cart in alphabetical order. + + :param cart: dict - a users shopping cart dictionary. + :return: dict - users shopping cart sorted in alphabetical order. + """ + + sorted_cart = dict(sorted(cart.items())) + return sorted_cart + + +def send_to_store(cart, aisle_mapping): + """Combine users order to aisle and refrigeration information. + + :param cart: dict - users shopping cart dictionary. + :param aisle_mapping: dict - aisle and refrigeration information dictionary. + :return: dict - fulfillment dictionary ready to send to store. + """ + + full_info = {} + sorted_cart = dict(sorted(cart.items(), reverse=True)) + sorted_mapping = dict(sorted(aisle_mapping.items(), reverse=True)) + for item, amount in sorted_cart.items(): + full_info[item] = [amount, aisle_mapping.get(item)[0], aisle_mapping.get(item)[1]] + return full_info + + +def update_store_inventory(fulfillment_cart, store_inventory): + """Update store inventory levels with user order. + + :param fulfillment cart: dict - fulfillment cart to send to store. + :param store_inventory: dict - store available inventory + :return: dict - store_inventory updated. + """ + + for item, info in fulfillment_cart.items(): + if store_inventory[item][0] < info[0]: + store_inventory[item][0] = "Out of Stock" + else: + store_inventory[item][0] -= info[0] + if store_inventory[item][0] == 0: + store_inventory[item][0] = "Out of Stock" + return store_inventory