diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..43520ef 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 10, 8, 5, 4]\n" + ] + } + ], + "source": [ + "students_grades = []\n", + "student1 = int(input(\"Enter first student grades: \"))\n", + "students_grades.append(student1)\n", + "student2 = int(input(\"Enter second student grades: \"))\n", + "students_grades.append(student2)\n", + "student3 = int(input(\"Enter third student grades: \"))\n", + "students_grades.append(student3)\n", + "student4 = int(input(\"Enter fourth student grades: \"))\n", + "students_grades.append(student4)\n", + "student5 = int(input(\"Enter fifth student grades: \"))\n", + "students_grades.append(student5)\n", + "print(students_grades)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n" + ] + } + ], + "source": [ + "print(sum(students_grades))" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 4, 8]\n" + ] + } + ], + "source": [ + "selected_students = sorted(students_grades[::2])\n", + "print(selected_students)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the new list with first, third and fifth students grades: [3, 4, 8]. Has a length of 3 and the score 5 has occurred 1 times.\n" + ] + } + ], "source": [ - "# Your code here" + "occurrences = students_grades.count(5)\n", + "print(f\"This is the new list with first, third and fifth students grades: {selected_students}. Has a length of {len(selected_students)} and the score 5 has occurred {occurrences} times.\")" ] }, { @@ -93,13 +165,187 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First tuple has this fruits ('banana', 'orange', 'watermelon', 'grape', 'apple')\n" + ] + } + ], + "source": [ + "fruit1 = input(\"Enter frist fruit: \")\n", + "fruit2 = input(\"Enter second fruit: \")\n", + "fruit3 = input(\"Enter third fruit: \")\n", + "fruit4 = input(\"Enter forth fruit: \")\n", + "fruit5 = input(\"Enter fifth fruit: \")\n", + "\n", + "tuple1 = (fruit1, fruit2, fruit3, fruit4, fruit5)\n", + "\n", + "print(f\"First tuple has this fruits {tuple1}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(tuple1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " First fruit on the tuple is: banana\n", + " Last fruit on the tuple is: apple\n" + ] + } + ], + "source": [ + "print(f\" First fruit on the tuple is: {tuple1[0]}\")\n", + "print(f\" Last fruit on the tuple is: {tuple1[4]}\")" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "#From here down, I've been doing the exercice knowing that tuples cannot be modified, concatenized without repeating elements. So maybe the exercice was wrong?" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the second tuple with these fruits: ('banana', 'orange', 'pear', 'grape', 'apple')\n" + ] + } + ], + "source": [ + "list_from_tuple1 = list(tuple1)\n", + "\n", + "list_from_tuple1.remove(\"watermelon\")\n", + "\n", + "list_from_tuple1.insert(2, \"pear\")\n", + "\n", + "tuple2 = tuple(list_from_tuple1)\n", + "\n", + "print(f\"This is the second tuple with these fruits: {tuple2}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the updated tuple: ('banana', 'orange', 'watermelon', 'grape', 'apple', 'banana', 'orange', 'watermelon', 'grape', 'apple', 'peach', 'melon')\n" + ] + }, + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fruit7 = input(\"Enter a new fruit: \")\n", + "fruit8 = input(\"Enter a new fruit: \")\n", + "tuple3 = (fruit1, fruit2, fruit3, fruit4, fruit5, fruit7, fruit8)\n", + "\n", + "\n", + "combined_tuples = tuple1 + tuple3\n", + "print(f\"This is the updated tuple: {combined_tuples}\")\n", + "len(combined_tuples)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('banana', 'orange', 'watermelon')\n", + "('grape', 'apple', 'peach', 'melon')\n" + ] + } + ], + "source": [ + "split_tuple1 = combined_tuples[:3]\n", + "split_tuple2 = combined_tuples[-4:]\n", + "\n", + "print(split_tuple1)\n", + "print(split_tuple2)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('banana', 'orange', 'watermelon', 'grape', 'apple', 'banana', 'orange', 'watermelon', 'grape', 'apple', 'peach', 'melon')\n" + ] + }, + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "last_tuple = tuple1 + split_tuple1 + split_tuple2\n", + "print(last_tuple)\n", + "len(last_tuple)" ] }, { @@ -136,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +409,124 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire', 'some', 'say', 'in', 'ice', 'from', 'what', 'i’ve', 'tasted', 'of', 'desire', 'i', 'hold', 'with', 'those', 'who', 'favor', 'fire', 'but', 'if', 'it', 'had', 'to', 'perish', 'twice', 'i', 'think', 'i', 'know', 'enough', 'of', 'hate', 'to', 'say', 'that', 'for', 'destruction', 'ice', 'is', 'also', 'great', 'and', 'would', 'suffice']\n" + ] + } + ], + "source": [ + "poem1 = poem.lower().replace(\",\" , \" \").replace(\".\", \" \").replace(\"\\n\", \" \").split()\n", + "\n", + "print(poem1)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['some', 'say', 'life', 'is', 'but', 'a', 'dream', 'some', 'say', 'it', 's', 'a', 'test', 'from', 'what', 'i', 've', 'seen', 'and', 'what', 'i', 'deem', 'i', 'side', 'with', 'those', 'who', 'see', 'it', 'as', 'a', 'quest', 'but', 'if', 'it', 'had', 'to', 'end', 'today', 'i', 'think', 'i', 'know', 'enough', 'of', 'love', 'to', 'say', 'that', 'though', 'it', 'fades', 'away', 'it', 's', 'still', 'what', 'we', 'are', 'made', 'of']\n" + ] + } + ], + "source": [ + "\n", + "import string\n", + "new_poem = new_poem.lower()\n", + "\n", + "for char in string.punctuation:\n", + " new_poem = new_poem.replace(char, \" \")\n", + "\n", + "new_poem2 = new_poem.split()\n", + "print(new_poem2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'perish', 'twice', 'what', 'suffice', 'in', 'end', 'i', 'if', 'that', 'tasted', 'fire', 'those', 'to', 'also', 'from', 'desire', 'had', 'great', 'hate', 'destruction', 'for', 'some', 'but', 'of', 'know', 'and', 'the', 'it', 'is', 'favor', 'enough', 'who', 'ice', 'hold', 'think', 'say', 'world', 'with', 'i’ve', 'would', 'will'}\n", + "{'still', 'see', 'what', 'today', 'dream', 'test', 'end', 'i', 'if', 'that', 'fades', 'we', 'as', 'are', 'to', 've', 'those', 'from', 'had', 'away', 'a', 'life', 'made', 'deem', 'but', 'of', 'and', 'know', 'quest', 'it', 'is', 's', 'love', 'though', 'enough', 'side', 'who', 'think', 'say', 'seen', 'with', 'some'}\n" + ] + } + ], + "source": [ + "poem1_set = set(poem1)\n", + "poem2_set = set(new_poem2)\n", + "\n", + "print(poem1_set)\n", + "print(poem2_set)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'perish', 'suffice', 'twice', 'in', 'tasted', 'fire', 'also', 'desire', 'great', 'destruction', 'for', 'the', 'favor', 'ice', 'hold', 'hate', 'world', 'i’ve', 'would', 'will'}\n" + ] + } + ], + "source": [ + "difference_poem1 = poem1_set - poem2_set\n", + "print(difference_poem1)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'still', 'see', 'today', 'dream', 'test', 'we', 'fades', 'as', 'are', 've', 'away', 'a', 'life', 'made', 'deem', 'quest', 's', 'love', 'though', 'side', 'seen'}\n" + ] + } + ], + "source": [ + "difference_poem2 = poem2_set - poem1_set\n", + "print(difference_poem2)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n" + ] + } + ], "source": [ - "# Your code here" + "poems_intersection = poem1_set & poem2_set\n", + "poems_intersection_sorted = sorted(poems_intersection)\n", + "print(poems_intersection_sorted)" ] }, { @@ -193,7 +552,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 101, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +561,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[\"Bob\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 103, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "grades[\"Bob\"][\"Philosophy\"] = 100" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades[\"Bob\"]" ] }, { @@ -241,12 +640,22 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "new_dict = dict(zip(keys, values))\n", + "\n", + "print(new_dict)\n" ] }, { @@ -277,15 +686,26 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Chemistry'" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "min(new_dict)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -299,7 +719,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,