From 5864d12656d3d81b6e45a4418ccf68fef354b1c4 Mon Sep 17 00:00:00 2001 From: Eppu Henriksson Date: Fri, 25 Jul 2025 15:35:35 +0200 Subject: [PATCH 1/2] lab done --- lab-python-data-structures.ipynb | 249 ++++++++++++++++++++++++++++++- 1 file changed, 246 insertions(+), 3 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..e1322bba 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,256 @@ "\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": [], + "source": [ + "# 1 Define a list called products\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# 2 Create an empty dictionary called inventory\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Input the quantity of each product available in the inventory\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many t-shirts? 10\n", + "How many mugs? 10\n", + "How many hats? 10\n", + "How many books? 10\n", + "How many keychains? 10\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 3 Ask the user to input the quantity of each product available in the inventory\n", + "\n", + "print(\"Input the quantity of each product available in the inventory\")\n", + "\n", + "inventory['t-shirt'] = int(input(\"How many t-shirts?\"))\n", + "inventory['mug'] = int(input(\"How many mugs?\"))\n", + "inventory['hat'] = int(input(\"How many hats?\"))\n", + "inventory['book'] = int(input(\"How many books?\"))\n", + "inventory['keychain'] = int(input(\"How many keychains?\"))\n", + "\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "# 4 Create an empty set called customer_orders\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products would you like to order? Please choose 1-3: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "1st product: hat\n", + "2nd product: mug\n", + "3rd product: book\n" + ] + } + ], + "source": [ + "# 5 Ask the user to input the name of three products that a customer wants to order and add them to the customer_orders set\n", + "\n", + "choise = int(input(\"How many products would you like to order? Please choose 1-3: \"))\n", + "print(products)\n", + "\n", + "if choise == 3:\n", + " choise1 = input(\"1st product: \")\n", + " choise2 = input(\"2nd product: \")\n", + " choise3 = input(\"3rd product: \")\n", + " customer_orders.add(choise1)\n", + " customer_orders.add(choise2)\n", + " customer_orders.add(choise3)\n", + "elif choise == 2:\n", + " choise1 = input(\"1st product: \")\n", + " choise2 = input(\"2nd product: \")\n", + " customer_orders.add(choise1)\n", + " customer_orders.add(choise2)\n", + "else:\n", + " choise1 = input(\"1st product: \")\n", + " customer_orders.add(choise1)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# 6 Print the products in the customer_orders set\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# 7 Calculate order statistics\n", + "\n", + "products_ordered = choise\n", + "percentage_ordered = products_ordered / sum(inventory.values())\n", + "\n", + "order_status = (products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 6.0%\n" + ] + } + ], + "source": [ + "# 8 Print the order statistics\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {products_ordered}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered * 100}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "# 9 Update the inventory\n", + "\n", + "if choise == 3:\n", + " inventory[choise1] -= 1\n", + " inventory[choise2] -= 1\n", + " inventory[choise3] -= 1\n", + "elif choise == 2:\n", + " inventory[choise1] -= 1\n", + " inventory[choise2] -= 1\n", + "else:\n", + " inventory[choise1] -= 1\n", + "\n", + "# Alternative\n", + "# Cast the set to a list\n", + "# product1 = list(customer_orders)[0]\n", + "# product2 = list(customer_orders)[1] \n", + "# product3 = list(customer_orders)[2]\n", + "\n", + "# Also for loop would be a much more elegant solution" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirts left: 10\n", + "Mugs left: 9\n", + "Hat left: 9\n", + "Books left: 9\n", + "Keychains left: 10\n" + ] + } + ], + "source": [ + "# 10 Print the updated inventory\n", + "\n", + "print(f\"T-shirts left: {inventory['t-shirt']}\")\n", + "print(f\"Mugs left: {inventory['mug']}\")\n", + "print(f\"Hat left: {inventory['hat']}\")\n", + "print(f\"Books left: {inventory['book']}\")\n", + "print(f\"Keychains left: {inventory['keychain']}\")" + ] } ], "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 +311,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From c9fb1436ef6e1f91b6d817d3585a8bdebdf294f1 Mon Sep 17 00:00:00 2001 From: Eppu Henriksson Date: Fri, 25 Jul 2025 17:14:29 +0200 Subject: [PATCH 2/2] [FT-072025] Eppu Henriksson --- .DS_Store | Bin 0 -> 6148 bytes lab-python-data-structures-solution.ipynb | 159 ++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 .DS_Store create mode 100644 lab-python-data-structures-solution.ipynb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..46cc050947f8c4422ae2d16386667947ed2de4a4 GIT binary patch literal 6148 zcmeHKF-`+P474FdP@0sK`+`XPV2LgT1r2-vg_J?5=&#Cm_yRu*Gq#0@4jLpHG?why z^?Gh|Q=DTn^Y!cQ#%yV3b2!nC3{&GdePS1taUdMexVMe1?8SC>`$_fp339LO0lZrq zzVZ*x+x=mSQ~tC!`05mfl}rjq0VyB_q<|Foy#nmLu=yfUQ3^-_De$QPzYh&g?1f`u zd^!+f1OU#E4#PTT31DLa*bB!*WMH0DU{bwC3{N`pt?GK=n3#0)xEar>o4qC!kK2)N zQEuK76{Ua_I9K2@w@dc_4g81s|D2?q6p#Y{N&%k@kHZ?TRK0cba_qGY{sd>uJDi4f oP!OUW1EU>d!*+ZfMOoLl#(6Is6N8R?(1H3hKwV@~;I9?<0HMGdmjD0& literal 0 HcmV?d00001 diff --git a/lab-python-data-structures-solution.ipynb b/lab-python-data-structures-solution.ipynb new file mode 100644 index 00000000..410f0c6f --- /dev/null +++ b/lab-python-data-structures-solution.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders\n", + "\n", + "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "2. Create an empty dictionary called `inventory`.\n", + "\n", + "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", + "\n", + "4. Create an empty set called `customer_orders`.\n", + "\n", + "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", + "\n", + "6. Print the products in the `customer_orders` set.\n", + "\n", + "7. Calculate the following order statistics:\n", + " - 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", + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```\n", + "\n", + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\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": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer orders: {'book', 'mug', 'hat'}\n", + "Order Statistics: \n", + "Total products ordered: 3\n", + "Percentage of Unique products ordered : 60.0\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 5}" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#1.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "#2.\n", + "inventory = {}\n", + "\n", + "#3.\n", + "inventory[\"t-shirt\"] = int(input(\"Select the quantity available for t-shirt\"))\n", + "inventory[\"mug\"] = int(input(\"Select the quantity available for mug\"))\n", + "inventory[\"hat\"] = int(input(\"Select the quantity available for hat\"))\n", + "inventory[\"book\"] = int(input(\"Select the quantity available for book\"))\n", + "inventory[\"keychain\"] = int(input(\"Select the quantity available for keychain\"))\n", + "\n", + "customer_orders = set()\n", + "\n", + "product_name = input(\"Enter the name of a product that a customer wants: \")\n", + "customer_orders.add(product_name)\n", + "\n", + "product_name = input(\"Enter the name of a product that a customer wants: \")\n", + "customer_orders.add(product_name)\n", + "\n", + "product_name = input(\"Enter the name of a product that a customer wants: \")\n", + "customer_orders.add(product_name)\n", + "\n", + "#6.\n", + "print(\"Products in customer orders: \", customer_orders)\n", + "\n", + "#7.\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered/len(products))* 100\n", + "\n", + "order_stats = (total_products_ordered, percentage_ordered)\n", + "\n", + "#8.\n", + "\n", + "print(\"Order Statistics: \")\n", + "print(\"Total products ordered: \", order_stats[0])\n", + "print(\"Percentage of Unique products ordered : \", order_stats[1])\n", + "\n", + "\n", + "#9./10. \n", + "product_one = list(customer_orders)[0]\n", + "inventory[product_one] -= 1\n", + "\n", + "\n", + "product_two = list(customer_orders)[1]\n", + "inventory[product_two] -= 1\n", + "\n", + "\n", + "product_three = list(customer_orders)[2]\n", + "inventory[product_three] -= 1\n", + "\n", + "inventory\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}