Skip to content
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fd6af70
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
ea094f1
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
9eae079
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
b53e6ab
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
7317bb5
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
c1e2dfa
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
4e6d22b
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
4ad0349
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 13, 2026
753b5e2
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
d28ea5e
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
29f21ee
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
7f93b86
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
dfb0252
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
fa72656
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
83c9f33
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
993dca5
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
f91aab1
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 14, 2026
6bc88b7
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
92716de
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
0fb6a22
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
4495c72
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
0bb4e0d
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
6e237cc
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
4e24d44
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
5e62dda
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
1bfd6d1
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
868efc6
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 15, 2026
20afbe1
Update lab-python-data-structures.ipynb
nailaikhenazenni-afk Apr 16, 2026
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
265 changes: 263 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,272 @@
"\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": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a valid integer.\n",
" is not a valid choice and will not be added.\n"
]
}
],
"source": [
"products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"inventory = { }\n",
"for product in products_list:\n",
" while True:\n",
" quantity_input = input(f\"Enter the quantity for {product}: \") or '0'\n",
" try:\n",
" quantity = int(quantity_input)\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Please enter a valid integer.\")\n",
"\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" product = input(\"Enter a product (choose from 't-shirt', 'mug', 'hat', 'book', 'keychain'): \").strip().lower()\n",
" \n",
" if product in products_list:\n",
" customer_orders.add(product)\n",
" print(f\"{product} has been added to the order.\")\n",
" else:\n",
" print(f\"{product} is not a valid choice and will not be added.\")\n",
" add_more = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" \n",
" if add_more != 'yes':\n",
" break\n",
"\n",
"print(\"products in order:\", customer_orders)\n",
"\n",
"total_ordered = len(customer_orders)\n",
"total_available = len(products_list)\n",
"percentage = (total_ordered / total_available) * 100\n",
"\n",
"order_status = (total_ordered, percentage)\n",
"\n",
"print(\"\\norder statistics:\")\n",
"print(f\"total products ordered: {order_status[0]}\")\n",
"print(f\"percentage of products ordered: {order_status[1]:.2f}%\")\n",
"\n",
"for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"Successfully ordered {product}. Remaining: {inventory[product]}\")\n",
" \n",
"\n",
"print(\"\\nupdated inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" \n",
" for product in products:\n",
"\n",
" while True:\n",
" quantity_input = input(f\"Enter the quantity for {product}: \") \n",
" try:\n",
" quantity = int(quantity_input)\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Please enter a valid integer.\") \n",
" \n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" orders = set() # Local variable to collect customer orders\n",
" valid_products = {'t-shirt', 'mug', 'hat', 'book', 'keychain'}\n",
"\n",
" while True:\n",
" # Prompt the user for input\n",
" order = input(\"Enter a valid product name to add to your order: \").strip().lower()\n",
" \n",
" if order in valid_products:\n",
" orders.add(order) # Add order to set\n",
" print(f\"'{order}' has been added to your order.\")\n",
" else:\n",
" print(f\"Error: '{order}' is not a valid product.\")\n",
"\n",
" # After every attempt, ask if they want to continue adding\n",
" choice = \"\"\n",
" while choice not in {\"yes\", \"no\"}:\n",
" choice = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if choice not in {\"yes\", \"no\"}:\n",
" print(\"Please enter 'yes' or 'no'.\")\n",
"\n",
" if choice == 'no':\n",
" break\n",
" return orders\n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Iterates through customer orders and reduces inventory counts for valid items.\n",
" \"\"\"\n",
" for product in customer_orders:\n",
" # Check if product is a key in the inventory and has stock available\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" # Handle cases where the item is out of stock or misspelled\n",
" print(f\"Error: '{product}' is not available or doesn't exist in inventory.\")\n",
" \n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_available_products = len(products)\n",
"\n",
" if total_products_ordered == 0:\n",
" return 0, 0.0\n",
"\n",
" percentage_unique_products_ordered = (total_products_ordered / total_available_products) * 100\n",
"\n",
" order_statistics = (total_products_ordered, percentage_unique_products_ordered)\n",
"\n",
" return order_statistics\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"\\norder statistics:\")\n",
" print(f\"total products ordered: {order_statistics[0]}\")\n",
" print(f\"percentage of unique products ordered: {order_statistics[1]:.2f}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"updated inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"def execute_the_program():\n",
" \n",
" products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
" inventory = initialize_inventory(products_list)\n",
" customer_orders = get_customer_orders()\n",
" updated_inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics= calculate_order_statistics(customer_orders, products_list)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(updated_inventory)\n",
" return\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def manage_customer_orders():\n",
"\n",
" products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
" inventory = initialize_inventory(products_list)\n",
" customer_orders = get_customer_orders()\n",
" updated_inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics= calculate_order_statistics(customer_orders, products_list)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(updated_inventory)\n",
" return\n",
"\n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +329,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down