From 4537eb88d51f62570d49dda9aac64e9a8e6336a2 Mon Sep 17 00:00:00 2001 From: Claudia Palladino Date: Tue, 7 Oct 2025 17:24:46 +0100 Subject: [PATCH] lab1 solutions --- .vscode/settings.json | 4 + lab-python-data-structures.ipynb | 209 ++++++++++++++++++++++++++++++- 2 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..ba2a6c01 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python-envs.defaultEnvManager": "ms-python.python:system", + "python-envs.pythonProjects": [] +} \ No newline at end of file diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..13ef3fb7 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,216 @@ "\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": 25, + "metadata": {}, + "outputs": [], + "source": [ + "# 1.Define a LIST called `products` that contains the following items:\n", + "# \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Create an empty DICTIONARY called `inventory`\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "# 3. Ask the user to input the quantity of each product available in the inventory.\n", + "# Use the product names from the `products` list as keys in the `inventory` dictionary\n", + "# and assign the respective quantities as values.\n", + "\n", + "inventory[products[0]] = int(input(f\"Enter quantity for {products[0]}: \"))\n", + "inventory[products[1]] = int(input(f\"Enter quantity for {products[1]}: \"))\n", + "inventory[products[2]] = int(input(f\"Enter quantity for {products[2]}: \"))\n", + "inventory[products[3]] = int(input(f\"Enter quantity for {products[3]}: \"))\n", + "inventory[products[4]] = int(input(f\"Enter quantity for {products[4]}: \"))\n", + "\n", + "\n", + "\n", + "# Print the `inventory` dictionary to see the available products and their quantities.\n", + "print(\"Inventory: \", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create an empty SET called `customer_orders`\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Ask the user to input the name of three products that a customer wants to order\n", + "# (from those in the products list, \n", + "# meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\".\n", + "# Add each product name to the `customer_orders` set.\n", + "customer_orders.add(input(\"Enter the first product to order from those in the products list: \"))\n", + "customer_orders.add(input(\"Enter the second product to order from those in the products list: \"))\n", + "customer_orders.add(input(\"Enter the third product to order from those in the products list: \"))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in the customer orders: {'keychain', 'book', 't-shirt'}\n" + ] + } + ], + "source": [ + "#6. Print the products in the `customer_orders` set.\n", + "print (\"Products in the customer orders: \", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: {3}\n", + "Percentage of Products Ordered: {60.0} %\n" + ] + } + ], + "source": [ + "# 7. Calculate the following order statistics:\n", + "# - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + "tot_products_ordered = len(customer_orders)\n", + "print(f\"Total Products Ordered:\", {tot_products_ordered})\n", + " \n", + "# - Percentage of Products Ordered:\n", + "# The percentage of products ordered compared to the total available products.\n", + "percentage_products_ordered = tot_products_ordered / len(products) * 100\n", + "print(\"Percentage of Products Ordered:\", {percentage_products_ordered}, \"%\")\n", + "\n", + "# Store these statistics in a tuple called `order_status`\n", + "order_status = (tot_products_ordered, percentage_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0% \n", + " \n" + ] + } + ], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# ```\n", + "# Order Statistics:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % \n", + "# ```\n", + "print(f\"\"\"Order Statistics:\n", + "Total Products Ordered: {tot_products_ordered}\n", + "Percentage of Products Ordered: {percentage_products_ordered}% \n", + " \"\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity of each product.\n", + "# Modify the `inventory` dictionary accordingly.\n", + "inventory[products[0]] = inventory[products[0]] - 1\n", + "inventory[products[1]] = inventory[products[1]] - 1\n", + "inventory[products[2]] = inventory[products[2]] - 1\n", + "inventory[products[3]] = inventory[products[3]] - 1\n", + "inventory[products[4]] = inventory[products[4]] - 1\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Updated inventory:\n", + "t-shirt: 3 \n", + "mug: 4 \n", + "hat: 5 \n", + "book: 6 \n", + "keychain: 7 \n", + "\n" + ] + } + ], + "source": [ + "# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "print(f\"\"\" Updated inventory:\n", + "{products[0]}: {inventory[products[0]]} \n", + "{products[1]}: {inventory[products[1]]} \n", + "{products[2]}: {inventory[products[2]]} \n", + "{products[3]}: {inventory[products[3]]} \n", + "{products[4]}: {inventory[products[4]]} \n", + "\"\"\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +273,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,