From 37aab8b406cfc00c07be81f0f623ee2266457b64 Mon Sep 17 00:00:00 2001 From: SofiaPS-bio Date: Fri, 29 Aug 2025 10:04:58 +0200 Subject: [PATCH] Resolved exercise --- lab-python-data-structures.ipynb | 115 +++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..d78149a1 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -31,7 +31,7 @@ "\n", "6. Print the products in the `customer_orders` set.\n", "\n", - "7. Calculate the following order statistics:\n", + "7. Calculate the following order statistics: \n", " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", " \n", @@ -44,17 +44,124 @@ " Percentage of Products Ordered: % \n", " ```\n", "\n", - "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly. #here\n", "\n", "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'2', '1', '6'}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #using this???\n", + "inventory = {}\n", + "\n", + "inv_shirt = int(input(\"Enter the inventory count for t-shirts: \"))\n", + "inv_mug = int(input(\"Enter the inventory count for mugs: \"))\n", + "inv_hat = int(input(\"Enter the inventory count for hats: \"))\n", + "inv_book = int(input(\"Enter the inventory count for books: \"))\n", + "inv_keychain = int(input(\"Enter the inventory count for keychains: \"))\n", + "\n", + "inventory[\"t-shirt\"] = inv_shirt\n", + "inventory[\"mug\"] = inv_mug\n", + "inventory[\"hat\"] = inv_hat\n", + "inventory[\"book\"] = inv_book\n", + "inventory[\"keychain\"] = inv_keychain\n", + "\n", + "customer_orders = set() #to set up an empty set, could also be {}, but it gave me an error\n", + "customer_orders.add(input(\"Enter one order (from `t-shirt`, `mug`, `hat`, `book`, `keychain`): \")) #attention with the double quotes inside the string\n", + "customer_orders.add(input(\"Enter another order (from `t-shirt`, `mug`, `hat`, `book`, `keychain`): \"))\n", + "customer_orders.add(input(\"Enter third order (from `t-shirt`, `mug`, `hat`, `book`, `keychain`): \"))\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Order Statistics:\n", + " Total Products Ordered: 3\n", + " Percentage of Products Ordered: 10.0% \n", + " \n" + ] + } + ], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "total_in_house = sum(inventory.values())\n", + "percentage_ordered = (total_products_ordered / total_in_house) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(f\"\"\"\n", + " Order Statistics:\n", + " Total Products Ordered: {total_products_ordered}\n", + " Percentage of Products Ordered: {percentage_ordered}% \n", + " \"\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + " t-shirt: 4\n", + " mug: 5\n", + " hat: 6\n", + " book: 7\n", + " keychain: 8\n", + " \n" + ] + } + ], + "source": [ + "print(f\"\"\"Updated Inventory:\n", + " t-shirt: {inventory['t-shirt']}\n", + " mug: {inventory['mug']}\n", + " hat: {inventory['hat']}\n", + " book: {inventory['book']}\n", + " keychain: {inventory['keychain']}\n", + " \"\"\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +175,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,