From 0f718464706a61ebfac6b1fe9e0247e6cfa9bbb6 Mon Sep 17 00:00:00 2001 From: moniblanes Date: Fri, 19 Dec 2025 10:58:08 +0100 Subject: [PATCH] solved --- lab-python-data-structures.ipynb | 106 +++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 4 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..f42decbd 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -34,8 +34,7 @@ "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", - " Store these statistics in a tuple called `order_status`.\n", + " Store these statistics in a tuple called `order_status`.\n", "\n", "8. Print the order statistics using the following format:\n", " ```\n", @@ -50,11 +49,110 @@ "\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": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #list of products" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 2, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 6}\n" + ] + } + ], + "source": [ + "inventory = {} #empty dictionary to store inventory\n", + "\n", + "inventory[\"t-shirt\"] = int(input(\"Enter quantity of t-shirt: \"))\n", + "inventory[\"mug\"] = int(input(\"Enter quantity of mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Enter quantity of hat: \"))\n", + "inventory[\"book\"] = int(input(\"Enter quantity of book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Enter quantity of keychain: \"))\n", + " \n", + "print(\"Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n", + "Customer orders: {'mug'}\n" + ] + } + ], + "source": [ + "customer_orders = set() #empty set to store customer orders\n", + "\n", + "customer_orders.add(input(\"Enter first product to order: \"))\n", + "customer_orders.add(input(\"Enter second product to order: \"))\n", + "customer_orders.add(input(\"Enter third product to order: \"))\n", + "\n", + "print(\"Inventory:\", inventory)\n", + "print(\"Customer orders:\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order status (total products ordered, percentage ordered): (3, 60.0)\n" + ] + } + ], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "print(\"Order status (total products ordered, percentage ordered):\", order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory after fulfilling one order of each product: {'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "for product in inventory:\n", + " inventory[product] -= 1\n", + "print(\"Updated Inventory after fulfilling one order of each product:\", inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,