From 620da1dbb2792c01c838b68f7ee9bb24e77f7ec3 Mon Sep 17 00:00:00 2001 From: Patricia Viladomiu Date: Thu, 20 Nov 2025 12:33:51 +0100 Subject: [PATCH] Lab solved --- lab-python-data-structures.ipynb | 202 ++++++++++++++++++++++++++++++- 1 file changed, 199 insertions(+), 3 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..1ebb46a8 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,13 +50,209 @@ "\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": 4, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please, enter the quantity of t-shirt in stock: 34\n", + "Please, enter the quantity of mug in stock: 2\n", + "Please, enter the quantity of hat in stock: 12\n", + "Please, enter the quantity of book in stock: 5\n", + "Please, enter the quantity of keychain in stock: 32\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 34, 'mug': 2, 'hat': 12, 'book': 5, 'keychain': 32}\n" + ] + } + ], + "source": [ + "for product in products:\n", + " quantity = int(input(f\"Please, enter the quantity of {product} in stock: \"))\n", + " inventory[product] = quantity\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please, enter three products to order: book\n", + "Please, enter three products to order: book\n", + "You already ordered that product, enter a different one: hat\n", + "Please, enter three products to order: t-shirt\n" + ] + } + ], + "source": [ + "for i in range(3):\n", + " product = input(\"Please, enter three products to order: \")\n", + " \n", + " while product not in products or product in customer_orders:\n", + " if product not in products:\n", + " product = input(\"Invalid product, try again: \")\n", + " else:\n", + " product = input(\"You already ordered that product, enter a different one: \")\n", + "\n", + " customer_orders.add(product)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your order includes: {'t-shirt', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "print(f\"Your order includes: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(85, 3.5294117647058822)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total_ordered = len(customer_orders)\n", + "total_products = sum(inventory.values())\n", + " \n", + "percentage_products_ordered= (total_ordered/total_products)*100\n", + "\n", + "order_status= (total_products, percentage_products_ordered)\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 3.5294117647058822%\n" + ] + } + ], + "source": [ + "print(f\"Order Statistics:\\nTotal Products Ordered: {total_ordered}\\nPercentage of Products Ordered: {percentage_products_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order processed\n", + "Order processed\n", + "Order processed\n" + ] + } + ], + "source": [ + "for product in customer_orders: \n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " print(\"Order processed\")\n", + " else: \n", + " print(\"Out of stock\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 33\n", + "mug: 2\n", + "hat: 11\n", + "book: 4\n", + "keychain: 32\n" + ] + } + ], + "source": [ + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] } ], "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 +264,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,