diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..e81fc633 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,409 @@ "\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` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\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", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many quantities of t-shirts? 4\n", + "How many quantities of mugs? 5\n", + "How many quantities of hats? 6\n", + "How many quantities of books? 7\n", + "How many quantities of keychains? 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'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. Use the product names from the `products` list as keys in the\n", + "#`inventory` dictionary and assign the respective quantities as values.\n", + "inventory[\"t-shirt\"] = int(input(\"How many quantities of t-shirts? \"))\n", + "inventory[\"mug\"] = int(input(\"How many quantities of mugs? \"))\n", + "inventory[\"hat\"] = int(input(\"How many quantities of hats? \"))\n", + "inventory[\"book\"] = int(input(\"How many quantities of books? \"))\n", + "inventory[\"keychain\"] = int(input(\"How many quantities of keychains? \"))\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create an empty set called `customer_orders`.\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter three products from {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8} 1. The choices are t-shirt, mug, hat, book, keychain. mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'mug'.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter three products from {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8} 2. The choices are t-shirt, mug, hat, book, keychain. book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'book'.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter three products from {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8} 3. The choices are t-shirt, mug, hat, book, keychain. hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'hat'.\n" + ] + } + ], + "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 \n", + "# of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set.\n", + "for i in range(3):\n", + " order = input(f\"Enter three products from {inventory} {i+1}. The choices are t-shirt, mug, hat, book, keychain.\").strip().lower()\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " print(f\"Added '{order}'.\")\n", + " else:\n", + " print(f\"{order} is not on inventory. Please choose from {products}.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "# 6. Print the products in the `customer_orders` set.\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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", + "# Store these statistics in a tuple called `order_status`." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "print (total_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n" + ] + } + ], + "source": [ + "total_inventory = sum(int(value) for value in inventory.values())\n", + "print(total_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.0\n" + ] + } + ], + "source": [ + "# 7b Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + "percentage_ordered = total_products_ordered / len(products) * 100\n", + "print(percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 8. Print the order statistics using the following format:\n", + " ```\n", + "# Order Statistics:\n", + " # Total Products Ordered: \n", + " # Percentage of Products Ordered: % " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: total_products_ordered: 3\n", + "Percentage of Products Ordered: 60 %\n" + ] + } + ], + "source": [ + "print(\"Total Products Ordered: total_products_ordered:\",total_products_ordered)\n", + "print(\"Percentage of Products Ordered:\",int(percentage_ordered),\"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#converted the float to int, to give a full number in percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book': 1, 'hat': 1, 'keychain': 1}\n" + ] + } + ], + "source": [ + "orders = {\"book\": 1, \"hat\": 1, \"keychain\": 1}\n", + "print(orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 3, 'mug': 4, 'hat': 5, 'book': 6, 'keychain': 7}\n" + ] + } + ], + "source": [ + "for product in inventory:\n", + " inventory[product] -= 1\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 3\n", + "mug: 4\n", + "hat: 5\n", + "book: 6\n", + "keychain: 7\n" + ] + } + ], + "source": [ + "# 10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +471,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,