diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..283b90c 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,138 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "dc7160d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Unique Products Ordered: 60.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 4\n", + "mug: 3\n", + "hat: 4\n", + "book: 5\n", + "keychain: 3\n", + "Total Price: 45.0\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " \"\"\"\n", + " Build the inventory dict using a dict comprehension + user input.\n", + " Example input prompt: \"Enter the quantity of t-shirts available: \"\n", + " \"\"\"\n", + " return {\n", + " product: int(input(f\"Enter the quantity of {product}s available: \"))\n", + " for product in products\n", + " }\n", + "\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"\n", + " Ask how many orders to take, then gather product names.\n", + " Uses a set comprehension so duplicates collapse automatically.\n", + " \"\"\"\n", + " n = int(input(\"Enter the number of customer orders: \"))\n", + " return {\n", + " input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " for _ in range(n)\n", + " }\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Deduct one unit (at most 1 per product) for each ordered item that exists and has stock.\n", + " Then remove zero-quantity products using a dict comprehension.\n", + " Returns the updated (filtered) inventory.\n", + " \"\"\"\n", + " for item in customer_orders:\n", + " if item in inventory and inventory[item] > 0:\n", + " inventory[item] -= 1\n", + "\n", + " # Filter out zero-quantity items via dict comprehension\n", + " inventory = {k: v for k, v in inventory.items() if v > 0}\n", + " return inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Compute total unique products ordered and percentage vs catalog size.\n", + " \"\"\"\n", + " total_unique = len(customer_orders)\n", + " percent_unique = (total_unique / len(products) * 100) if products else 0.0\n", + " return total_unique, percent_unique\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_unique, percent_unique = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_unique}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percent_unique:.1f}\")\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Print the inventory in a compact way using a generator expression.\n", + " \"\"\"\n", + " print(\"\\nUpdated Inventory:\")\n", + " if not inventory:\n", + " print(\"(All items are out of stock.)\")\n", + " return\n", + " print(\"\\n\".join(f\"{k}: {v}\" for k, v in inventory.items()))\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " \"\"\"\n", + " Prompt for the price of each ordered product and sum them.\n", + " Uses a dict comprehension to capture per-item prices (optional to reuse),\n", + " then sums values with a generator expression.\n", + " Assumes 1 unit per product.\n", + " \"\"\"\n", + " prices = {item: float(input(f\"Enter the price of {item}: \")) for item in customer_orders}\n", + " total = sum(prices.values())\n", + " return total\n", + "\n", + "\n", + "if __name__ == \"__main__\":\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders()\n", + "\n", + " stats = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(stats)\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " print_updated_inventory(inventory)\n", + "\n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"Total Price: {total_price:.1f}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6e1344ff", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -93,7 +220,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,