Skip to content

Solved lab #521

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

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
374 changes: 374 additions & 0 deletions lab-python-data-structures-CLEAN.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,374 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "12e921a2",
"metadata": {},
"source": [
"# Lab | Data Structures \n",
"## Exercise: Managing Customer Orders\n",
"\n",
"As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n",
"\n",
"Follow the steps below to complete the exercise:"
]
},
{
"cell_type": "markdown",
"id": "68c616b4",
"metadata": {},
"source": [
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "88dceb12",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "markdown",
"id": "82bb9daf",
"metadata": {},
"source": [
"2. Create an empty dictionary called `inventory`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "523aac4d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n"
]
}
],
"source": [
"inventory = {}\n",
"print(inventory)"
]
},
{
"cell_type": "markdown",
"id": "9377e346",
"metadata": {},
"source": [
"3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "730d595d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"¿Cuántos 't-shirt' hay en inventario? 10\n",
"¿Cuántos 'mug' hay en inventario? 8\n",
"¿Cuántos 'hat' hay en inventario? 7\n",
"¿Cuántos 'book' hay en inventario? 5\n",
"¿Cuántos 'keychain' hay en inventario? 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 8, 'hat': 7, 'book': 5, 'keychain': 8}\n"
]
}
],
"source": [
"for product in products:\n",
" quantity = int(input(f\"How many '{product}' are available in inventory? \"))\n",
" inventory[product] = quantity\n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "markdown",
"id": "5f1d5563",
"metadata": {},
"source": [
"4. Create an empty set called `customer_orders`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7f4a92ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set()\n"
]
}
],
"source": [
"customer_orders = set()\n",
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "e223325c",
"metadata": {},
"source": [
"5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6db1e015",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of product #1 the customer wants to order: mug\n",
"Enter the name of product #2 the customer wants to order: book\n",
"Enter the name of product #3 the customer wants to order: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'hat', 'mug'}\n"
]
}
],
"source": [
"for i in range(3):\n",
" product = input(f\"Enter the name of product #{i+1} the customer wants to order: \").lower()\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" else:\n",
" print(f\"'{product}' is not a valid product.\")\n",
"\n",
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "dabde211",
"metadata": {},
"source": [
"6. Print the products in the `customer_orders` set."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "413021e4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'hat', 'mug'}\n"
]
}
],
"source": [
"print(customer_orders)\n"
]
},
{
"cell_type": "markdown",
"id": "9c4a20a3",
"metadata": {},
"source": [
"7. Calculate the following order statistics:\n",
" - Total Products Ordered: The total number of products in the `customer_orders` set.\n",
" - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
" \n",
" Store these statistics in a tuple called `order_status`."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1804647b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 60.0)\n"
]
}
],
"source": [
"total_ordered = len(customer_orders)\n",
"total_available = len(products)\n",
"percentage_ordered = (total_ordered / total_available) * 100\n",
"\n",
"order_status = (total_ordered, percentage_ordered)\n",
"\n",
"print(order_status)\n"
]
},
{
"cell_type": "markdown",
"id": "85f9feda",
"metadata": {},
"source": [
"8. Print the order statistics using the following format:\n",
" ```\n",
" Order Statistics:\n",
" Total Products Ordered: <total_products_ordered>\n",
" Percentage of Products Ordered: <percentage_ordered>% \n",
" ```"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8b9ac871",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n"
]
},
{
"cell_type": "markdown",
"id": "74b60e9a",
"metadata": {},
"source": [
"9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c7d8bcc9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 10, 'mug': 7, 'hat': 6, 'book': 4, 'keychain': 8}\n"
]
}
],
"source": [
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} is out of stock!\")\n",
"\n",
"print(inventory)\n"
]
},
{
"cell_type": "markdown",
"id": "6539c3be",
"metadata": {},
"source": [
"10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b8fbbcf8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 7\n",
"hat: 6\n",
"book: 4\n",
"keychain: 8\n"
]
}
],
"source": [
"print(\"Updated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "342212ab-99df-4436-adaa-a4a1eb12ad4c",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}