diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..1fc8496 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -1,87 +1,75 @@ { "cells": [ { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "cell_type": "code", + "execution_count": null, + "id": "20f4728e-ecdb-46f3-8770-84c74bf755ba", "metadata": {}, + "outputs": [], "source": [ - "# Lab | List, Dict and Set Comprehension" - ] - }, - { - "cell_type": "markdown", - "id": "7dd3cbde-675a-4b81-92c3-f728846dbe06", - "metadata": {}, - "source": [ - "## Exercise: Managing Customer Orders Optimized with Comprehension" - ] - }, - { - "cell_type": "markdown", - "id": "5d500160-2fb7-4777-b5e4-09d45ebaf328", - "metadata": {}, - "source": [ - "In the previous exercise, you developed a program to manage customer orders and inventory. Now, let's take it a step further and incorporate comprehension into your code.\n", - "\n", - "Follow the steps below to complete the exercise:\n", - "\n", - "1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n", - "\n", - " - *Hint: Apply it to initialize inventory, updating the inventory and printing the updated inventory.*\n", - " \n", - " - For example, in initializing the inventory, we could have:\n", - " \n", - " ```python\n", - " def initialize_inventory(products):\n", - " inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", - " return inventory\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", - " ```\n", - "
\n", - " \n", - " \n", - "2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input. Use comprehension.\n", "\n", - "3. Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n", + "def initialize_inventory(products):\n", + " return {product: int(input(f\"Enter the quantity of {product}: \")) for product in products}\n", "\n", - "4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory.\n", + "def get_customer_orders():\n", + " num_orders = int(input(\"Enter the number of products you want to order: \"))\n", + " return {input(f\"Enter the name of a product that the customer wants to order: \")\n", + " for _ in range(num_orders)}\n", "\n", - "5. Print the total price of the customer order.\n", + "def update_inventory(inventory, customer_orders):\n", + " updated_inventory = {\n", + " product: (qty - 1 if product in customer_orders else qty)\n", + " for product, qty in inventory.items()\n", + " }\n", + " return {p: q for p,q in updated_inventory.items() if q > 0}\n", "\n", - "Your code should produce output similar to the following:\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = total_products_ordered / len(products) * 100\n", + " order_status = (total_products_ordered, percentage_ordered)\n", + " # return total_products_ordered, percentage_ordered\n", + " return order_status\n", "\n", - "```python\n", - "Enter the quantity of t-shirts available: 5\n", - "Enter the quantity of mugs available: 4\n", - "Enter the quantity of hats available: 3\n", - "Enter the quantity of books available: 2\n", - "Enter the quantity of keychains available: 1\n", - "Enter the number of customer orders: 2\n", - "Enter the name of a product that a customer wants to order: hat\n", - "Enter the name of a product that a customer wants to order: keychain\n", + "def print_order_statistics(order_statistics):\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", order_statistics[0])\n", + " print(\"Percentage of Products Ordered:\", order_statistics[1], \"%\")\n", "\n", - "Order Statistics:\n", - "Total Products Ordered: 2\n", - "Percentage of Unique Products Ordered: 40.0\n", + "def print_updated_inventory(inventory):\n", + " print(\"Inventory after order:\")\n", + " for item in inventory:\n", + " print(item, inventory[item])\n", "\n", - "Updated Inventory:\n", - "t-shirt: 5\n", - "mug: 4\n", - "hat: 2\n", - "book: 2\n", - "Enter the price of keychain: 5\n", - "Enter the price of hat: 10\n", - "Total Price: 15.0\n", - "\n", - "```\n" + "def calculate_total_price(customer_order):\n", + " prices = {product: float(input(f\"Enter the price of the product {product}:\")) for product in customer_order }\n", + " total_price = sum(prices.values())\n", + " return total_price\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(inventory, customer_orders)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "total_price = calculate_total_price(customer_orders)\n", + "print_updated_inventory(inventory)\n", + "print(f\"Total price of the order: {total_price}\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7614c047-2078-4b15-8f3e-5fc6eb1eb268", + "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": { @@ -93,7 +81,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,