diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..f906e40 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,152 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter student name: Clara\n", + "Enter Clara grade: 5\n", + "Enter student name: Gustav\n", + "Enter Gustav grade: 8\n", + "Enter student name: Ana\n", + "Enter Ana grade: 5\n", + "Enter student name: William \n", + "Enter William grade: 4\n", + "Enter student name: John\n", + "Enter John grade: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Clara': 5, 'Gustav': 8, 'Ana': 5, 'William ': 4, 'John': 9}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "students_grades = {}\n", + "for i in range(5):\n", + " student = input(\"Enter student name: \")\n", + " grade = int(input(f\"Enter {student} grade: \"))\n", + " students_grades[student] = grade\n", + "print(students_grades)\n", + "students_grades_sum = sum(students_grades.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "31\n" + ] + } + ], "source": [ - "# Your code here" + "print(students_grades_sum)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 8, 5, 4, 9]\n" + ] + } + ], + "source": [ + "students_grades_newlist = list(students_grades.values())\n", + "print(students_grades_newlist)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 5, 9]\n" + ] + } + ], + "source": [ + "print(students_grades_newlist[0:6:2])" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 5, 9]\n" + ] + } + ], + "source": [ + "students_grades_newlist_sorted = sorted(students_grades_newlist[0:6:2])\n", + "print(students_grades_newlist_sorted)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 5, 9]\n", + "3\n" + ] + } + ], + "source": [ + "print(students_grades_newlist_sorted)\n", + "print(len(students_grades_newlist_sorted))" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "count = 0\n", + "for grade_element in students_grades_newlist_sorted:\n", + " if grade_element == 5:\n", + " count +=1\n", + "print(count)" ] }, { @@ -95,11 +236,129 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "# Initializes a tuple with 5 different types of fruit.\n", + "fruits = (\"orange\", \"strawberry\", \"lemon\", \"grape\", \"banana\")" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('orange', 'strawberry', 'lemon', 'grape', 'banana')\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)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Replaces the second element of the tuple with a new fruit that the store has recently received\n", + "fruits2 = list(fruits)\n", + "fruits2[1] = \"kiwi\"\n", + "fruits = tuple(fruits2)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('orange', 'kiwi', 'lemon', 'grape', 'banana')\n" + ] + } + ], + "source": [ + "# Prints the updated tuple so you can see the changes\n", + "print(fruits)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('orange', 'kiwi', 'lemon', 'grape', 'banana', 'watermelon', 'blueberry')\n" + ] + } + ], + "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\n", + "fruits3 = (\"watermelon\", \"blueberry\")\n", + "fruits4 = list(fruits)\n", + "fruits5 = list(fruits3)\n", + "fruits = tuple(fruits4 + fruits5)\n", + "print(fruits)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('orange', 'kiwi', 'lemon')\n", + "('banana', 'watermelon', 'blueberry')\n" + ] + } + ], + "source": [ + "# 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", + "fruits6 = list(fruits)\n", + "fruits7 = fruits6[0:3]\n", + "fruits7_tuple = tuple(fruits7)\n", + "print(fruits7_tuple)\n", + "fruits8 = fruits6[len(fruits6)-3:(len(fruits6)+1)]\n", + "fruits8_tuple = tuple(fruits8)\n", + "print(fruits8_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('orange', 'kiwi', 'lemon', 'grape', 'banana', 'watermelon', 'blueberry', 'banana', 'watermelon', 'blueberry', 'orange', 'kiwi', 'lemon')\n", + "13\n" + ] + } + ], + "source": [ + "# 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.\n", + "fruits_list = list(fruits)\n", + "final_tuple = tuple(fruits_list + fruits8 + fruits7)\n", + "print(final_tuple)\n", + "print(len(final_tuple))" ] }, { @@ -136,7 +395,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +422,139 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 117, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'those', 'it', 'also', 'enough', 'would', 'with', 'think', 'hold', 'had', 'say', 'that', 'suffice', 'but', 'some', 'from', 'know', 'in', 'to', 'tasted', 'i’ve', 'who', 'end', 'what', 'great', 'i', 'will', 'favor', 'perish', 'the', 'is', 'for', 'twice', 'and', 'if', 'desire', 'ice', 'destruction', 'world', 'of', 'hate', 'fire'}\n", + "{'those', 'it', 'enough', \"i've\", 'though', 'are', 'today', 'side', 'with', 'life', 'think', 'had', 'made', 'say', 'that', 'deem', 'but', 'some', 'from', 'know', 'quest', \"it's\", 'to', 'we', 'seen', 'who', 'end', 'what', 'dream', 'as', 'i', 'still', 'is', 'a', 'and', 'if', 'see', 'fades', 'away', 'love', 'of', 'test'}\n" + ] + } + ], + "source": [ + "# Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n", + "poem_clean = poem.lower().replace(\",\",\"\").replace(\".\",\"\")\n", + "poem_clean_list = poem_clean.split()\n", + "poem_clean_set = set(poem_clean_list)\n", + "print(poem_clean_set)\n", + "\n", + "new_poem_clean = new_poem.lower().replace(\",\",\"\").replace(\".\",\"\")\n", + "new_poem_clean_list = new_poem_clean.split()\n", + "new_poem_clean_set = set(new_poem_clean_list)\n", + "print(new_poem_clean_set)" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['also', 'would', 'hold', 'suffice', 'in', 'tasted', 'i’ve', 'great', 'will', 'favor', 'perish', 'the', 'for', 'twice', 'desire', 'ice', 'destruction', 'world', 'hate', 'fire']\n", + "20\n", + "{'also', 'would', 'hold', 'suffice', 'in', 'tasted', 'i’ve', 'great', 'favor', 'will', 'perish', 'the', 'for', 'twice', 'desire', 'ice', 'destruction', 'world', 'hate', 'fire'}\n", + "20\n" + ] + } + ], + "source": [ + "# Print the number of unique words in each set.\n", + "poem_clean_set_unique_words = [word for word in poem_clean_set if word not in new_poem_clean_set]\n", + "print(poem_clean_set_unique_words)\n", + "print(len(poem_clean_set_unique_words))\n", + "print(set(poem_clean_set_unique_words))\n", + "print(len(set(poem_clean_set_unique_words)))" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[\"i've\", 'though', 'are', 'today', 'side', 'life', 'made', 'deem', 'quest', \"it's\", 'we', 'seen', 'dream', 'as', 'still', 'a', 'see', 'fades', 'away', 'love', 'test']\n", + "21\n", + "{\"i've\", 'though', 'are', 'today', 'side', 'life', 'made', 'deem', 'quest', \"it's\", 'we', 'seen', 'dream', 'as', 'still', 'a', 'see', 'fades', 'away', 'love', 'test'}\n", + "21\n" + ] + } + ], + "source": [ + "new_poem_clean_set_unique_words = [word for word in new_poem_clean_set if word not in poem_clean_set]\n", + "print(new_poem_clean_set_unique_words)\n", + "print(len(new_poem_clean_set_unique_words))\n", + "print(set(new_poem_clean_set_unique_words))\n", + "print(len(set(new_poem_clean_set_unique_words)))" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a',\n", + " 'also',\n", + " 'are',\n", + " 'as',\n", + " 'away',\n", + " 'deem',\n", + " 'desire',\n", + " 'destruction',\n", + " 'dream',\n", + " 'fades',\n", + " 'favor',\n", + " 'fire',\n", + " 'for',\n", + " 'great',\n", + " 'hate',\n", + " 'hold',\n", + " \"i've\",\n", + " 'ice',\n", + " 'in',\n", + " \"it's\",\n", + " 'i’ve',\n", + " 'life',\n", + " 'love',\n", + " 'made',\n", + " 'perish',\n", + " 'quest',\n", + " 'see',\n", + " 'seen',\n", + " 'side',\n", + " 'still',\n", + " 'suffice',\n", + " 'tasted',\n", + " 'test',\n", + " 'the',\n", + " 'though',\n", + " 'today',\n", + " 'twice',\n", + " 'we',\n", + " 'will',\n", + " 'world',\n", + " 'would']" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "join_poem_sets = poem_clean_set_unique_words + new_poem_clean_set_unique_words\n", + "sorted(join_poem_sets)" ] }, { @@ -193,7 +580,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +589,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 83, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "grades['Bob']['Philosophy'] = 100\n", + "print(grades)" ] }, { @@ -239,14 +636,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", - "values = [75, 85, 60,90]\n", - "\n", - "# Your code here" + "values = [75, 85, 60,90]" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(('Physics', 75), ('Math', 85), ('Chemistry', 60), ('Philosophy', 90))\n", + "{'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", + "from builtins import zip\n", + "from builtins import dict\n", + "dict_zip1 = zip(keys, values)\n", + "dict_tuple = tuple(dict_zip1)\n", + "print(dict_tuple)\n", + "final_dict = dict(dict_tuple)\n", + "print(final_dict)" ] }, { @@ -275,11 +695,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "min_grade = min(final_dict.values())\n", + "print(min_grade)" ] } ], @@ -299,7 +729,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,