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
189 changes: 187 additions & 2 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,196 @@
"\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": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 65, 'mug': 42, 'hat': 34, 'book': 20, 'keychain': 21}\n"
]
}
],
"source": [
"t_shirt=int(input(\"Please enter the numbers of t-shirts: \"))\n",
"mug=int(input(\"Please enter the number of mugs: \"))\n",
"hat=int(input(\"Please enter the number of hats: \"))\n",
"book=int(input(\"Please enter the number of books: \"))\n",
"keychain=int(input(\"Please enter the number of keychains: \"))\n",
"\n",
"inventory = {}\n",
"inventory[\"t-shirt\"]=t_shirt\n",
"inventory[\"mug\"]=mug\n",
"inventory[\"hat\"]=hat\n",
"inventory[\"book\"]=book\n",
"inventory[\"keychain\"]=keychain\n",
"\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'set'>\n"
]
}
],
"source": [
"customer_orders = set()\n",
"customers_orders = {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n",
"print(type(customer_orders))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product added to cart\n",
"Product added to cart\n",
"Product added to cart\n",
"{'mug', 't-shirt'}\n"
]
}
],
"source": [
"for i in range(3):\n",
" product = input(\"Enter the name of a product you want to order: \")\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" print(\"Product added to cart\")\n",
" else:\n",
" print(\"Product not in list\")\n",
"\n",
"print(customer_orders)\n",
"\n",
"# this is not working "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"0.00010989010989010989\n"
]
}
],
"source": [
"total_products_ordered = len(customer_orders)\n",
"print(total_products_ordered)\n",
"\n",
"percentage_ordered = len(customer_orders)/(sum(inventory.values())*100)\n",
"print(percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n"
]
}
],
"source": [
"order_status = (\"total_products_ordered\", \"percentage_ordered\")\n",
"print(type(order_status))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Pecentage of Products Ordered: 0.00010989010989010989%\n"
]
}
],
"source": [
"print(\"Order Statistics\"+\":\")\n",
"print(\"Total Products Ordered\"+\": \"+ str(total_products_ordered))\n",
"print(\"Pecentage of Products Ordered\"+\": \"+ str(percentage_ordered)+\"%\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 65, 'mug': 42, 'hat': 34, 'book': 20, 'keychain': 21}\n",
"t-shirt 64\n",
"mug 41\n",
"hat 33\n",
"book 19\n",
"keychain 20\n"
]
}
],
"source": [
"print(inventory)\n",
"for key in inventory:\n",
" inventory[key] -= 1\n",
"\n",
"for key, value in inventory.items():\n",
" print(key,value)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -68,7 +253,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down