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
228 changes: 194 additions & 34 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,215 @@
"tags": []
},
"source": [
"# Lab | Data Structures "
"# Lab | Data Structures | Samia"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"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",
"# 1.defining the list products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n"
]
}
],
"source": [
"# 2. creating an empty dictionary invetory\n",
"inventory = {}\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# 3. input of the user for quantity of each product\n",
"for i in range(len(products)):\n",
" quantity = int(input(\"Enter the quantity for \" + products[i] + \": \"))\n",
" inventory[products[i]] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 10, 'hat': 15, 'book': 20, 'keychain': 25}\n"
]
}
],
"source": [
"# 3.5 checking our dictionary inventory\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set()\n"
]
}
],
"source": [
"# 4. creating an empty set customer orders\n",
"customer_orders = set()\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is the list of products:\n",
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Enter the items of the 3 products to order:\n",
"mug\n",
"book\n",
"hat\n"
]
}
],
"source": [
"# 5. user input for 3 products to order\n",
"print(\"Here is the list of products:\")\n",
"print(products)\n",
"\n",
"6. Print the products in the `customer_orders` set.\n",
"print(\"Enter the items of the 3 products to order:\")\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",
"for i in range(3):\n",
" item = input(\"Enter a product name: \").lower()\n",
" print(item)\n",
" if item in products:\n",
" customer_orders.add(item)\n",
" else:\n",
" print(\"Invalid product. Please choose from the list.\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer Orders: {'hat', 'book', 'mug'}\n"
]
}
],
"source": [
"# 6. Printing the ordered products\n",
"print(\"Customer Orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total unique products ordered: 3\n",
"Percentage of products ordered: 60.0 %\n"
]
}
],
"source": [
"# 7+8. Calculating order statistics and Printing order statistics\n",
"print(\"Order Statistics:\")\n",
"# total of products ordered\n",
"total_products_ordered = len(customer_orders)\n",
"print(\"Total unique products ordered:\", total_products_ordered)\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",
"# percentage of products ordered\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n",
"\n",
"9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"# Storing order status\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"# Printing order status\n",
"#print(\"Order Status (Total, Percentage):\", order_status)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt : 5\n",
"mug : 9\n",
"hat : 14\n",
"book : 19\n",
"keychain : 25\n"
]
}
],
"source": [
"# 9. Updating the inventory by subtracting 1 for each ordered product\n",
"for ordered_product in customer_orders:\n",
" if inventory[ordered_product] > 0:\n",
" inventory[ordered_product] = inventory[ordered_product] - 1\n",
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
"# 10. Printing the updated inventory\n",
"print(\"Updated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(product, \":\", quantity)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +228,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down