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
259 changes: 256 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"tags": []
},
"source": [
"# Lab | Data Structures "
"# Lab 01 | Data Structures\n",
"----"
]
},
{
Expand Down Expand Up @@ -50,11 +51,263 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solved Lab 01 | Data Structures\n",
"----"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# 1.defining the list products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Create an empty dictionary called `inventory`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 2. creating an empty dictionary invetory\n",
"inventory = {}\n",
"print(inventory)"
]
},
{
"cell_type": "markdown",
"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": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n",
"{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n"
]
}
],
"source": [
"# 3. input of the user for quantity of each product\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"print(inventory)\n",
"\n",
"for i in range(len(products)):\n",
" while True: # keep asking until valid\n",
" quantity = input(\"Enter the quantity for \" + products[i] + \": \")\n",
" if quantity.isdigit(): # checks if input is a positive integer\n",
" quantity = int(quantity)\n",
" inventory[products[i]] = quantity\n",
" break # valid → exit loop\n",
" else:\n",
" print(f\"❌ Invalid input! Please enter a numeric quantity for {products[i]}.\")\n",
"\n",
"# 3.5 checking our dictionary inventory\n",
"print(inventory)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Create an empty set called `customer_orders`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 4. creating an empty set customer orders\n",
"customer_orders = set()\n",
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# 5. User input for 3 products to order\n",
"print(\"Here is the list of products:\")\n",
"print(products)\n",
"\n",
"print(\"Enter the names of 3 products to order:\")\n",
"\n",
"while len(customer_orders) < 3:\n",
" item = input(\"Enter a product name: \").lower()\n",
"\n",
" if item in products:\n",
" customer_orders.add(item)\n",
" print(f\"✅ Added '{item}' to customer orders.\")\n",
" else:\n",
" print(\"❌ Invalid product! Please choose from:\", products)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6. Print the products in the `customer_orders` set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 6. Printing the ordered products\n",
"print(\"Customer Orders:\", customer_orders)"
]
},
{
"cell_type": "markdown",
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# 7. Calculating order statistics\n",
"\n",
"# Total Products Ordered\n",
"total_products_ordered = len(customer_orders)\n",
"\n",
"# Percentage of Products Ordered\n",
"percentage_products_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"# Calculate Order Status\n",
"order_status = (total_products_ordered, percentage_products_ordered)\n",
"# Print Order Status\n",
"#print(\"Order Status (Total, Percentage):\", order_status)"
]
},
{
"cell_type": "markdown",
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# 8. Storing the statistics in a tuple order_status\n",
"print(\"Order Statistics:\")\n",
"print(\"Total Products Ordered:\", total_products_ordered)\n",
"print(f\"Percentage of Products Ordered: {int(percentage_products_ordered)}%\")"
]
},
{
"cell_type": "markdown",
"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": null,
"metadata": {},
"outputs": [],
"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",
" else:\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"10. Print the updated inventory, displaying the quantity of each product on separate lines."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 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 +321,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down