diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..621b0de6 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -1,7 +1,7 @@ { "cells": [ { - "cell_type": "markdown", + "cell_type": "raw", "metadata": { "tags": [] }, @@ -50,6 +50,207 @@ "\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": 83, + "metadata": {}, + "outputs": [], + "source": [ + "#1 Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "#2 Create an empty dictionary called inventory.\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please give me the stock of t-shirt 10\n", + "Please give me the stock of mug 10\n", + "Please give me the stock of hat 10\n", + "Please give me the stock of book 10\n", + "Please give me the stock of keychain 10\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 and assign the respective quantities as values\n", + "for product in products:\n", + " stock= input(f\"Please give me the stock of {product}\") \n", + " while not stock.isdigit():\n", + " stock= input(f\"Please give me the stock of {product}\")\n", + " inventory[product] = int(stock) " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "#4 Create an empty set called customer_orders\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "choose 3 products only : t-shirt, mug, hat, book, keychain hat,mug,book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "user_choice: ['hat', 'mug', 'book']\n" + ] + } + ], + "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, 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 = set()\n", + "user_whishlist = input(\"choose 3 products only : t-shirt, mug, hat, book, keychain \")\n", + "try:\n", + " user_choice = user_whishlist.split(',')\n", + " number_item = len(user_choice)\n", + " while number_item != 3:\n", + " user_whishlist = input(\" only 3 products are possible \")\n", + " user_choice = user_whishlist.split(',')\n", + " number_item = len(user_choice)\n", + " else:\n", + " print(f\"user_choice: {user_choice}\")\n", + " for choice in user_choice:\n", + " if choice in products:\n", + " customer_orders.add(choice)\n", + " else:\n", + " customer_orders = set()\n", + " print(\"you didn't choose the right products types and you will loose your order\")\n", + " \n", + "except:\n", + " input(\"you didn't choose the right products types/number\")\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "# 6 Print the products in the customer_orders set.\n", + "print(customer_orders) " + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [], + "source": [ + "# 7 Calculate the following order statistics:\n", + "from functools import reduce\n", + "total_available_products =reduce(lambda a, b: a + b, inventory.values())\n", + "total_products_ordered = len(user_choice)\n", + "order_status = (total_products_ordered /total_available_products) * 100\n" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "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 using the following format:\n", + "\n", + "print(f\"\"\"Order Statistics:\n", + "Total Products Ordered: {total_products_ordered} \n", + "Percentage of Products Ordered: {order_status}%\"\"\" )" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n" + ] + } + ], + "source": [ + "#9 Update the inventory by subtracting 1 from the quantity of each product. \n", + "#Modify the inventory dictionary accordingly.\n", + "for order_item in customer_orders:\n", + " inventory[order_item] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The updated stock for t-shirt is 10\n", + "The updated stock for mug is 9\n", + "The updated stock for hat is 9\n", + "The updated stock for book is 9\n", + "The updated stock for keychain is 10\n" + ] + } + ], + "source": [ + "# 10 Print the updated inventory, displaying the quantity of each product on separate lines\n", + "for key,value in inventory.items():\n", + " print(f\"The updated stock for {key} is {value}\")\n" + ] } ], "metadata": { @@ -68,7 +269,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,