Skip to content

lab solved #482

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
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
281 changes: 281 additions & 0 deletions .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"# Lab | Data Structures "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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:\n",
"\n",
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"\n",
"2. Create an empty dictionary called `inventory`.\n",
"\n",
"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.\n",
"\n",
"4. Create an empty set called `customer_orders`.\n",
"\n",
"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.\n",
"\n",
"6. Print the products in the `customer_orders` set.\n",
"\n",
"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`.\n",
"\n",
"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",
" ```\n",
"\n",
"9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"\n",
"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": 1,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many t-shirts in the inventory? 204\n",
"How many mugs in the inventory? 347\n",
"How many hats in the inventory? 389\n",
"How many books in the inventory? 216\n",
"How many keychains in the inventory? 110\n"
]
}
],
"source": [
"nts = int(input(\"How many t-shirts in the inventory?\"))\n",
"nm = int(input(\"How many mugs in the inventory?\"))\n",
"nh = int(input(\"How many hats in the inventory?\"))\n",
"nb = int(input(\"How many books in the inventory?\"))\n",
"nk = int(input(\"How many keychains in the inventory?\"))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 204, 'mug': 347, 'hat': 389, 'book': 216, 'keychain': 110}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory[products[0]] = nts\n",
"inventory [products[1]] = nm\n",
"inventory [products[2]] = nh\n",
"inventory [products[3]] = nb\n",
"inventory [products[4]] = nk\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Hello! Tell me the first of the three products you want to order from the following list: t-shirt, mug, hat, book and keychain: t-shirt\n",
"Now, the second product of your order: mug\n",
"And finally, the third product: hat\n"
]
}
],
"source": [
"first_prod = input(\"Hello! Tell me the first of the three products you want to order from the following list: t-shirt, mug, hat, book and keychain:\")\n",
"sec_prod = input(\"Now, the second product of your order:\")\n",
"third_prod = input(\"And finally, the third product:\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'mug', 't-shirt'}\n"
]
}
],
"source": [
"customer_orders.add(first_prod)\n",
"customer_orders.add(sec_prod)\n",
"customer_orders.add(third_prod)\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 60.0)\n"
]
}
],
"source": [
"tpo = len(customer_orders)\n",
"tap = len(products)\n",
"perc_order = tpo/tap * 100\n",
"order_status = (tpo, perc_order)\n",
"print(order_status)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"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(f\"\"\"Order Statistics: \n",
"Total Products Ordered: {tpo}\n",
"Percentage of Products Ordered: {perc_order} %\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"inventory[products[0]] = nts - 1\n",
"inventory [products[1]] = nm - 1\n",
"inventory [products[2]] = nh - 1\n",
"inventory [products[3]] = nb - 1\n",
"inventory [products[4]] = nk - 1"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T-shirts: 203\n",
"Mugs: 346\n",
"Hats: 388\n",
"Books: 215\n",
"Keychains: 109\n"
]
}
],
"source": [
"print(f\"\"\"T-shirts: {inventory['t-shirt']}\n",
"Mugs: {inventory['mug']}\n",
"Hats: {inventory['hat']}\n",
"Books: {inventory['book']}\n",
"Keychains: {inventory['keychain']}\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading