Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved lab #358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
280 changes: 280 additions & 0 deletions .ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"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",
"\n",
" ```\n",
"<br>\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",
"\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",
"\n",
"5. Print the total price of the customer order.\n",
"\n",
"Your code should produce output similar to the following:\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",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.0\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"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "689cbc7c-09f9-4d92-bacd-77fbe03e91e3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 10\n",
"Enter the quantity of mugs available: 5\n",
"Enter the quantity of hats available: 10\n",
"Enter the quantity of books available: 10\n",
"Enter the quantity of keychains available: 10\n"
]
}
],
"source": [
"#1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\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"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8234cf71-7e06-4323-ab59-d65d8c9cd709",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 3\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: book\n",
"Enter the name of a product that a customer wants to order: keychain\n"
]
}
],
"source": [
"#2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders \n",
"# and gathers the product names using a loop and user input. Use comprehension.\n",
"\n",
"def get_customer_orders():\n",
" num_orders = int(input(\"Enter the number of customer orders:\"))\n",
" customer_orders = {input(\"Enter the name of a product that a customer wants to order: \"): 1 for i in range(num_orders)}\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "4b9aadc2-434e-4cf2-9987-c4c01f9412f8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 3\n",
"Percentage of unique products ordered: 60.00%\n"
]
},
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = sum(customer_orders.values()) \n",
" unique_products_ordered = len(customer_orders) \n",
" total_unique_products = len(products) \n",
" \n",
" percentage_unique = (unique_products_ordered / total_unique_products) * 100\n",
"\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_unique:.2f}%\")\n",
"\n",
" return total_products_ordered, percentage_unique\n",
"\n",
"\n",
"calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "fe575a6b-b160-429e-aa62-7801693c1db4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: \n",
"{'t-shirt': 10, 'mug': 5, 'hat': 7, 'book': 7, 'keychain': 7}\n"
]
}
],
"source": [
"#3. Add a new function to calculate the total price of the customer order. For each product in customer_orders, \n",
"# prompt the user to enter the price of that product. \n",
"# Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" inventory = {item: (inventory[item] - 1) if item in customer_orders else inventory[item] for item in inventory}\n",
" print(f\"Updated inventory: \\n{inventory}\")\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "6826bec6-7218-4526-8176-9f60b5103eb6",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of hats: 10\n",
"Enter the price of books: 5\n",
"Enter the price of keychains: 10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Price: 25\n"
]
},
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def total_price(customer_orders):\n",
" item_prices = {order: int(input(f\"Enter the price of {order}s: \")) for order in customer_orders}\n",
" total_cost = sum(item_prices.values())\n",
" print(f\"Total Price: {total_cost}\")\n",
" return total_cost\n",
"\n",
"total_price(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11956625-3545-4dea-a360-0293e5266316",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}