From b42c6df7448c3d995cf9cf8e0884837b97f3b464 Mon Sep 17 00:00:00 2001 From: "Alexandra.Diaconita" Date: Sun, 23 Nov 2025 15:22:44 +0000 Subject: [PATCH] Lab1 solved --- lab-python-data-structures.ipynb | 204 ++++++++++++++++++++++++++++++- 1 file changed, 202 insertions(+), 2 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..b65a1f2e 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,211 @@ "\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": 38, + "metadata": {}, + "outputs": [], + "source": [ + "#1. list + specific items\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available 120 t-shirt!\n", + "Available 59 mug!\n", + "Available 99 hat!\n", + "Available 101 book!\n", + "Available 75 keychain!\n" + ] + } + ], + "source": [ + "#2. dictionary\n", + "inventory = {}\n", + "#3. quantity\n", + "for product in products:\n", + " quantity = input (f\"Could you, please, let us know what is the quantity available in stock for {product}?\")\n", + " while not quantity.isdigit():\n", + " quantity = input (f\"Wrong format. Please insert a valid quantity for {product}!\")\n", + " print (f\"Available {quantity} {product}!\")\n", + " inventory.update({product: int(quantity)})\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 120, 'mug': 59, 'hat': 99, 'book': 101, 'keychain': 75}\n" + ] + } + ], + "source": [ + "#3. Dictionary verified! \n", + "print (inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The products selected:\n", + "You chose: book.\n", + "You chose: mug.\n", + "You chose: hat.\n", + "Customer selection:\n", + "{'book', 'mug', 'hat'}\n", + "The quantities ordered:\n", + "You chose: 56 of book.\n", + "You chose: 23 of mug.\n", + "You chose: 16 of hat.\n", + "Quantity to be purchased:\n", + "{'book': 56, 'mug': 23, 'hat': 16}\n" + ] + } + ], + "source": [ + "#4.Empty set\n", + "print (\"The products selected:\")\n", + "customer_orders = set()\n", + "#5 products wanted\n", + "for i in range(3):\n", + " preference = input (f\"Could you, please, tell us a product you would like to buy?\")\n", + " while not (preference in products):\n", + " preference = input (f\"Product not available. Please insert a product in {products}!\")\n", + " print (f\"You chose: {preference}.\")\n", + " customer_orders.add(preference)\n", + "#6 Check the set\n", + "print(\"Customer selection:\")\n", + "print(customer_orders)\n", + "\n", + "#5.1 quantity ordered\n", + "print (\"The quantities ordered:\")\n", + "number_orders = {}\n", + "for purchased_product in customer_orders:\n", + " quantity_purchased = input(f\"How many of {purchased_product} would you like to purchase?\")\n", + " while (not quantity_purchased.isdigit()) or (int(quantity_purchased) > inventory[purchased_product]):\n", + " quantity_purchased = input (f\"Wrong format or quantity unavailable. The quantity is stock for {purchased_product} is {inventory[purchased_product]}. Please insert a number in {purchased_product}!\")\n", + " print (f\"You chose: {quantity_purchased} of {purchased_product}.\")\n", + " number_orders.update({purchased_product:int(quantity_purchased)})\n", + "#5.2 print quantity ordered\n", + "print (\"Quantity to be purchased:\")\n", + "print(number_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total of products ordered is 95.\n", + "(95, 20.92511013215859)\n" + ] + } + ], + "source": [ + "#7-8 Products ordered by the customer\n", + "total_products_ordered = sum(number_orders.values())\n", + "print (f\"The total of products ordered is {total_products_ordered}.\")\n", + "\n", + "#7-8 Percentage ordered\n", + "total_inventory = sum(inventory.values())\n", + "percentage_ordered = (total_products_ordered / total_inventory) *100\n", + "\n", + "\n", + "#7. Creating a tuple\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "print (order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: \n", + " Total Products Ordered: 95\n", + " Percentage of Products Ordered: 20.92511013215859 %.\n" + ] + } + ], + "source": [ + "\n", + "#8 Printing the order status:\n", + "\n", + "print(f\"Order Statistics: \\n Total Products Ordered: {order_status[0]}\\n Percentage of Products Ordered: {order_status[1]} %.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "#9 New inventory\n", + "for product, quantity in inventory.items():\n", + " inventory.update({product: quantity-1})" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The quantity of t-shirt is 119.\n", + "\n", + "The quantity of mug is 58.\n", + "\n", + "The quantity of hat is 98.\n", + "\n", + "The quantity of book is 100.\n", + "\n", + "The quantity of keychain is 74.\n", + "\n" + ] + } + ], + "source": [ + "#10 Printing the update inventory\n", + "for product, quantity in inventory.items():\n", + " print (f\"The quantity of {product} is {quantity}.\\n\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +268,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.0" } }, "nbformat": 4,