diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..35abf6d 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade for student 1: 5\n", + "Enter the grade for student 2: 3\n", + "Enter the grade for student 3: 2\n", + "Enter the grade for student 4: 5\n", + "Enter the grade for student 5: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2.0, 5.0, 5.0]\n", + "3\n", + "2\n" + ] + } + ], "source": [ - "# Your code here" + "grades = [] \n", + "for i in range(5): \n", + " grade = float(input(f\"Enter the grade for student {i+1}: \"))\n", + " grades.append(grade) \n", + "\n", + "sum_grades = sum(grades)\n", + "\n", + "grades_list = grades[::2] \n", + "grades_list = sorted(grades_list, reverse=False) \n", + "\n", + "\n", + "print(grades_list)\n", + "print(len(grades_list)) \n", + "print(grades_list.count(5)) " ] }, { @@ -93,15 +127,164 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('apple', 'orange', 'banana', 'peach', 'grapple')" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Initializes a tuple with 5 different types of fruit. \n", + "fruits = (\"apple\", \"orange\", \"banana\", \"peach\", \"grapple\") \n", + "fruits" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple grapple\n" + ] + } + ], + "source": [ + "# Outputs the first and last elements of the tuple, so you can see the full range of fruits the store offers.\n", + "print(fruits[0], fruits[-1]) " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'tangerine', 'banana', 'peach', 'grapple')\n" + ] + } + ], + "source": [ + "# 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", + "fruits_modify = list(fruits) \n", + "fruits_modify[1] = \"tangerine\" \n", + "fruits = tuple(fruits_modify)\n", + "print(fruits) " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 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. " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('apple', 'tangerine', 'banana', 'peach', 'grapple', 'berrie', 'lemon')" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fruits_2 = (\"berrie\", \"lemon\") \n", + "fruits_new = fruits + fruits_2 \n", + "fruits_new" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Splits the resulting tuple into 2 tuples of 3 elements each (the first tuple contains the first 3 elements, \n", + "# and the second tuple contains the last 3 elements), so you can organize the inventory more effectively." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'tangerine', 'banana')\n", + "('grapple', 'berrie', 'lemon')\n" + ] + } + ], + "source": [ + "f1 = fruits_new[:3] \n", + "f2 = fruits_new[-3:]\n", + "print(f1)\n", + "print(f2) \n" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Combines the 2 tuples from the previous step with the original tuple into a new tuple, and prints the resulting tuple and its length, \n", + "# so you can see the final inventory after all the changes." ] }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 'tangerine', 'banana', 'grapple', 'berrie', 'lemon', 'apple', 'tangerine', 'banana', 'peach', 'grapple')\n" + ] + } + ], + "source": [ + "inventory = f1 + f2 + fruits\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -136,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -161,13 +344,186 @@ "It's still what we are made of.\"\"\"" ] }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation)." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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" + ] + } + ], + "source": [ + "import string\n", + "def remove_punctuation(poem):\n", + " return poem.translate(str.maketrans(\"\", \"\", string.punctuation))\n", + "\n", + "def to_lowercase(poem):\n", + " return poem.lower()\n", + "\n", + "clean_poem = remove_punctuation(to_lowercase(poem)) \n", + "print(clean_poem) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "some say life is but a dream\n", + "some say its a test\n", + "from what ive seen and what i deem\n", + "i side with those who see it as a quest\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", + "its still what we are made of\n" + ] + } + ], + "source": [ + "def remove_punctuation(new_poem):\n", + " return new_poem.translate(str.maketrans(\"\", \"\", string.punctuation))\n", + "\n", + "def to_lowercase(new_poem):\n", + " return new_poem.lower()\n", + "\n", + "clean_new_poem = remove_punctuation(to_lowercase(new_poem)) \n", + "\n", + "print(clean_new_poem)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "41\n", + "42\n" + ] + } + ], + "source": [ + "# Print the number of unique words in each set. \n", + "set_1 = clean_poem.split() \n", + "set_2 = clean_new_poem.split() \n", + "unique_set_1 = set(set_1) \n", + "unique_set_2 = set(set_2) \n", + "print(len(unique_set_1)) \n", + "print(len(unique_set_2)) " + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Identify and print the unique words present in the first poem but not in the second one." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'great', 'favor', 'for', 'world', 'destruction', 'twice', 'perish', 'ice', 'would', 'tasted', 'desire', 'the', 'hate', 'also', 'hold', 'in', 'will', 'suffice', 'i’ve', 'fire'}\n" + ] + } + ], + "source": [ + "unique_first = unique_set_1 - unique_set_2\n", + "print(unique_first) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Identify and print the unique words present in the second poem but not in the first one." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'we', 'side', 'are', 'made', 'see', 'test', 'life', 'fades', 'still', 'love', 'ive', 'its', 'away', 'today', 'quest', 'a', 'seen', 'though', 'dream', 'as', 'deem'}\n" + ] + } + ], + "source": [ + "unique_second = unique_set_2 - unique_set_1\n", + "print(unique_second) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Identify and print the unique words present in both poems and print it in alphabetical order." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'also', 'and', 'are', 'as', 'away', 'but', 'deem', 'desire', 'destruction', 'dream', 'end', 'enough', 'fades', 'favor', 'fire', 'for', 'from', 'great', 'had', 'hate', 'hold', 'i', 'ice', 'if', 'in', 'is', 'it', 'its', 'ive', 'i’ve', 'know', 'life', 'love', 'made', 'of', 'perish', 'quest', 'say', 'see', 'seen', 'side', 'some', 'still', 'suffice', 'tasted', 'test', 'that', 'the', 'think', 'those', 'though', 'to', 'today', 'twice', 'we', 'what', 'who', 'will', 'with', 'world', 'would']\n" + ] + } + ], + "source": [ + "unique_both = unique_set_1 | unique_set_2 \n", + "def clean(unique_both): return unique_both .lower().translate(str.maketrans(\"\", \"\", string.punctuation)) \n", + "sorted_words = sorted(unique_both , key=clean)\n", + "print(sorted_words) " ] }, { @@ -193,7 +549,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +558,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100\n", + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n" + ] + } + ], "source": [ - "# Your code here" + "grades['Bob']['Philosophy'] = 100 \n", + "print(grades['Bob']['Philosophy']) \n", + "print(grades) " ] }, { @@ -239,14 +606,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ + "# 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.\n", "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "grades = zip(keys, values) \n", + "dictionary = dict(grades) \n", + "print (dictionary) " ] }, { @@ -275,12 +653,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "min_subject = min(dictionary, key=dictionary.get)\n", + "print(min_subject)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -299,7 +693,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,