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
340 changes: 339 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,344 @@
"\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": 6,
"metadata": {},
"outputs": [],
"source": [
"# 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": 7,
"metadata": {},
"outputs": [],
"source": [
"# Create an empty dictionary called inventory.\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of t-shirt 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of t-shirt is 1\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of mug 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of mug is 2\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of hat 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of hat is 3\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of book 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of book is 4\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the number of keychain 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number of keychain is 5\n"
]
}
],
"source": [
"# 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",
"for product_type in products:\n",
" inventory[product_type] = input(f\"Please enter the number of {product_type}\")\n",
" print(f\"The number of {product_type} is {inventory[product_type]}\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# Create an empty set called customer_orders\n",
"customer_orders = set({})"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product ordered: mag\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter one of the following product types, \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product ordered: mog\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter one of the following product types, \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product ordered: mug\n",
"Please enter the product ordered: t-shirt\n",
"Please enter the product ordered: boak\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter one of the following product types, \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the product ordered: book\n"
]
}
],
"source": [
"# 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",
"count = 0\n",
"while count < 3:\n",
" customer_order_type = input('Please enter the product ordered:')\n",
" if customer_order_type in products:\n",
" customer_orders.add(customer_order_type)\n",
" count += 1\n",
" else:\n",
" print('Please enter one of the following product types, \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\"')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 't-shirt', 'mug'}\n"
]
}
],
"source": [
"# Print the products in the customer_orders set.\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 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",
"\n",
"len(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"order_status = ((len(customer_orders)/len(products))*100)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60.0\n"
]
}
],
"source": [
"print(order_status)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"3\n",
"60.0%\n"
]
}
],
"source": [
"# Print the order statistics using the following format:\n",
" # Order Statistics:\n",
" # Total Products Ordered: <total_products_ordered>\n",
" # Percentage of Products Ordered: <percentage_ordered>% \n",
"\n",
"print('Order Statistics:')\n",
"print(len(customer_orders))\n",
"print(f'{order_status}%')"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"# Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly.\n",
"for inventory_keys in inventory.keys():\n",
" x = int(inventory[inventory_keys])\n",
" x -= 1\n",
" inventory[inventory_keys] = str(x)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': '0', 'mug': '1', 'hat': '2', 'book': '3', 'keychain': '4'}\n"
]
}
],
"source": [
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('t-shirt', '0')\n",
"('mug', '1')\n",
"('hat', '2')\n",
"('book', '3')\n",
"('keychain', '4')\n"
]
}
],
"source": [
"for inventory_item in inventory.items():\n",
" print(inventory_item)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" "
]
}
],
"metadata": {
Expand All @@ -68,7 +406,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down