From 3efd873c173849804b8d51e414afecc0345bd462 Mon Sep 17 00:00:00 2001 From: Sandra Fdez Pascual Date: Tue, 16 Jun 2026 17:23:26 +0200 Subject: [PATCH] Add lab solution --- ...ab-python-data-structures-checkpoint.ipynb | 305 ++++++++++++++++++ lab-python-data-structures.ipynb | 231 ++++++++++++- 2 files changed, 535 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb new file mode 100644 index 00000000..a6f48878 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,305 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders\n", + "\n", + "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "2. Create an empty dictionary called `inventory`.\n", + "\n", + "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", + "\n", + "4. Create an empty set called `customer_orders`.\n", + "\n", + "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\". Add each product name to the `customer_orders` set.\n", + "\n", + "6. Print the products in the `customer_orders` set.\n", + "\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", + " Store these statistics in a tuple called `order_status`.\n", + "\n", + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " 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", + "\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": 1, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 50\n", + "Quantity of mug: 25\n", + "Quantity of hat: 10\n", + "Quantity of book: 60\n", + "Quantity of keychain: 20\n" + ] + } + ], + "source": [ + "inventory[\"t-shirt\"] = int(input(\"Quantity of t-shirt: \"))\n", + "inventory[\"mug\"] = int(input(\"Quantity of mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Quantity of hat: \"))\n", + "inventory[\"book\"] = int(input(\"Quantity of book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Quantity of keychain: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product: glass\n", + "Invalid product. Enter again: t-shirt\n", + "Enter product: hat\n", + "Enter product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "product1 = input(\"Enter product: \") \n", + "while product1 not in products: \n", + " product1 = input(\"Invalid product. Enter again:\") \n", + "customer_orders.add(product1)\n", + "\n", + "product2 = input(\"Enter product: \") \n", + "while product2 not in products: \n", + " product2 = input(\"Invalid product. Enter again:\") \n", + "customer_orders.add(product2)\n", + "\n", + "product3 = input(\"Enter product: \") \n", + "while product3 not in products: \n", + " product3 = input(\"Invalid product. Enter again:\") \n", + "customer_orders.add(product3)\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "60.0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(customer_orders)/len(products) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (len(customer_orders),len(customer_orders)/len(products)*100)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "print(order_status) " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "for product in inventory.keys():\n", + "\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 49, 'mug': 24, 'hat': 9, 'book': 59, 'keychain': 19}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt 49\n", + "mug 24\n", + "hat 9\n", + "book 59\n", + "keychain 19\n" + ] + } + ], + "source": [ + "for product, quantity in inventory.items():\n", + "\n", + " print(product, quantity)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.14.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..a6f48878 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,235 @@ "\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": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 50\n", + "Quantity of mug: 25\n", + "Quantity of hat: 10\n", + "Quantity of book: 60\n", + "Quantity of keychain: 20\n" + ] + } + ], + "source": [ + "inventory[\"t-shirt\"] = int(input(\"Quantity of t-shirt: \"))\n", + "inventory[\"mug\"] = int(input(\"Quantity of mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Quantity of hat: \"))\n", + "inventory[\"book\"] = int(input(\"Quantity of book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Quantity of keychain: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product: glass\n", + "Invalid product. Enter again: t-shirt\n", + "Enter product: hat\n", + "Enter product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "product1 = input(\"Enter product: \") \n", + "while product1 not in products: \n", + " product1 = input(\"Invalid product. Enter again:\") \n", + "customer_orders.add(product1)\n", + "\n", + "product2 = input(\"Enter product: \") \n", + "while product2 not in products: \n", + " product2 = input(\"Invalid product. Enter again:\") \n", + "customer_orders.add(product2)\n", + "\n", + "product3 = input(\"Enter product: \") \n", + "while product3 not in products: \n", + " product3 = input(\"Invalid product. Enter again:\") \n", + "customer_orders.add(product3)\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "60.0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(customer_orders)/len(products) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (len(customer_orders),len(customer_orders)/len(products)*100)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "print(order_status) " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "for product in inventory.keys():\n", + "\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 49, 'mug': 24, 'hat': 9, 'book': 59, 'keychain': 19}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt 49\n", + "mug 24\n", + "hat 9\n", + "book 59\n", + "keychain 19\n" + ] + } + ], + "source": [ + "for product, quantity in inventory.items():\n", + "\n", + " print(product, quantity)" + ] } ], "metadata": { @@ -68,7 +297,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.5" } }, "nbformat": 4,