Skip to content

Solved lab #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
205 changes: 204 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,209 @@
"\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": 142,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [],
"source": [
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Insert the quantity of product: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 200\n",
"mug: 250\n",
"hat: 400\n",
"book: 80\n",
"keychain: 380\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",
"\n",
"\n",
"print(\"Insert the quantity of product: \")\n",
"for product in products:\n",
" quantity = int(input(f\"{product}: \"))\n",
" inventory[product] = quantity "
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Insert the name of three products to order: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product 1: mug\n",
"Quantity of mug: 50\n",
"Product 2: hat\n",
"Quantity of hat: 60\n",
"Product 3: keychain\n",
"Quantity of keychain: 100\n"
]
}
],
"source": [
"#5. Ask the user to input the name of three products that a customer wants to order (from those in the products list,\n",
"# meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n",
"\n",
"print(\"Insert the name of three products to order: \")\n",
"for i in range(3):\n",
" order = input(f\"Product {i+1}: \").strip()\n",
" if order in products:\n",
" quantity_order = int(input(f\"Quantity of {order}: \"))\n",
" customer_orders.add((order,quantity_order))\n",
" else:\n",
" print(\"Invalid product.\")"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{('hat', 60), ('mug', 50), ('keychain', 100)}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 205,
"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",
"sum_order_prod = sum(qty for _, qty in customer_orders)\n",
"\n",
"sum_invent_prod = sum(inventory.values())\n",
"\n",
"percentage = (sum_order_prod / sum_invent_prod) * 100\n",
"\n",
"order_status = (sum_order_prod, percentage)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order statistics: \n",
"Total of products ordered: 210\n",
"Percentage of products ordered: 16.03%\n"
]
}
],
"source": [
"print(\"Order statistics: \")\n",
"print(f\"Total of products ordered: {sum_order_prod}\")\n",
"print(f\"Percentage of products ordered: {percentage:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 214,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 200, 'mug': 200, 'hat': 340, 'book': 80, 'keychain': 280}\n"
]
}
],
"source": [
"#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n",
"for product, quantity_order in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= quantity_order\n",
" if inventory[product] < 0:\n",
" inventory[product] = 0\n",
"\n",
"print(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +271,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down