From c44d93ccf29bccf94f82b58ed7e4a2994f0e1f0d Mon Sep 17 00:00:00 2001 From: mosijituo049 Date: Tue, 5 May 2026 16:21:02 +0200 Subject: [PATCH] zhiwen week1/day1 Lab | Data Structures --- lab-python-data-structures.ipynb | 100 ++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..9b0af92c 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,104 @@ "\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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please enter the quantity of each product available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 0\n", + "mug: 4\n", + "hat: 6\n", + "book: 4\n", + "keychain: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 0, 'mug': 4, 'hat': 6, 'book': 4, 'keychain': 8}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "please enter three products that you want to purchase: t-shirt book book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered:\n", + "2Percentage of Products Ordered:50.00%\n", + "t-shirt is not available\n", + "updated inventory: {'t-shirt': 0, 'mug': 4, 'hat': 6, 'book': 2, 'keychain': 8}\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "total_available_products=products.copy()\n", + "inventory={}\n", + "quantity=0\n", + "print(\"please enter the quantity of each product available.\")\n", + "for i in products:\n", + " quantity=int(input(i+\":\"))\n", + " while quantity<0:\n", + " quantity=int(input(\"please enter a new number that is not under 0:\"))\n", + " if quantity ==0:\n", + " total_available_products.remove(i)\n", + " inventory[i]=quantity\n", + "print(inventory)\n", + "\n", + "product_ordered_list=input(\"please enter three products that you want to purchase:\").split()\n", + "product_ordered={}\n", + "for i in product_ordered_list: \n", + " if i in product_ordered:\n", + " product_ordered[i]+=1\n", + " else:\n", + " product_ordered.update({i:1})\n", + "product_ordered\n", + " \n", + "customer_orders=set(product_ordered_list) & set(products)\n", + "total_products_ordered=len(customer_orders)\n", + "percentage_ordered=len(customer_orders)/len(total_available_products)\n", + "print(f\"Order Statistics:\\nTotal Products Ordered:{total_products_ordered}\\n Percentage of Products Ordered:{percentage_ordered:.2%}\")\n", + "\n", + "for j in product_ordered:\n", + " if not(j in inventory):\n", + " print(f\"there is no {j}\")\n", + " elif inventory[j]==0:\n", + " print(f\"{j} is not available\")\n", + " elif product_ordered[j]>inventory[j]:\n", + " print(f\"only {inventory[i]} {i} in stock\")\n", + " inventory[j]=0\n", + " else:\n", + " inventory[j]=inventory[j]-product_ordered[j]\n", + "print(\"updated inventory:\",inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -68,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.14" } }, "nbformat": 4,