From 4f9e17f0f90086f33cd99158fe4f75ca4c416da2 Mon Sep 17 00:00:00 2001 From: martin-paz-y Date: Sat, 30 Aug 2025 09:19:45 +0200 Subject: [PATCH] Solved lab --- lab-python-data-structures.ipynb | 69 +++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..bdb743be 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,76 @@ "\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": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 20, 'mug': 40, 'hat': 3, 'book': 2, 'keychain': 10}\n", + "{'hat', 'book', 'keychain'}\n", + "3\n", + "0.04\n", + "(3, 0.04)\n", + "{'t-shirt': 19, 'mug': 39, 'hat': 2, 'book': 1, 'keychain': 9}\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\", \"book\", \"keychain\"] #. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "inventory= {} # Create an empty dictionary called `inventory`.\n", + "tshiqty = int(input(\"Enter t-shirt quantity : \"))\n", + "mugqty = int(input(\"Enter mug quantity : \"))\n", + "hatqty = int(input(\"Enter hat quantity : \"))\n", + "booqty = int(input(\"Enter book quantity : \"))\n", + "chaqty = int(input(\"Enter keychain quantity : \")) #. Ask the user to input the quantity of each product available in the inventory. \n", + "inventory= {products[0]:tshiqty,products[1]:mugqty,products[2]:hatqty,products[3]:booqty,products[4]:chaqty} #Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", + "\n", + "customer_orders=set() #4. Create an empty set called `customer_orders`.\n", + "\n", + "pro_1 = input(\"Enter one of these: t-shirt, mug, hat, book, keychain: \")\n", + "pro_2 = input(\"Enter one of these: t-shirt, mug, hat, book, keychain: \")\n", + "pro_3 = input(\"Enter one of these: t-shirt, mug, hat, book, keychain: \") #5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\".\n", + "customer_orders.add(pro_1)\n", + "customer_orders.add(pro_2)\n", + "customer_orders.add(pro_3) #Add each product name to the `customer_orders` set.\n", + "\n", + "print(inventory)\n", + "print(customer_orders) #Print the products in the `customer_orders` set.\n", + "\n", + "totalorders= len(customer_orders)\n", + "#totalavailable=len(inventory) (not working cos is always 5)\n", + "totalavailable=sum(inventory.values())\n", + "percentageordered=totalorders/totalavailable\n", + "order_status = (totalorders, percentageordered)#Calculate the order statistics\n", + "\n", + "\n", + "print(totalorders)\n", + "print(percentageordered)\n", + "print(order_status) # Print the order statistics \n", + "\n", + "\n", + "for product in inventory: \n", + " inventory[product] -= 1 #9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "print(inventory) #10. Print the updated inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +133,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,