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
283 changes: 280 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,290 @@
"\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": 4,
"metadata": {},
"outputs": [],
"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\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# 2. Create an empty dictionary called inventory.\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of t-shirt available in the inventory 45\n",
"Enter the number of mug available in the inventory 56\n",
"Enter the number of hat available in the inventory 10\n",
"Enter the number of book available in the inventory 12\n",
"Enter the number of keychain available in the inventory 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory dictionary is: {'t-shirt': 45, 'mug': 56, 'hat': 10, 'book': 12, 'keychain': 5}\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 \n",
"# dictionary and assign the respective quantities as values.\n",
"for product in products:\n",
" qty = int(input(f\"Enter the number of '{product}' available in the inventory\"))\n",
" inventory[product] = qty\n",
"print(\"Inventory dictionary is:\", inventory)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# 4. Create an empty set called customer_orders.\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter three (3) products from the following product list: ['t-shirt', 'mug', 'hat', 'book', 'keychain']. Separate the items by a space: mug hat book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"['mug', 'hat', 'book']\n",
"Customer orders are: {'hat', 'book', 'mug'}\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",
"customer_input = input(f\"Enter three (3) products from the following product list: {products}. Separate the items by a space:\").split()\n",
"print(customer_input) # chet the putput of the input\n",
"#check whether customer 's lis is valid, otherwise keep as king to enter a valid list\n",
"\n",
"while not set(customer_input).issubset(set(products)) or len(customer_input) !=3:\n",
" print (\"Your list of products is not valid, please try again!\")\n",
" customer_input = input(f\"Enter three (3) products from the following product list: {products}. Separate the items by a space:\").split()\n",
"\n",
"for item in customer_input:\n",
" customer_orders.add(item)\n",
"print(f\"Customer orders are: {customer_orders}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The prodects in the customer's set: \n",
"Customer orders are: {'hat', 'book', 'mug'}\n",
"hat\n",
"book\n",
"mug\n"
]
}
],
"source": [
"#6. Print the products in the `customer_orders` set\n",
"print(\"The prodects in the customer's set: \")\n",
"print(f\"Customer orders are: {customer_orders}\")\n",
"for i in customer_orders:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the desired quantity of product hat: 2\n",
"Please enter the desired quantity of product book: 3\n",
"Please enter the desired quantity of product mug: 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The products order is: {'hat': 2, 'book': 3, 'mug': 1}\n",
"Number of products ordered: 3\n"
]
}
],
"source": [
"# 7.1. Total Products Ordered: The total number of products in the `customer_orders` set.\n",
"\n",
"#Ask the client to enter the wanted quatity of every product & store it in a dictionaary\n",
"cust_prod_qty = {}\n",
"for element in customer_orders:\n",
" prod_qty = int(input(f\"Please enter the desired quantity of product '{element}': \"))\n",
" cust_prod_qty[element] = prod_qty\n",
"print(f\"The products order is: {cust_prod_qty}\")"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory dictionary is: {'t-shirt': 45, 'mug': 56, 'hat': 10, 'book': 12, 'keychain': 5}\n",
"The products order as a dict is: {'hat': 2, 'book': 3, 'mug': 1}\n",
"The products order as a set is: {'hat', 'book', 'mug'}\n",
"The percentage of 'hat' ordered is: 20.0%\n",
"The percentage of 'book' ordered is: 25.0%\n",
"The percentage of 'mug' ordered is: 1.79%\n",
"(20.0, 25.0, 1.79)\n"
]
}
],
"source": [
"# 7.2. Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n",
"print(\"Inventory dictionary is:\", inventory)\n",
"print(f\"The products order as a dict is: {cust_prod_qty}\") #Products ordered as a dictionary\n",
"print(f\"The products order as a set is: {customer_orders}\") #Products ordered as a set\n",
"order_status_list = [] # created to convert it to a tuple in the coming question\n",
"total_ordered_items = 0 #initiate the variable total of product ordrered (for the next question)\n",
"\n",
"for j in cust_prod_qty.keys():\n",
" prod_perc = round(cust_prod_qty[j]/inventory[j]*100, 2)\n",
" total_ordered_items += cust_prod_qty[j]\n",
" print(f\"The percentage of '{j}' ordered is: {prod_perc}%\")\n",
" order_status_list.append(prod_perc)\n",
" \n",
"#Store these statistics in a tuple called `order_status`.\n",
"order_status = tuple(order_status_list)\n",
"print(order_status)\n"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 6\n",
"Percentage of Products Ordered:4.69% (6/128)\n"
]
}
],
"source": [
"# 8. Order Statistics:\n",
" # Total Products Ordered: <total_products_ordered>\n",
" # Percentage of Products Ordered: <percentage_ordered>%\n",
"\n",
"total_inventory_items = 0 # initiate the total inventory variable\n",
"for m in inventory:\n",
" total_inventory_items += inventory[m]\n",
"\n",
"\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {total_ordered_items}\")\n",
"print(f\"Percentage of Products Ordered:\\\n",
"{round(total_ordered_items/total_inventory_items*100, 2)}% ({total_ordered_items}/{total_inventory_items})\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The updated status of inventory is: {'t-shirt': 45, 'mug': 53, 'hat': 4, 'book': 3, 'keychain': 5}\n"
]
}
],
"source": [
"# 9. Update the inventory by subtracting 1 from the quantity of each product. \n",
"# Modify the `inventory` dictionary accordingly.\n",
"for i in cust_prod_qty:\n",
" inventory[i] -= cust_prod_qty[i]\n",
"print(f\"The updated status of inventory is: {inventory}\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt : 45\n",
"mug : 53\n",
"hat : 4\n",
"book : 3\n",
"keychain : 5\n"
]
}
],
"source": [
"# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n",
"for key, value in inventory.items():\n",
" print(key,\": \",value)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +345,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down