From 8416e2b45607699191bd64b9c01a0bbbaba98859 Mon Sep 17 00:00:00 2001 From: Isailton Silva Date: Wed, 24 Dec 2025 00:26:45 +0100 Subject: [PATCH] Solved lab --- ...Customer_Orders_Optimized-checkpoint.ipynb | 250 ++++++++++++++++++ .../lab-python-flow-control-checkpoint.ipynb | 69 +++++ Lab_Customer_Orders_Optimized.ipynb | 250 ++++++++++++++++++ lab-python-flow-control.ipynb | 10 +- 4 files changed, 577 insertions(+), 2 deletions(-) create mode 100644 .ipynb_checkpoints/Lab_Customer_Orders_Optimized-checkpoint.ipynb create mode 100644 .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb create mode 100644 Lab_Customer_Orders_Optimized.ipynb diff --git a/.ipynb_checkpoints/Lab_Customer_Orders_Optimized-checkpoint.ipynb b/.ipynb_checkpoints/Lab_Customer_Orders_Optimized-checkpoint.ipynb new file mode 100644 index 0000000..ec90826 --- /dev/null +++ b/.ipynb_checkpoints/Lab_Customer_Orders_Optimized-checkpoint.ipynb @@ -0,0 +1,250 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "ae754247-9485-4a86-8907-4c12180eb568", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "68cc9e4b-be44-4963-b103-f79d607f01bc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=== Inventory Setup ===\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 10\n", + "Enter quantity for mug: 15\n", + "Enter quantity for hat: 18\n", + "Enter quantity for book: 12\n", + "Enter quantity for keychain: 13\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Current Inventory: {'t-shirt': 10, 'mug': 15, 'hat': 18, 'book': 12, 'keychain': 13}\n" + ] + } + ], + "source": [ + "# ----------------------------\n", + "# Lab | Data Structures – Optimized Customer Orders\n", + "# Student: Isailton da Silva\n", + "# ----------------------------\n", + "\n", + "# Step 1: Define the products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Step 2: Create an empty inventory dictionary\n", + "inventory = {}\n", + "\n", + "# Step 3: Fill inventory with quantities from user input\n", + "print(\"=== Inventory Setup ===\")\n", + "for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative. Try again.\")\n", + " else:\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + "\n", + "print(\"\\nCurrent Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "37995ef0-2b61-481a-8736-0ac6018ae150", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Customer Orders ===\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: t-shirt\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: {'t-shirt', 'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "# Step 4: Create empty set for customer orders\n", + "customer_orders = set()\n", + "\n", + "# Step 5: Loop to add products to the customer order\n", + "print(\"\\n=== Customer Orders ===\")\n", + "while True:\n", + " product = input(\"Enter a product to order: \").lower()\n", + " \n", + " if product not in products:\n", + " print(\"Product not in store. Please choose from:\", products)\n", + " continue\n", + " \n", + " customer_orders.add(product)\n", + " \n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break\n", + "\n", + "print(\"\\nCustomer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1bdb08c4-9dee-47bf-b4c2-1c8afc4b3f01", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Order Statistics ===\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 80.00%\n" + ] + } + ], + "source": [ + "# Step 6: Calculate order statistics\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\n=== Order Statistics ===\")\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "85f64135-f716-4357-bc74-f9a3a5316f3a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Updated Inventory ===\n", + "t-shirt: 9\n", + "mug: 14\n", + "hat: 17\n", + "book: 11\n", + "keychain: 13\n" + ] + } + ], + "source": [ + "# Step 7: Update inventory only for products actually ordered\n", + "for product in customer_orders:\n", + " if inventory.get(product, 0) > 0:\n", + " inventory[product] -= 1\n", + "\n", + "print(\"\\n=== Updated Inventory ===\")\n", + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0462df5f-1b7e-4de2-9ce9-8743dd73ff54", + "metadata": {}, + "source": [] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..1f05e3a --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,69 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "ae754247-9485-4a86-8907-4c12180eb568", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "markdown", + "id": "0462df5f-1b7e-4de2-9ce9-8743dd73ff54", + "metadata": {}, + "source": [] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab_Customer_Orders_Optimized.ipynb b/Lab_Customer_Orders_Optimized.ipynb new file mode 100644 index 0000000..ec90826 --- /dev/null +++ b/Lab_Customer_Orders_Optimized.ipynb @@ -0,0 +1,250 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "ae754247-9485-4a86-8907-4c12180eb568", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "68cc9e4b-be44-4963-b103-f79d607f01bc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=== Inventory Setup ===\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 10\n", + "Enter quantity for mug: 15\n", + "Enter quantity for hat: 18\n", + "Enter quantity for book: 12\n", + "Enter quantity for keychain: 13\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Current Inventory: {'t-shirt': 10, 'mug': 15, 'hat': 18, 'book': 12, 'keychain': 13}\n" + ] + } + ], + "source": [ + "# ----------------------------\n", + "# Lab | Data Structures – Optimized Customer Orders\n", + "# Student: Isailton da Silva\n", + "# ----------------------------\n", + "\n", + "# Step 1: Define the products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Step 2: Create an empty inventory dictionary\n", + "inventory = {}\n", + "\n", + "# Step 3: Fill inventory with quantities from user input\n", + "print(\"=== Inventory Setup ===\")\n", + "for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative. Try again.\")\n", + " else:\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + "\n", + "print(\"\\nCurrent Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "37995ef0-2b61-481a-8736-0ac6018ae150", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Customer Orders ===\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: t-shirt\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: {'t-shirt', 'hat', 'book', 'mug'}\n" + ] + } + ], + "source": [ + "# Step 4: Create empty set for customer orders\n", + "customer_orders = set()\n", + "\n", + "# Step 5: Loop to add products to the customer order\n", + "print(\"\\n=== Customer Orders ===\")\n", + "while True:\n", + " product = input(\"Enter a product to order: \").lower()\n", + " \n", + " if product not in products:\n", + " print(\"Product not in store. Please choose from:\", products)\n", + " continue\n", + " \n", + " customer_orders.add(product)\n", + " \n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break\n", + "\n", + "print(\"\\nCustomer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1bdb08c4-9dee-47bf-b4c2-1c8afc4b3f01", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Order Statistics ===\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 80.00%\n" + ] + } + ], + "source": [ + "# Step 6: Calculate order statistics\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\n=== Order Statistics ===\")\n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "85f64135-f716-4357-bc74-f9a3a5316f3a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Updated Inventory ===\n", + "t-shirt: 9\n", + "mug: 14\n", + "hat: 17\n", + "book: 11\n", + "keychain: 13\n" + ] + } + ], + "source": [ + "# Step 7: Update inventory only for products actually ordered\n", + "for product in customer_orders:\n", + " if inventory.get(product, 0) > 0:\n", + " inventory[product] -= 1\n", + "\n", + "print(\"\\n=== Updated Inventory ===\")\n", + "for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0462df5f-1b7e-4de2-9ce9-8743dd73ff54", + "metadata": {}, + "source": [] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..1f05e3a 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "id": "ae754247-9485-4a86-8907-4c12180eb568", "metadata": {}, "source": [ "## Exercise: Managing Customer Orders Optimized\n", @@ -37,6 +37,12 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "markdown", + "id": "0462df5f-1b7e-4de2-9ce9-8743dd73ff54", + "metadata": {}, + "source": [] } ], "metadata": { @@ -55,7 +61,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,