From dace11a09f7ad2793cb58ec9579ac3a36eacc918 Mon Sep 17 00:00:00 2001 From: Francisca Date: Wed, 18 Feb 2026 12:57:29 +0000 Subject: [PATCH] Lab resolution --- ...ab-python-data-structures-checkpoint.ipynb | 76 +++++++ lab-python-data-structures.ipynb | 202 +++++++++++++++++- 2 files changed, 277 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..5b3ce9e0 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,76 @@ +{ + "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. " + ] + } + ], + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..11590f05 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,206 @@ "\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": [ + "Please enter the quantity of t-shirt: 1\n", + "Please enter the quantity of mug: 5\n", + "Please enter the quantity of hat: 7\n", + "Please enter the quantity of book: 8\n", + "Please enter the quantity of keychain: 6\n" + ] + } + ], + "source": [ + "for product in products:\n", + " quantity_of_product = int(input(f\"Please enter the quantity of {product}:\"))\n", + " inventory[product] = quantity_of_product" + ] + }, + { + "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 a product to order: hat\n", + "Enter a product to order: book\n", + "Enter a product to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer order: {'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "while len(customer_orders) < 3:\n", + " order = input(\"Enter a product to order: \").strip()\n", + " if order in products:\n", + " if order in customer_orders:\n", + " print(f\"{order} is already in your order. Choose a different product.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"{order} is not in the product list. Choose a valid product.\")\n", + "\n", + "print(f\"Customer order:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "print(total_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.0\n" + ] + } + ], + "source": [ + "percentage_of_products_ordered = len(customer_orders)/len(products)*100\n", + "print(percentage_of_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "order_status = (total_products_ordered, percentage_of_products_ordered)\n", + "print(order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 4\n", + "hat: 6\n", + "book: 7\n", + "keychain: 6\n" + ] + } + ], + "source": [ + "for product in customer_orders:\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1 \n", + " else:\n", + " print(f\"{product} is out of stock!\")\n", + "\n", + "print(f\"Updated Inventory:\")\n", + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +268,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,