diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..61a4fc5 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -2,7 +2,9 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "tags": [] + }, "source": [ "# Lab | Data Structures " ] @@ -11,266 +13,226 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Exercise 1: Working with Lists" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Imagine you are building a program for a teacher who wants to track the progress of their students throughout the semester. The teacher wants to input the grades of each student one by one, and get a summary of their performance. There are in total 5 students. You are tasked with building the program that will allow the teacher to do this easily.\n", + "## Exercise: Managing Customer Orders\n", "\n", - "The program will prompt the teacher to enter the grades of each student. Once the teacher has entered all the grades, the program will calculate the total sum of the grades and display it on the screen. Then, the program will create a new list by selecting only the grades of the first, third, and fifth students entered by the teacher, and sort them in ascending order.\n", + "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", "\n", - "Finally, the program will print out the new list, along with its length and the number of occurrences of the score 5 in the list. This will give the teacher a good overview of the performance of the selected students, and help them identify any potential issues early on." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*Hint:*\n", - "- You can use the input() function to ask the user to enter their information.\n", - "- Look for list methods to perform the tasks. \n", - "- Remember, it is possible to get a part of the sequence using:\n", + "Follow the steps below to complete the exercise:\n", "\n", - " ```python\n", - " sequence[x:y:z]\n", - " ```\n", - " where x, y, z are integers.\n", + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", "\n", - " The above returns a new sequence with the following characteristics:\n", + "2. Create an empty dictionary called `inventory`.\n", "\n", - " - A sequence with the same type as the original (a slice of a list is a list, a slice of a tuple is a tuple, and a slice of a string is a string).\n", - " - A sequence with elements from `sequence [x]` to `sequence [y-1]` (does not include a sequence [y]). By skipping `z` elements each time, it can be omitted if ` z = 1`.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*Recommended External Resources:*\n", - "- *[Python Lists](https://www.w3schools.com/python/python_lists.asp)*\n", - "- *[Python List Methods](https://www.w3schools.com/python/python_ref_list.asp)*\n", - "- *[Python Built-in Functions](https://docs.python.org/3/library/functions.html)*\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise 2: Tuples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Imagine you're running a fruit stand and want to keep track of your inventory. Write a Python program that does the following:\n", + "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.\n", "\n", - "- Initializes a tuple with 5 different types of fruit.\n", - "- Outputs the first and last elements of the tuple, so you can see the full range of fruits the store offers.\n", - "- Replaces the second element of the tuple with a new fruit that the store has recently received, and prints the updated tuple so you can see the changes.\n", - "- Concatenates a new tuple containing 2 additional fruits to the original tuple, so you can add them to the store inventory, and prints the resulting tuple to see the updated inventory.\n", - "- Splits the resulting tuple into 2 tuples of 3 elements each (the first tuple contains the first 3 elements, and the second tuple contains the last 3 elements), so you can organize the inventory more effectively.\n", - "- Combines the 2 tuples from the previous step with the original tuple into a new tuple, and prints the resulting tuple and its length, so you can see the final inventory after all the changes." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*Recommended External Resources: [Python Tuples Examples and Methods](https://www.w3schools.com/python/python_tuples.asp)*\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise 3: Sets" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Imagine you are a data analyst working for a literature museum. Your manager has given you two poems to analyze, and she wants you to write a Python program to extract useful information from them.\n", + "4. Create an empty set called `customer_orders`.\n", "\n", - "Your program should:\n", + "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.\n", "\n", - "- Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n", - "- Print the number of unique words in each set.\n", - "- Identify and print the unique words present in the first poem but not in the second one.\n", - "- Identify and print the unique words present in the second poem but not in the first one.\n", - "- Identify and print the unique words present in both poems and print it in alphabetical order." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*Recommended External Resources:*\n", - "- *[Python Sets](https://www.w3schools.com/python/python_sets.asp)* \n", - "- *[Python Set Methods](https://www.w3schools.com/python/python_ref_set.asp)*\n", - "- *[Python String Methods](https://www.w3schools.com/python/python_ref_string.asp)*\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "poem = \"\"\"Some say the world will end in fire,\n", - "Some say in ice.\n", - "From what I’ve tasted of desire\n", - "I hold with those who favor fire.\n", - "But if it had to perish twice,\n", - "I think I know enough of hate\n", - "To say that for destruction ice\n", - "Is also great\n", - "And would suffice.\"\"\"\n", + "6. Print the products in the `customer_orders` set.\n", + "\n", + "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`.\n", + "\n", + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```\n", + "\n", + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", "\n", - "new_poem = \"\"\"Some say life is but a dream,\n", - "Some say it's a test.\n", - "From what I've seen and what I deem,\n", - "I side with those who see it as a quest.\n", + "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", "\n", - "But if it had to end today,\n", - "I think I know enough of love,\n", - "To say that though it fades away,\n", - "It's still what we are made of.\"\"\"" + "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": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter Amount of T shirts: 1\n", + "Enter Amount of Mug: 2\n", + "Enter Amount of Hats: 3\n", + "Enter Amount of Books: 3\n", + "Enter Amount of Keychains: 4\n" + ] + } + ], "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise 4: Dictionaries" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Consider the following dictionary of students with their scores in different subjects. One of the students, Bob, has complained about his score in Philosophy and, after reviewing it, the teacher has decided to update his score to 100. Write a Python program that updates Bob's score in Philosophy to 100 in the dictionary." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*Recommended External Resources: [Python Dictionary Examples and Methods](https://www.w3schools.com/python/python_dictionaries.asp)*\n" + "products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "inventory[\"t-shirt\"]= input(\"Enter Amount of T shirts:\")\n", + "inventory[\"mug\"]= input(\"Enter Amount of Mug:\")\n", + "inventory[\"hat\"]= input(\"Enter Amount of Hats:\")\n", + "inventory[\"book\"]= input(\"Enter Amount of Books:\")\n", + "inventory[\"keychain\"]= input(\"Enter Amount of Keychains:\")\n" ] }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': '1', 'mug': '2', 'hat': '3', 'book': '3', 'keychain': '4'}" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "grades = {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}" + "inventory" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Bonus" - ] - }, - { - "cell_type": "markdown", + "execution_count": 93, "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter Product To Order: hat\n", + "Enter Amount: 2\n", + "Enter Product To Order: mug\n", + "Enter Amount: 3\n", + "Enter Product To Order: book\n", + "Enter Amount: 4\n" + ] + } + ], "source": [ - "1. Below are the two lists. Write a Python program to convert them into a dictionary in a way that item from list1 is the key and item from list2 is the value." + "customer_orders = set()\n", + "item_desc_1 = input(\"Enter Product To Order:\")\n", + "customer_orders.add(item_desc_1)\n", + "item_amount_1 = int(input(\"Enter Amount:\"))\n", + "item_desc_2 = input(\"Enter Product To Order:\")\n", + "customer_orders.add(item_desc_2)\n", + "item_amount_2 = int(input(\"Enter Amount:\"))\n", + "item_desc_3 = input(\"Enter Product To Order:\")\n", + "customer_orders.add(item_desc_3)\n", + "item_amount_3 = int(input(\"Enter Amount:\"))" ] }, { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*Hint: Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it. Afterwards, you can use a function that turns a tuple into a dictionary.*" - ] - }, - { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 139, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': '1', 'mug': '2', 'hat': '3', 'book': '3', 'keychain': '4'}" + ] + }, + "execution_count": 139, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "*Recommended External Resources: [Python Zip Function](https://www.w3schools.com/python/ref_func_zip.asp)*\n" + "inventory" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 113, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], "source": [ - "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", - "values = [75, 85, 60,90]\n", - "\n", - "# Your code here" + "total=item_amount_1+item_amount_2+item_amount_3\n", + "print(total)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 121, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ - "2. Get the subject with the minimum score from the previous dictionary." + "customer_orders = {\"hat\":2,\"mug\":3,\"book\":4}\n", + "items = len(customer_orders)\n", + "print(items)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 154, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + " Total Products Ordered: 5 \n", + " Percentage of Products Ordered: 60.0 %\n" + ] + } + ], "source": [ - "*Hint: Use the built-in function min(). Read about the parameter key.*" + "customer_orders = {\"hat\":2,\"mug\":3,\"book\":4}\n", + "items = 5\n", + "ordered = 3\n", + "order_status = ordered/items*100\n", + "print(\"Order Statistics:\\n\",\"Total Products Ordered: \", items,\"\\n Percentage of Products Ordered: \", order_status,\"%\") " ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 168, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Customer Order:\n", + "hat: 1\n", + "mug: 2\n", + "book: 3\n" + ] + } + ], "source": [ - "\n", - "*Recommended External Resources:*\n", - "- *[Python Min Function Official Documentation](https://docs.python.org/3.8/library/functions.html#min)*\n", - "- *[How to use key function in max and min in Python](https://medium.com/analytics-vidhya/how-to-use-key-function-in-max-and-min-in-python-1fdbd661c59c)*" + "customer_orders = {\"hat\":2,\"mug\":3,\"book\":4}\n", + "for product in customer_orders:\n", + " customer_orders[product] -= 1\n", + "print(\"Updated Customer Order:\")\n", + "for product, quantity in customer_orders.items(): #This prints a new line for each item in the dictionary.\n", + " print(f\"{product}: {quantity}\")\n", + "\n" ] }, { @@ -278,16 +240,14 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here" - ] + "source": [] } ], "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": { @@ -299,7 +259,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,