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
220 changes: 219 additions & 1 deletion lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,224 @@
"\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": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# 1. List Products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n"
]
}
],
"source": [
"inventory = {}\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter number 1: 22\n",
"Enter number 2: 12\n",
"Enter number 3: 27\n",
"Enter number 4: 56\n",
"Enter number 5: 89\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"['22', '12', '27', '56', '89']\n",
"{'t-shirt': 22, 'mug': 12, 'hat': 27, 'book': 56, 'keychain': 89}\n"
]
}
],
"source": [
"values = []\n",
"for i in range(0,5):\n",
" user_num = input(f\"Enter number {i+1}: \")\n",
" if user_num.isdigit():\n",
" values.append(user_num)\n",
" else:\n",
" print(\"Enter the valid value\")\n",
" user_num = input(f\"Enter number {i+1}: \")\n",
" values.append(user_num)\n",
"print(values)\n",
"\n",
"for i in range(0,5):\n",
" inventory[products[i]] = int(values[i])\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the product from t-shirt, mug, hat, book, keychain that you want to order t-shirt\n",
"Enter the product from t-shirt, mug, hat, book, keychain that you want to order hat\n",
"Enter the product from t-shirt, mug, hat, book, keychain that you want to order book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 't-shirt', 'hat'}\n"
]
}
],
"source": [
"customer_orders = set()\n",
"\n",
"for i in range(0,3):\n",
" customer_orders.add(input(\"Enter the product from t-shirt, mug, hat, book, keychain that you want to order\").lower())\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total number of products ordered are: 3\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many books would you like to order? 4\n",
"How many t-shirts would you like to order? 1\n",
"How many hats would you like to order? 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book': 4, 't-shirt': 1, 'hat': 3}\n",
"Total products ordered: 8\n",
"Total products in the inventory: 206\n",
"Percentage of Products Ordered is : 3.88\n"
]
}
],
"source": [
"ordered_products = len(customer_orders)\n",
"print(\"The total number of products ordered are: \",ordered_products)\n",
"ordered_dict = {}\n",
"for product in customer_orders:\n",
" quantity = input(f\"How many {product}s would you like to order? \")\n",
" if quantity.isdigit():\n",
" ordered_dict[product] = int(quantity)\n",
" else:\n",
" #print(\"invalid input\")\n",
" quantity = input(f\"Enter valid number {product}s would you like to order? \")\n",
" ordered_dict[product] = int(quantity)\n",
"print(ordered_dict)\n",
"print(\"Total products ordered: \",sum(ordered_dict.values()))\n",
"print(\"Total products in the inventory: \",sum(inventory.values()))\n",
"percentage_ordered = (sum(ordered_dict.values()) / sum(inventory.values())) * 100\n",
"print(f\"Percentage of Products Ordered is : {percentage_ordered:.2f}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8, 3.8834951456310676]\n",
"Products ordered: 8\n",
"Percentage of Products Ordered is : 3.88\n"
]
}
],
"source": [
"ordered_products = sum(ordered_dict.values())\n",
"\n",
"order_status = [ordered_products, percentage_ordered]\n",
"print(order_status)\n",
"print(f\"Products ordered: {ordered_products}\")\n",
"print(f\"Percentage of Products Ordered is : {percentage_ordered:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"for product in inventory:\n",
" inventory[product] -= 1\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 21\n",
"mug: 11\n",
"hat: 26\n",
"book: 55\n",
"keychain: 88\n"
]
}
],
"source": [
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
}
],
"metadata": {
Expand All @@ -68,7 +286,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down