diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..09fc9ed 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,102 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ba2530dc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventario: {'t-shirt': 1, 'mug': 2, 'hat': 1, 'book': 1, 'keychain': 1}\n", + "Customer Orders: ['mug', 'mug', 'hat']\n", + "Order Statistics: (2, 40.0)\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.00%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 1\n", + "book: 1\n", + "keychain: 1\n", + "Total Price: 2.0\n" + ] + } + ], + "source": [ + "# 1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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", + "\n", + "inventory = initialize_inventory(products)\n", + "print(\"Inventario: \", inventory)\n", + "\n", + "#*Hint: Apply it to initialize inventory, updating the inventory and printing the updated inventory.*\n", + "\n", + "# 2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the \n", + "# product names using a loop and user input. Use comprehension.\n", + "def get_customer_orders():\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " return [input(\"Enter the name of a product that a customer wants to order: t-shirt, mug, hat, book, keychain \").strip().lower()\n", + " for _ in range(num_orders)]\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(\"Customer Orders: \", customer_orders)\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\n", + "# 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 get_total_price(customer_orders):\n", + " unique_orders = list(dict.fromkeys(customer_orders)) # 1 unidad por producto (preserva orden)\n", + " total_price = sum(float(input(f\"Enter the price of {product}: \")) for product in unique_orders)\n", + " return total_price\n", + "\n", + "# 4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer\n", + "# orders. Use comprehension to filter out the products with a quantity of zero from the inventory.\n", + "\n", + "def update_inventory(customer_orders, inventory): \n", + " u = set(customer_orders) # 1 unidad por producto\n", + " inventory = {p: (q - (1 if p in u else 0)) for p, q in inventory.items() if (q - (1 if p in u else 0)) > 0}\n", + " return inventory\n", + " \n", + "\n", + "# 5. Print the total price of the customer order.\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(set(customer_orders))\n", + " percentage_unique_products_ordered = (len(set(customer_orders)) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique_products_ordered\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(\"Order Statistics: \", order_statistics)\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_unique_products_ordered = order_statistics\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_unique_products_ordered:.2f}%\")\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + "print(f\"Percentage of Unique Products Ordered: {order_statistics[1]:.2f}%\")\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print(\"\\nUpdated Inventory:\")\n", + "[print(f\"{p}: {inventory[p]}\") for p in products if p in inventory]\n", + "\n", + "total_price = get_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -93,7 +184,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,