From 35bfa189a564a7e0b76aa957bf0ba0f604c41c8e Mon Sep 17 00:00:00 2001 From: Rui Braz Date: Fri, 29 Aug 2025 00:13:42 +0100 Subject: [PATCH] Solved lab --- ...ab-python-data-structures-checkpoint.ipynb | 297 ++++++++++++++++++ lab-python-data-structures.ipynb | 227 ++++++++++++- 2 files changed, 521 insertions(+), 3 deletions(-) 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..a12593ae --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,297 @@ +{ + "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": 71, + "metadata": {}, + "outputs": [], + "source": [ + "# creating a list that contains the items\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "# creating an empty iventory\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter the quantity of each product available in the inventory:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity for t-shirt: 67\n", + "Quantity for mug: 56\n", + "Quantity for hat: 44\n", + "Quantity for book: 89\n", + "Quantity for keychain: 99\n" + ] + } + ], + "source": [ + "#3 ask the user to input the quantity of each product using a for loop to go through a list \n", + "\n", + "print(\"Please enter the quantity of each product available in the inventory:\")\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Quantity for {product}: \")) # using input() inside a loop and casting the input to an int()\n", + " inventory[product] = quantity # assinging a value to a dictionary key" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [], + "source": [ + "#4 creating an empty set called customer_orders\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please select three products to order from the following list:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter product name 1/3: t-shirt\n", + "Enter product name 2/3: mug\n", + "Enter product name 3/3: keychain\n" + ] + } + ], + "source": [ + "#5 ask the user to input the name of three products that a customer wants to order\n", + "\n", + "print(\"Please select three products to order from the following list:\")\n", + "print(products) # display the available options\n", + "\n", + "for i in range(3): # loop three times to get the three orders\n", + " order = input(f\"Enter product name {i+1}/3: \")\n", + " customer_orders.add(order)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer ordered these products: {'keychain', 'mug', 't-shirt'}\n" + ] + } + ], + "source": [ + "#6 print the products in the customer_orders set\n", + "\n", + "print(f\"The customer ordered these products: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "#7 calculate orders statistics\n", + "# total products ordered: the total number of products in the customer_orders set\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "# percentage of products ordered: the percentage of products ordered compared to the total available products\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# store these statistics in a tuple called order_status.\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "#8 print the order statistics\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory Updating...\n", + " - Sold one 'keychain'. Updated quantity: 98\n", + " - Sold one 'mug'. Updated quantity: 55\n", + " - Sold one 't-shirt'. Updated quantity: 66\n" + ] + } + ], + "source": [ + "#9 update the inventory by subtracting 1 from the quantity of each product\n", + "\n", + "print(\"Inventory Updating...\")\n", + "for product in customer_orders:\n", + " if product in inventory: # check if product is in inventory\n", + " inventory[product] -= 1\n", + " print (f\" - Sold one '{product}'. Updated quantity: {inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 66\n", + "mug: 55\n", + "hat: 44\n", + "book: 89\n", + "keychain: 98\n" + ] + } + ], + "source": [ + "#10 print the updated inventory \n", + "\n", + "print(\"Updated Inventory:\")\n", + "# .items() displays the key and value for each item in the dictionary\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..a12593ae 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,234 @@ "\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": 71, + "metadata": {}, + "outputs": [], + "source": [ + "# creating a list that contains the items\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "# creating an empty iventory\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter the quantity of each product available in the inventory:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity for t-shirt: 67\n", + "Quantity for mug: 56\n", + "Quantity for hat: 44\n", + "Quantity for book: 89\n", + "Quantity for keychain: 99\n" + ] + } + ], + "source": [ + "#3 ask the user to input the quantity of each product using a for loop to go through a list \n", + "\n", + "print(\"Please enter the quantity of each product available in the inventory:\")\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"Quantity for {product}: \")) # using input() inside a loop and casting the input to an int()\n", + " inventory[product] = quantity # assinging a value to a dictionary key" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [], + "source": [ + "#4 creating an empty set called customer_orders\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please select three products to order from the following list:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter product name 1/3: t-shirt\n", + "Enter product name 2/3: mug\n", + "Enter product name 3/3: keychain\n" + ] + } + ], + "source": [ + "#5 ask the user to input the name of three products that a customer wants to order\n", + "\n", + "print(\"Please select three products to order from the following list:\")\n", + "print(products) # display the available options\n", + "\n", + "for i in range(3): # loop three times to get the three orders\n", + " order = input(f\"Enter product name {i+1}/3: \")\n", + " customer_orders.add(order)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer ordered these products: {'keychain', 'mug', 't-shirt'}\n" + ] + } + ], + "source": [ + "#6 print the products in the customer_orders set\n", + "\n", + "print(f\"The customer ordered these products: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "#7 calculate orders statistics\n", + "# total products ordered: the total number of products in the customer_orders set\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "# percentage of products ordered: the percentage of products ordered compared to the total available products\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# store these statistics in a tuple called order_status.\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "#8 print the order statistics\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory Updating...\n", + " - Sold one 'keychain'. Updated quantity: 98\n", + " - Sold one 'mug'. Updated quantity: 55\n", + " - Sold one 't-shirt'. Updated quantity: 66\n" + ] + } + ], + "source": [ + "#9 update the inventory by subtracting 1 from the quantity of each product\n", + "\n", + "print(\"Inventory Updating...\")\n", + "for product in customer_orders:\n", + " if product in inventory: # check if product is in inventory\n", + " inventory[product] -= 1\n", + " print (f\" - Sold one '{product}'. Updated quantity: {inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 66\n", + "mug: 55\n", + "hat: 44\n", + "book: 89\n", + "keychain: 98\n" + ] + } + ], + "source": [ + "#10 print the updated inventory \n", + "\n", + "print(\"Updated Inventory:\")\n", + "# .items() displays the key and value for each item in the dictionary\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -68,7 +289,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,