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

LAB4 #257

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

LAB4 #257

Show file tree
Hide file tree
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
231 changes: 231 additions & 0 deletions .ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
{
"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": null,
"id": "d77400a0-c260-46a8-89aa-fe727059bd99",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb7e3905-ff7c-49e7-9db9-d83c345028b5",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6f54de4-9a93-4643-8cec-3ed6c0a1e522",
"metadata": {},
"outputs": [],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf3f3e54-9e36-4cd1-96d4-49b2b951c903",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" num_pedidos = int(input(\"Ingrese el número de pedidos del cliente: \"))\n",
" return [input('Introduce el nombre del producto que deseas pedir: ') for _ in range(num_pedidos)]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc09b94f-20eb-4f30-a01f-f3aba5b0dae2",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" inventory = {product: inventory[product] - 1 if product in customer_orders else inventory[product] for product in inventory}\n",
" inventory = {product: cantidad for product, cantidad in inventory.items() if cantidad > 0}\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f42d74e7-1cf3-458d-9d09-1b6b0381d26a",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"\\nInventario actualizado:\")\n",
" [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fbd4b33-f7fd-424f-90b9-8d494bda916d",
"metadata": {},
"outputs": [],
"source": [
"for product in customer_orders:\n",
" if product in inventory and inventory[costumer_orders] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"No hay suficiente inventario para el producto: {product}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f05aa32-fab1-480d-ab2f-d785e427f17b",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = sum(float(input(f\"Ingrese el precio de {producto}: \")) for producto in customer_orders)\n",
" return total_price\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa34ed01-14b7-45eb-95b0-a0886536eca0",
"metadata": {},
"outputs": [],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b045ab9-3c9f-4a31-ad3e-59847d31e89b",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca4d3a40-d3e9-48cf-a374-ddcbcb912be2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
132 changes: 131 additions & 1 deletion lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,136 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d77400a0-c260-46a8-89aa-fe727059bd99",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb7e3905-ff7c-49e7-9db9-d83c345028b5",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6f54de4-9a93-4643-8cec-3ed6c0a1e522",
"metadata": {},
"outputs": [],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf3f3e54-9e36-4cd1-96d4-49b2b951c903",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" num_pedidos = int(input(\"Ingrese el número de pedidos del cliente: \"))\n",
" return [input('Introduce el nombre del producto que deseas pedir: ') for _ in range(num_pedidos)]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc09b94f-20eb-4f30-a01f-f3aba5b0dae2",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" inventory = {product: inventory[product] - 1 if product in customer_orders else inventory[product] for product in inventory}\n",
" inventory = {product: cantidad for product, cantidad in inventory.items() if cantidad > 0}\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f42d74e7-1cf3-458d-9d09-1b6b0381d26a",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"\\nInventario actualizado:\")\n",
" [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fbd4b33-f7fd-424f-90b9-8d494bda916d",
"metadata": {},
"outputs": [],
"source": [
"for product in customer_orders:\n",
" if product in inventory and inventory[costumer_orders] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"No hay suficiente inventario para el producto: {product}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f05aa32-fab1-480d-ab2f-d785e427f17b",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = sum(float(input(f\"Ingrese el precio de {producto}: \")) for producto in customer_orders)\n",
" return total_price\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa34ed01-14b7-45eb-95b0-a0886536eca0",
"metadata": {},
"outputs": [],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b045ab9-3c9f-4a31-ad3e-59847d31e89b",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca4d3a40-d3e9-48cf-a374-ddcbcb912be2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -93,7 +223,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down