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
260 changes: 260 additions & 0 deletions lab-python-data-structures.ipynb.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"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": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"#1 Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print (products)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#2. Create an empty dictionary called `inventory`.\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many t-shirt put in a basket? : 3\n",
"How many mug to put in a basket? : 2\n",
"How many hat put in a basket? : 4\n",
"How many book put in a basket? : 5\n",
"How many keychain put in a basket?: 1\n"
]
}
],
"source": [
"#3. Ask the user to input the quantity of each product available in the inventory. \n",
"#Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n",
"inventory [\"t-shirt\"] = int (input (f\"How many {products [0]} put in a basket? : \"))\n",
"inventory [\"mug\"] = int (input (f\"How many {products [1]} to put in a basket? : \"))\n",
"inventory [\"hat\"] = int (input (f\"How many {products [2]} put in a basket? : \"))\n",
"inventory [\"book\"] = int (input (f\"How many {products [3]} put in a basket? : \"))\n",
"inventory [\"keychain\"] = int (input (f\"How many {products [4]} put in a basket?: \"))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#4. Create an empty set called `customer_orders`.\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select 3 items to check out from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] (comma-separated): hat, mug, book\n"
]
}
],
"source": [
"#5. Ask the user to input the name of three products that a customer wants to order \n",
"#(from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". \n",
"#Add each product name to the `customer_orders` set.\n",
"checkout_items = input(f\"Select 3 items to check out from {products} (comma-separated): \")\n",
"checkout_items_list = checkout_items.split(\",\")\n",
"for item in checkout_items_list: #all items need to be put \n",
" customer_orders.add(item.strip())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"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",
"#Store these statistics in a tuple called `order_status`.\n",
"#for product_name in customer_orders: \n",
" #customer_order_sum =0 \n",
" #customer_order_sum += inventory.get(product_name, 0)\n",
"total_products_ordered = sum(inventory.values())\n",
"customer_order_sum = 0\n",
"for item_name in customer_orders:\n",
" customer_order_sum += inventory.get(item_name, 0)\n",
"\n",
"total_available_products = sum(inventory.values()) # Sum of all inventories\n",
"percentage_of_product_ordered = (customer_order_sum / total_available_products) * 100\n",
"order_status = (total_products_ordered, percentage_of_product_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered 15, Percentage of Products Ordered:73%\n"
]
}
],
"source": [
"#8. Print the order statistics using the following format:\n",
"#Order Statistics:Total Products Ordered: <total_products_ordered> \n",
"#Percentage of Products Ordered: <percentage_ordered>% \n",
"print (f\"Total Products Ordered {total_products_ordered},\", f\"Percentage of Products Ordered:{int(percentage_of_product_ordered)}%\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"#9. Update the inventory by subtracting 1 from the quantity of each product. \n",
"#Modify the `inventory` dictionary accordingly.\n",
"for item_name in customer_orders : \n",
" if inventory.get(item_name, 0) > 0 :\n",
" inventory [item_name] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current inventory of t-shirt : 3\n",
"Current inventory of mug : 0\n",
"Current inventory of hat : 2\n",
"Current inventory of book : 3\n",
"Current inventory of keychain : 1\n",
"Total Items Ordered 15\n",
"Percentage of Items Ordered vs inventory:73%\n"
]
}
],
"source": [
"#10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"#Solve the exercise by implementing the steps using the Python concepts of lists, \n",
"#dictionaries, sets, and basic input/output operations.\n",
"for product, quantity in inventory.items () : \n",
" print (f\"Current inventory of {product} : {quantity}\")\n",
"\n",
"print (f\"Total Items Ordered {total_products_ordered}\")\n",
"print (f\"Percentage of Items Ordered vs inventory:{int(percentage_of_product_ordered)}%\")"
]
},
{
"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.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}