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..2b720616 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,214 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "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": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt (in kg): 10\n", + "Please enter the quantity of mug (in kg): 5\n", + "Please enter the quantity of hat (in kg): 12\n", + "Please enter the quantity of book (in kg): 14\n", + "Please enter the quantity of keychain (in kg): 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 5, 'hat': 12, 'book': 14, 'keychain': 1}\n" + ] + } + ], + "source": [ + "# Define a list called products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "# Create an empty dictionary\n", + "inventory = {}\n", + "# Quantity of each product\n", + "for product in products:\n", + " quantity = int(input(\"Please enter the quantity of \" + product + \" (in kg): \"))\n", + " inventory[product] = quantity\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter 1 product among: ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n", + "Please enter 1 product among: ['t-shirt', 'mug', 'hat', 'book', 'keychain'] book\n", + "Please enter 1 product among: ['t-shirt', 'mug', 'hat', 'book', 'keychain'] hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# Create an empty set\n", + "customer_orders = set()\n", + "# Set customer_orders top 3 products\n", + "while len(customer_orders) != 3:\n", + " customer_product = input(\"Please enter 1 product among: \" + str(products))\n", + " if customer_product in products:\n", + " customer_orders.add(customer_product)\n", + " else:\n", + " customer_product = input(\"Please enter 1 product among: \" + str(products))\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: \n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered:60.0% \n" + ] + } + ], + "source": [ + "Total_Products_Ordered = len(customer_orders)\n", + "Percentage_of_Products_Ordered = len(customer_orders) / len(products) *100\n", + "print(f\"Order Statistics: \")\n", + "print(f\"Total Products Ordered: {Total_Products_Ordered}\")\n", + "print(f\"Percentage of Products Ordered:{Percentage_of_Products_Ordered}% \")" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 9, 'mug': 4, 'hat': 11, 'book': 13, 'keychain': 0}\n" + ] + } + ], + "source": [ + "# Update the inventory quantity\n", + "for key in inventory:\n", + " inventory[key]-= 1 # helped by the AI companion here because i don't understand why the code directly deduce 1 to the quantity without precising to work on the value itself\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "For t-shirt product, the updated quantity is:9 kg.\n", + "For mug product, the updated quantity is:4 kg.\n", + "For hat product, the updated quantity is:11 kg.\n", + "For book product, the updated quantity is:13 kg.\n", + "For keychain product, the updated quantity is:0 kg.\n" + ] + } + ], + "source": [ + "for key, value in inventory.items():\n", + " print(f\"For {key} product, the updated quantity is:{value} kg.\")" + ] + } + ], + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..2b720616 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -10,8 +10,10 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "## Exercise: Managing Customer Orders\n", "\n", @@ -50,6 +52,142 @@ "\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": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt (in kg): 10\n", + "Please enter the quantity of mug (in kg): 5\n", + "Please enter the quantity of hat (in kg): 12\n", + "Please enter the quantity of book (in kg): 14\n", + "Please enter the quantity of keychain (in kg): 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 5, 'hat': 12, 'book': 14, 'keychain': 1}\n" + ] + } + ], + "source": [ + "# Define a list called products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "# Create an empty dictionary\n", + "inventory = {}\n", + "# Quantity of each product\n", + "for product in products:\n", + " quantity = int(input(\"Please enter the quantity of \" + product + \" (in kg): \"))\n", + " inventory[product] = quantity\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter 1 product among: ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n", + "Please enter 1 product among: ['t-shirt', 'mug', 'hat', 'book', 'keychain'] book\n", + "Please enter 1 product among: ['t-shirt', 'mug', 'hat', 'book', 'keychain'] hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# Create an empty set\n", + "customer_orders = set()\n", + "# Set customer_orders top 3 products\n", + "while len(customer_orders) != 3:\n", + " customer_product = input(\"Please enter 1 product among: \" + str(products))\n", + " if customer_product in products:\n", + " customer_orders.add(customer_product)\n", + " else:\n", + " customer_product = input(\"Please enter 1 product among: \" + str(products))\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: \n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered:60.0% \n" + ] + } + ], + "source": [ + "Total_Products_Ordered = len(customer_orders)\n", + "Percentage_of_Products_Ordered = len(customer_orders) / len(products) *100\n", + "print(f\"Order Statistics: \")\n", + "print(f\"Total Products Ordered: {Total_Products_Ordered}\")\n", + "print(f\"Percentage of Products Ordered:{Percentage_of_Products_Ordered}% \")" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 9, 'mug': 4, 'hat': 11, 'book': 13, 'keychain': 0}\n" + ] + } + ], + "source": [ + "# Update the inventory quantity\n", + "for key in inventory:\n", + " inventory[key]-= 1 # helped by the AI companion here because i don't understand why the code directly deduce 1 to the quantity without precising to work on the value itself\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "For t-shirt product, the updated quantity is:9 kg.\n", + "For mug product, the updated quantity is:4 kg.\n", + "For hat product, the updated quantity is:11 kg.\n", + "For book product, the updated quantity is:13 kg.\n", + "For keychain product, the updated quantity is:0 kg.\n" + ] + } + ], + "source": [ + "for key, value in inventory.items():\n", + " print(f\"For {key} product, the updated quantity is:{value} kg.\")" + ] } ], "metadata": { @@ -68,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,