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
174 changes: 174 additions & 0 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,180 @@
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "012dc04c",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"# Exercise #1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d52baf3",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = ()\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"id": "ede18899",
"metadata": {},
"source": [
"# Exercise 2:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81f1d233",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" product = input(\"Enter a product name (or 'done' to finish): \")\n",
" if product == \"done\":\n",
" break\n",
" customer_orders.add(product)\n",
"\n",
" return customer_orders\n"
]
},
{
"cell_type": "markdown",
"id": "15993ea1",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"source": [
"# Exercise 3:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e38eac0",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "markdown",
"id": "5d25fd22",
"metadata": {},
"source": [
"# Exercise 4:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d71d9ca",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_products = (total_products_ordered / len(products)) * 100\n",
"\n",
" return total_products_ordered, percentage_unique_products"
]
},
{
"cell_type": "markdown",
"id": "ba6a49e2",
"metadata": {},
"source": [
"# Exercise 6:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68be2fab",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"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": "markdown",
"id": "2019a1d4",
"metadata": {},
"source": [
"# Exercise 7:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fdf45e98",
"metadata": {
"vscode": {
"languageId": "javascript"
}
},
"outputs": [],
"source": [
"products = [\"apple\", \"banana\", \"orange\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
Expand Down