Skip to content

Solved Lab- Data Structures #523

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 3 commits 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
90 changes: 88 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,97 @@
"\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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n",
"New orders are as follows: {'hat', 'book', 'mug'}\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n",
"Updated Inventory:\n",
"Amount of t-shirts in the inventory: 5\n",
"Amount of mugs in the inventory: 4\n",
"Amount of hats in the inventory: 4\n",
"Amount of books in the inventory: 4\n",
"Amount of keychains in the inventory: 5\n"
]
}
],
"source": [
"# Define a list\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# Create empty dictionary\n",
"inventory = {}\n",
"# Ask for input for each product\n",
"inventory[\"t-shirt\"] = int(input(\"Enter the quantity of the t-shirt: \"))\n",
"inventory[\"mug\"] = int(input(\"Enter the quantity of the mug: \"))\n",
"inventory[\"hat\"] = int(input(\"Enter the quantity of the hat: \"))\n",
"inventory[\"book\"] = int(input(\"Enter the quantity of the book: \"))\n",
"inventory[\"keychain\"] = int(input(\"Enter the quantity of the keychain: \"))\n",
"\n",
"print(inventory)\n",
"# Empty set \n",
"customer_orders = set()\n",
"\n",
"# Three products orders from the customer\n",
"order_1 = input(\"Enter product 1: \").strip().lower()\n",
"order_2 = input(\"Enter product 2: \").strip().lower()\n",
"order_3 = input(\"Enter product 3: \").strip().lower()\n",
"\n",
"# validation\n",
"if order_1 in products: \n",
" customer_orders.add(order_1)\n",
"if order_2 in products:\n",
" customer_orders.add(order_2)\n",
"if order_3 in products:\n",
" customer_orders.add(order_3)\n",
"\n",
"# Print orders\n",
"print(f\"New orders are as follows: {customer_orders}\")\n",
"\n",
"# Calculate order statistics\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"order_status= (total_products_ordered, percentage_ordered)\n",
"\n",
"# Print statistics\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]} %\")\n",
"\n",
"#Update inventory\n",
"if \"t-shirt\" in customer_orders and inventory[\"t-shirt\"] > 0:\n",
" inventory[\"t-shirt\"] -= 1\n",
"if \"mug\" in customer_orders and inventory[\"mug\"] > 0:\n",
" inventory[\"mug\"] -= 1\n",
"if \"hat\" in customer_orders and inventory[\"hat\"] > 0:\n",
" inventory[\"hat\"] -= 1\n",
"if \"book\" in customer_orders and inventory[\"book\"] > 0:\n",
" inventory[\"book\"] -= 1\n",
"if \"keychain\" in customer_orders and inventory[\"keychain\"] > 0:\n",
" inventory[\"keychain\"] -= 1\n",
"\n",
"# Print inventory\n",
"print(\"Updated Inventory:\")\n",
"print(f\"Amount of t-shirts in the inventory: {inventory['t-shirt']}\")\n",
"print(f\"Amount of mugs in the inventory: {inventory['mug']}\")\n",
"print(f\"Amount of hats in the inventory: {inventory['hat']}\")\n",
"print(f\"Amount of books in the inventory: {inventory['book']}\")\n",
"print(f\"Amount of keychains in the inventory: {inventory['keychain']}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +154,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down