From b337facf99dc9b99e6dda7926849dfa92e8f9b0b Mon Sep 17 00:00:00 2001 From: Katarzyna Grzyb Date: Mon, 15 Jun 2026 18:07:53 +0200 Subject: [PATCH] updated lab1 --- .DS_Store | Bin 0 -> 6148 bytes ...ab-python-data-structures-checkpoint.ipynb | 392 ++++++++++++++++++ lab-python-data-structures.ipynb | 318 +++++++++++++- 3 files changed, 709 insertions(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..728ede93fc62f4dcf633f5bd7db7ec5025d3a972 GIT binary patch literal 6148 zcmeHKyH3ME5S$As5@;eSgeWXgCnA4fBB!9BAs+xFK}m!iM~PF=nJ?lqXleKldJ34m zJ4JQ^C^QJcuCzPny&Io*{5T%~(^(GNKm$OHE?B8Ed|=WqzF`%g*(Dk|M?7lx25B;PMk=c0b*RU!ltb? zW0fguYhuW3MsH$4oMXw_GsZbpSQNE6i_?MEUS3quz4c~IPLDb+&}BBlIkANV!!^dk z8pF~wr~<0MKURQiwpeY?p+;3e6;K6M3dr{%pbLf`bBFfR!C2erDr$D8|mt^9Ob~Oz2RfDxeCK71(iy z4O#!YpYQ+6Bt23ERDnOGfT>2ksKb){+PYDkthE9CiY_MOa)&Dl8+Iw?SXqh>=+>AI Xq(KZl<_?*m>5qVwL4zvrqYAtO%<_6n literal 0 HcmV?d00001 diff --git a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb new file mode 100644 index 00000000..fc8acf51 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,392 @@ +{ + "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": 13, + "metadata": {}, + "outputs": [], + "source": [ + "#1 and #2\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What´s the quantity of t-shirt? 5\n", + "What´s the quantity of mug? 2\n", + "What´s the quantity of hat? 5\n", + "What´s the quantity of book? 9\n", + "What´s the quantity of keychain? 1\n" + ] + } + ], + "source": [ + "#3\n", + "for product in products:\n", + " outcome=input(f\"What´s the quantity of {product}?\")\n", + " inventory[product]=int(outcome)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 2, 'hat': 5, 'book': 9, 'keychain': 1}" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "#4\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What do you want to order? Add the name of three products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: hat\n", + "Enter a product name: keychain\n", + "Enter a product name: mug\n" + ] + } + ], + "source": [ + "#5\n", + "print(\"What do you want to order? Add the name of three products.\")\n", + "while len(customer_orders) < 3:\n", + " user_choice = input(\"Enter a product name: \")\n", + " if user_choice in products:\n", + " customer_orders.add(user_choice)\n", + " else:\n", + " print(\"Please enter valid products from the list.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'keychain'}\n" + ] + } + ], + "source": [ + "#6\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = ()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = total_products_ordered / len(products) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "60.0\n" + ] + } + ], + "source": [ + "print(total_products_ordered)\n", + "print(percentage_ordered)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "#7 if we mean here the available products in the inventory and not product types, then:\n", + "\n", + "percentage_ordered2 = total_products_ordered/ sum(inventory.values()) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13.636363636363635\n" + ] + } + ], + "source": [ + "print(percentage_ordered2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 13.64%\n" + ] + } + ], + "source": [ + "#8\n", + "print(f\"\"\"\n", + "Order Statistics:\n", + "Total Products Ordered: {total_products_ordered}\n", + "Percentage of Products Ordered: {percentage_ordered2:.2f}%\"\"\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "#9\n", + "for product in customer_orders:\n", + " inventory[product] = inventory[product] - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 1, 'hat': 4, 'book': 9, 'keychain': 0}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt:5\n", + "mug:1\n", + "hat:4\n", + "book:9\n", + "keychain:0\n" + ] + } + ], + "source": [ + "#10\n", + "for product in inventory:\n", + " print(f\"{product}:{inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..fc8acf51 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,322 @@ "\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": 13, + "metadata": {}, + "outputs": [], + "source": [ + "#1 and #2\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What´s the quantity of t-shirt? 5\n", + "What´s the quantity of mug? 2\n", + "What´s the quantity of hat? 5\n", + "What´s the quantity of book? 9\n", + "What´s the quantity of keychain? 1\n" + ] + } + ], + "source": [ + "#3\n", + "for product in products:\n", + " outcome=input(f\"What´s the quantity of {product}?\")\n", + " inventory[product]=int(outcome)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 2, 'hat': 5, 'book': 9, 'keychain': 1}" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "#4\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What do you want to order? Add the name of three products.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: hat\n", + "Enter a product name: keychain\n", + "Enter a product name: mug\n" + ] + } + ], + "source": [ + "#5\n", + "print(\"What do you want to order? Add the name of three products.\")\n", + "while len(customer_orders) < 3:\n", + " user_choice = input(\"Enter a product name: \")\n", + " if user_choice in products:\n", + " customer_orders.add(user_choice)\n", + " else:\n", + " print(\"Please enter valid products from the list.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'keychain'}\n" + ] + } + ], + "source": [ + "#6\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = ()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = total_products_ordered / len(products) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "60.0\n" + ] + } + ], + "source": [ + "print(total_products_ordered)\n", + "print(percentage_ordered)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "#7 if we mean here the available products in the inventory and not product types, then:\n", + "\n", + "percentage_ordered2 = total_products_ordered/ sum(inventory.values()) * 100" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13.636363636363635\n" + ] + } + ], + "source": [ + "print(percentage_ordered2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 13.64%\n" + ] + } + ], + "source": [ + "#8\n", + "print(f\"\"\"\n", + "Order Statistics:\n", + "Total Products Ordered: {total_products_ordered}\n", + "Percentage of Products Ordered: {percentage_ordered2:.2f}%\"\"\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "#9\n", + "for product in customer_orders:\n", + " inventory[product] = inventory[product] - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 1, 'hat': 4, 'book': 9, 'keychain': 0}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt:5\n", + "mug:1\n", + "hat:4\n", + "book:9\n", + "keychain:0\n" + ] + } + ], + "source": [ + "#10\n", + "for product in inventory:\n", + " print(f\"{product}:{inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +384,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,