diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..b7b38a56 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +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": 2, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\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 \n", + "# and assign the respective quantities as values.\n", + "\n", + "quantity_t_shirt = int(input(\"Enter the quantity of t-shirt: \"))\n", + "inventory['t-shirt'] = quantity_t_shirt\n", + "quantity_mug = int(input(\"Enter the quantity of mug: \"))\n", + "inventory['mug'] = quantity_mug\n", + "quantity_hat = int(input(\"Enter the quantity of hat: \"))\n", + "inventory['hat'] = quantity_hat\n", + "quantity_book = int(input(\"Enter the quantity of book: \"))\n", + "inventory['book'] = quantity_book\n", + "quantity_keychain = int(input(\"Enter the quantity of keychain: \"))\n", + "inventory['keychain'] = quantity_keychain\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create an empty set called `customer_orders`.\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Ask the user to input the name of three products that a \n", + "# customer wants to order (from those in the products list, \n", + "# meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\"\n", + "# or \"keychain\". Add each product name to the `customer_orders` \n", + "# set.\n", + "\n", + "user_input1 = input(f'Enter one product from {products}: ')\n", + "user_input2 = input(f'Enter another product from {products}: ')\n", + "user_input3 = input(f'Enter one last product from {products}: ')\n", + "customer_orders.add(user_input1)\n", + "customer_orders.add(user_input2)\n", + "customer_orders.add(user_input3)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "# 6. Print the products in the `customer_orders` set.\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# 7. Calculate the following order statistics:\n", + "# - Total Products Ordered: The total number of products \n", + "# in the `customer_orders` set.\n", + "# - Percentage of Products Ordered: The percentage of products \n", + "# ordered compared to the total available products.\n", + "# Store these statistics in a tuple called `order_status`.\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_of_prodcuts_ordered = total_products_ordered/len(products)*100\n", + "order_status = (total_products_ordered, percentage_of_prodcuts_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: <3>.\n", + "Percentage of Products Ordered: <60.0>%\n" + ] + } + ], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# ```\n", + "# Order Statistics:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % \n", + "# ```\n", + "print(\n", + " f'Order Statistics:\\n'\n", + " f'Total Products Ordered: <{total_products_ordered}>.\\n'\n", + " f'Percentage of Products Ordered: <{percentage_of_prodcuts_ordered}>%'\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "# 9. Update the inventory by subtracting 1 from the quantity \n", + "# of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "inventory[\"t-shirt\"] -= 1\n", + "inventory[\"mug\"] -= 1\n", + "inventory[\"hat\"] -= 1\n", + "inventory[\"book\"] -= 1\n", + "inventory[\"keychain\"] -= 1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the upadted inventory: \n", + "T-shirt: 4\n", + "Mug: 4\n", + "Hat: 4\n", + "Book: 4\n", + "Keychain: 4\n", + "\n" + ] + } + ], + "source": [ + "# 10. Print the updated inventory, displaying the quantity \n", + "# of each product on separate lines.\n", + "\n", + "print(\n", + " f'This is the upadted inventory: \\n'\n", + " f'T-shirt: {inventory[\"t-shirt\"]}\\n'\n", + " f'Mug: {inventory[\"mug\"]}\\n'\n", + " f'Hat: {inventory[\"hat\"]}\\n'\n", + " f'Book: {inventory[\"book\"]}\\n'\n", + " f'Keychain: {inventory[\"keychain\"]}\\n'\n", + ")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -68,7 +264,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,