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
225 changes: 224 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,229 @@
"\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": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1. \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"products"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"# 2.\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Number of t-shirt: 12\n",
"Number of mug: 22\n",
"Number of hat: 23\n",
"Number of book: 44\n",
"Number of keychain: 55\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 12, 'mug': 22, 'hat': 23, 'book': 44, 'keychain': 55}\n"
]
}
],
"source": [
"# 3.\n",
"for product in products:\n",
" value = int(input(f\"Number of {product}:\"))\n",
" inventory[product] = value\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"# 4.\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a product from the list: mug\n",
"Choose a product from the list: Mug\n",
"Choose a product from the list: mug\n",
"Choose a product from the list: banana\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"This product is not available\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Choose a product from the list: Book\n",
"Choose a product from the list: hat\n"
]
}
],
"source": [
"# 5.\n",
"\n",
"while len(customer_orders) < 3:\n",
" user_products = input(\"Choose a product from the list:\")\n",
" user_products = user_products.lower()\n",
" if user_products in products:\n",
" customer_orders.add(user_products)\n",
" else:\n",
" print(f\"This product is not available\")"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'mug', 'hat'}\n"
]
}
],
"source": [
"# 6.\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 7.\n",
"total = (len(customer_orders))\n",
"percentage = len(customer_orders)/len(products)*100\n",
"order_status = (total, percentage)\n",
"type(order_status)\n",
"order_status"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total products ordered: 3\n",
"Percentage of products ordered: 60.0%\n"
]
}
],
"source": [
"# 8.\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total products ordered: {order_status[0]}\")\n",
"print(f\"Percentage of products ordered: {order_status[1]}%\")"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"# 9.\n",
"for product in customer_orders:\n",
" actual_quantity_product = inventory[product]\n",
" new_quantity = actual_quantity_product -1\n",
" inventory[product] = new_quantity"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 12, 'mug': 20, 'hat': 21, 'book': 42, 'keychain': 55}"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 10.\n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -68,7 +291,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down