diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..b079848a 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -6,7 +6,8 @@ "tags": [] }, "source": [ - "# Lab | Data Structures " + "# Lab 01 | Data Structures\n", + "----" ] }, { @@ -50,11 +51,316 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# SOLUTION\n", + "----" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# 1.defining the list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Create an empty dictionary called `inventory`." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# 2. creating an empty dictionary invetory\n", + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" + ] + } + ], + "source": [ + "# 3. input of the user for quantity of each product\n", + "for i in range(len(products)):\n", + " while True: # keep asking until valid\n", + " quantity = input(\"Enter the quantity for \" + products[i] + \": \")\n", + " if quantity.isdigit(): # checks if input is a positive integer\n", + " quantity = int(quantity)\n", + " inventory[products[i]] = quantity\n", + " break # valid → exit loop\n", + " else:\n", + " print(f\"❌ Invalid input! Please enter a numeric quantity for {products[i]}.\")\n", + "\n", + "# 3.5 checking our dictionary inventory\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. Create an empty set called `customer_orders`." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "# 4. creating an empty set customer orders\n", + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the list of products:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Enter the names of 3 products to order:\n", + "✅ Added 'mug' to customer orders.\n", + "✅ Added 'book' to customer orders.\n", + "✅ Added 'hat' to customer orders.\n" + ] + } + ], + "source": [ + "# 5. User input for 3 products to order\n", + "print(\"Here is the list of products:\")\n", + "print(products)\n", + "\n", + "print(\"Enter the names of 3 products to order:\")\n", + "\n", + "while len(customer_orders) < 3:\n", + " item = input(\"Enter a product name: \").lower()\n", + "\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " print(f\"✅ Added '{item}' to customer orders.\")\n", + " else:\n", + " print(\"❌ Invalid product! Please choose from:\", products)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6. Print the products in the `customer_orders` set." + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 6. Printing the ordered products\n", + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", + " Store these statistics in a tuple called `order_status`." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "# 7. Calculating order statistics\n", + "\n", + "# Total Products Ordered\n", + "total_products_ordered = len(customer_orders)\n", + "\n", + "# Percentage of Products Ordered\n", + "percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "# Calculate Order Status\n", + "order_status = (total_products_ordered, percentage_products_ordered)\n", + "# Print Order Status\n", + "#print(\"Order Status (Total, Percentage):\", order_status)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60%\n" + ] + } + ], + "source": [ + "# 8. Storing the statistics in a tuple order_status\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(f\"Percentage of Products Ordered: {int(percentage_products_ordered)}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Updating the inventory by subtracting 1 for each ordered product\n", + "for ordered_product in customer_orders:\n", + " if inventory[ordered_product] > 0:\n", + " inventory[ordered_product] = inventory[ordered_product] - 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory :\n", + "t-shirt : 10\n", + "mug : 17\n", + "hat : 27\n", + "book : 37\n", + "keychain : 50\n" + ] + } + ], + "source": [ + "# 10. Printing the updated inventory\n", + "print(\"Updated Inventory :\")\n", + "for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +374,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,