Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 148 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,157 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "ea69cf8c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Initialize Inventory ---\n",
"\n",
"--- Get Customer Orders ---\n",
"Enter product names to order. Type 'done' when finished.\n",
"-> Added 'Cap' to your order list.\n",
"-> Added 'Hoodie' to your order list.\n",
"-> Added 'Hat' to your order list.\n",
"-> Added 'T-Shirt' to your order list.\n",
"Warning: 'Hat' does not exist in inventory!\n",
"\n",
"--- Order Statistics ---\n",
"Total products ordered: 4\n",
"Percentage of unique products ordered: 75.00%\n",
"\n",
"--- Updated Inventory ---\n",
"T-Shirt: 99\n",
"Hoodie: 199\n",
"Cap: 299\n",
"Socks: 400\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"def initialize_inventory(products):\n",
" \"\"\"1. Initializes the inventory dictionary using a loop and user input.\"\"\"\n",
" inventory = {}\n",
" print(\"\\n--- Initialize Inventory ---\")\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter the stock quantity for '{product}': \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" break\n",
" print(\"Quantity cannot be negative.\")\n",
" except ValueError:\n",
" print(\"Please enter a valid whole number.\")\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"2. Prompts the user to enter product names and returns a unique set.\"\"\"\n",
" customer_orders = set()\n",
" print(\"\\n--- Get Customer Orders ---\")\n",
" print(\"Enter product names to order. Type 'done' when finished.\")\n",
" \n",
" while True:\n",
" order = input(\"Enter product name: \").strip()\n",
" if order.lower() == 'done':\n",
" break\n",
" if order:\n",
" customer_orders.add(order)\n",
" print(f\"-> Added '{order}' to your order list.\")\n",
" \n",
" return customer_orders\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"3. Updates the inventory dictionary based on customer orders (case-insensitive).\"\"\"\n",
" inventory_lowercase = {product.lower(): product for product in inventory}\n",
"\n",
" for order in customer_orders:\n",
" order_lower = order.lower()\n",
" \n",
" if order_lower in inventory_lowercase:\n",
" original_name = inventory_lowercase[order_lower]\n",
" if inventory[original_name] > 0:\n",
" inventory[original_name] -= 1\n",
" else:\n",
" print(f\"Warning: '{original_name}' is out of stock!\")\n",
" else:\n",
" print(f\"Warning: '{order}' does not exist in inventory!\")\n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"4. Calculates total products ordered and percentage using lambda and filter().\"\"\"\n",
" total_products_ordered = len(customer_orders)\n",
" \n",
" # Lowercase product list for exact matching\n",
" products_lower = [p.lower() for p in products]\n",
" \n",
" # Use lambda and filter() to keep only the orders that exist in our product catalog\n",
" valid_orders_iterator = filter(lambda order: order.lower() in products_lower, customer_orders)\n",
" \n",
" # Count how many elements passed the filter criteria\n",
" valid_orders_count = len(list(valid_orders_iterator))\n",
" \n",
" if len(products) > 0:\n",
" percentage_ordered = (valid_orders_count / len(products)) * 100\n",
" else:\n",
" percentage_ordered = 0.0\n",
" \n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"5. Prints the calculated order statistics.\"\"\"\n",
" total, percentage = order_statistics\n",
" print(\"\\n--- Order Statistics ---\")\n",
" print(f\"Total products ordered: {total}\")\n",
" print(f\"Percentage of unique products ordered: {percentage:.2f}%\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"6. Prints the final updated inventory.\"\"\"\n",
" print(\"\\n--- Updated Inventory ---\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"# 7. Call the functions in the appropriate sequence to execute the program\n",
"if __name__ == \"__main__\":\n",
" products = [\"T-Shirt\", \"Hoodie\", \"Cap\", \"Socks\"]\n",
" \n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" update_inventory(customer_orders, inventory)\n",
" \n",
" # This step now executes your new lambda-filter pipeline internally\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" \n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00aecdcd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +207,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down