From a056448a98d6e6617928a3a1949ec94b938796af Mon Sep 17 00:00:00 2001 From: Dimitrios Gkoutzouvelidis Date: Wed, 3 Sep 2025 17:53:40 +0200 Subject: [PATCH] Solved lab --- lab-python-data-structures.ipynb | 702 ++++++++++++++++++++++++++++++- 1 file changed, 698 insertions(+), 4 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..317672f5 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -35,7 +35,7 @@ " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", " \n", - " Store these statistics in a tuple called `order_status`.\n", + " \n", "\n", "8. Print the order statistics using the following format:\n", " ```\n", @@ -50,13 +50,707 @@ "\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": [ + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# 1 \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "print(\"Available products:\", products)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Initial inventory: {}\n" + ] + } + ], + "source": [ + "#2\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "print(\"Initial inventory:\", inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 4\n", + "mug: 5\n", + "hat: 6\n", + "book: 7\n", + "keychain: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Current Inventory: {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 5}\n" + ] + } + ], + "source": [ + "#3 \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(\"\\nCurrent Inventory:\", inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 4\n", + "mug: 5\n", + "hat: 6\n", + "book: 7\n", + "keychain: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Current Inventory: {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n", + "Customer Orders (empty set): set()\n" + ] + } + ], + "source": [ + "#4\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "print(\"\\nCurrent Inventory:\", inventory)\n", + "print(\"Customer Orders (empty set):\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 5\n", + "mug: 6\n", + "hat: 7\n", + "book: 8\n", + "keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please choose 3 products to order from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: mug\n", + "Enter product 2: book\n", + "Enter product 3: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: {'book', 'mug', 'keychain'}\n" + ] + } + ], + "source": [ + "#5\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "print(\"\\nPlease choose 3 products to order from:\", products)\n", + "for i in range(3):\n", + " product_choice = input(f\"Enter product {i+1}: \").lower()\n", + " customer_orders.add(product_choice)\n", + "\n", + "print(\"\\nCustomer Orders:\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 5\n", + "mug: 5\n", + "hat: 5\n", + "book: 5\n", + "keychain: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please choose 3 products to order from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: book\n", + "Enter product 2: book\n", + "Enter product 3: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products ordered by the customer:\n", + "hat\n", + "book\n" + ] + } + ], + "source": [ + "#6\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "print(\"\\nPlease choose 3 products to order from:\", products)\n", + "for i in range(3):\n", + " product_choice = input(f\"Enter product {i+1}: \").lower()\n", + " customer_orders.add(product_choice)\n", + "\n", + "print(\"\\nProducts ordered by the customer:\")\n", + "for order in customer_orders:\n", + " print(order)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 5\n", + "mug: 5\n", + "hat: 5\n", + "book: 5\n", + "keychain: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please choose 3 products to order from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: mug\n", + "Enter quantity for mug: 3\n", + "Enter product 2: hat\n", + "Enter quantity for hat: 1\n", + "Enter product 3: book\n", + "Enter quantity for book: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products ordered by the customer:\n", + "mug: 3\n", + "hat: 1\n", + "book: 4\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 8\n", + "Percentage of Products Ordered: 32.00%\n" + ] + } + ], + "source": [ + "#7\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = {}\n", + "print(\"\\nPlease choose 3 products to order from:\", products)\n", + "for i in range(3):\n", + " product_choice = input(f\"Enter product {i+1}: \").lower()\n", + " qty = int(input(f\"Enter quantity for {product_choice}: \"))\n", + " customer_orders[product_choice] = qty\n", + "\n", + "print(\"\\nProducts ordered by the customer:\")\n", + "for order, qty in customer_orders.items():\n", + " print(f\"{order}: {qty}\")\n", + "\n", + "total_products_ordered = sum(customer_orders.values()) \n", + "total_products_available = sum(inventory.values()) \n", + "percentage_ordered = (total_products_ordered / total_products_available) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 4\n", + "mug: 4\n", + "hat: 4\n", + "book: 4\n", + "keychain: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please choose 3 products to order from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: hat\n", + "Enter quantity for hat: 1\n", + "Enter product 2: book\n", + "Enter quantity for book: 1\n", + "Enter product 3: mug\n", + "Enter quantity for mug: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products ordered by the customer:\n", + "hat: 1\n", + "book: 1\n", + "mug: 1\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 15.00%\n" + ] + } + ], + "source": [ + "#8\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = {}\n", + "print(\"\\nPlease choose 3 products to order from:\", products)\n", + "for i in range(3):\n", + " product_choice = input(f\"Enter product {i+1}: \").lower()\n", + " qty = int(input(f\"Enter quantity for {product_choice}: \"))\n", + " customer_orders[product_choice] = qty\n", + "\n", + "print(\"\\nProducts ordered by the customer:\")\n", + "for order, qty in customer_orders.items():\n", + " print(f\"{order}: {qty}\")\n", + "\n", + "total_products_ordered = sum(customer_orders.values()) \n", + "total_products_available = sum(inventory.values()) \n", + "percentage_ordered = (total_products_ordered / total_products_available) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 10\n", + "mug: 10\n", + "hat: 10\n", + "book: 10\n", + "keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please choose 3 products to order from: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: hat\n", + "Enter quantity for hat: 2\n", + "Enter product 2: book\n", + "Enter quantity for book: 4\n", + "Enter product 3: mug\n", + "Enter quantity for mug: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products ordered by the customer:\n", + "hat: 2\n", + "book: 4\n", + "mug: 1\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 7\n", + "Percentage of Products Ordered: 14.00%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 9\n", + "hat: 8\n", + "book: 6\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "#9\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = {}\n", + "print(\"\\nPlease choose 3 products to order from:\", products)\n", + "for i in range(3):\n", + " product_choice = input(f\"Enter product {i+1}: \").lower()\n", + " qty = int(input(f\"Enter quantity for {product_choice}: \"))\n", + " customer_orders[product_choice] = qty\n", + "\n", + "print(\"\\nProducts ordered by the customer:\")\n", + "for order, qty in customer_orders.items():\n", + " print(f\"{order}: {qty}\")\n", + "\n", + "total_products_ordered = sum(customer_orders.values()) \n", + "total_products_available = sum(inventory.values()) \n", + "percentage_ordered = (total_products_ordered / total_products_available) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "for product, qty in customer_orders.items():\n", + " if product in inventory:\n", + " inventory[product] -= qty \n", + "\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the quantity available for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 10\n", + "mug: 10\n", + "hat: 10\n", + "book: 10\n", + "keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Available Products:\n", + "t-shirt\n", + "mug\n", + "hat\n", + "book\n", + "keychain\n", + "\n", + "Enter customer orders:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: mug\n", + "Quantity for mug: 3\n", + "Enter product 2: hat\n", + "Quantity for hat: 1\n", + "Enter product 3: book\n", + "Quantity for book: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 7\n", + "Percentage of Products Ordered: 14.00%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 7\n", + "hat: 9\n", + "book: 7\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "#10\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "print(\"Enter the quantity available for each product:\")\n", + "for product in products:\n", + " quantity = int(input(f\"{product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(\"\\nAvailable Products:\")\n", + "for product in inventory:\n", + " print(product)\n", + "\n", + "customer_orders = {}\n", + "print(\"\\nEnter customer orders:\")\n", + "\n", + "for i in range(3): \n", + " product_choice = input(f\"Enter product {i+1}: \").lower()\n", + " qty = int(input(f\"Quantity for {product_choice}: \"))\n", + " customer_orders[product_choice] = qty\n", + "\n", + "total_products_ordered = sum(customer_orders.values()) \n", + "total_products_available = sum(inventory.values()) \n", + "percentage_ordered = (total_products_ordered / total_products_available) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "for product, qty in customer_orders.items():\n", + " inventory[product] -= qty\n", + "\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -68,7 +762,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,