From ec5eeeda8667fc89f11ed3c6ce6a80b52d005963 Mon Sep 17 00:00:00 2001 From: annnieglez Date: Sat, 22 Feb 2025 20:42:44 +0100 Subject: [PATCH] lab done --- lab-python-data-structures.ipynb | 158 ++++++++++++++++++++++++++----- 1 file changed, 136 insertions(+), 22 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..5002e18 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": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input for student 2.\n", + "Invalid input for student 2.\n", + "Invalid input for student 3.\n", + "The total sum of grades is: 87\n", + "The new grade list is: [1, 1, 1, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6], the length of this list is 15.\n", + "The ocurrence of the score 5 in the list is: 6\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "grades_list = []\n", + "\n", + "for student in range (0,5):\n", + " while True:\n", + " try:\n", + " student_grade = (input(f\"Enter the grades of the {student + 1} student\")).split()\n", + " student_grade_int = [int(grade) for grade in student_grade]\n", + " grades_list.append(student_grade_int)\n", + " break\n", + " except ValueError:\n", + " print(f\"Invalid input for student {student + 1}.\")\n", + "\n", + "\n", + "\n", + "sum_ = sum([int(grade) for grades in grades_list for grade in grades])\n", + "print (f\"The total sum of grades is: {sum_}\")\n", + "\n", + "new_grade_list = sorted([int(grade) for grades in grades_list[::2] for grade in grades])\n", + "\n", + "print (f\"The new grade list is: {new_grade_list}, the length of this list is {len(new_grade_list)}.\\nThe ocurrence of the score 5 in the list is: {new_grade_list.count(5)}\")\n", + "\n" ] }, { @@ -95,11 +129,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The first fruit is mango and the last pineaple\n", + "The update fruits are: ('mango', 'kiwi', 'apple', 'orange', 'pineaple')\n", + "The new update fruits are: ('mango', 'kiwi', 'apple', 'orange', 'pineaple', 'strawberry', 'blackberry')\n", + "The last update fruits are: ('mango', 'kiwi', 'apple', 'orange', 'pineaple', 'strawberry', 'blackberry', 'mango', 'kiwi', 'apple', 'pineaple', 'strawberry', 'blackberry'), and there are 13 fruits in total\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "fruits_tuple = tuple((\"mango\", \"banana\", \"apple\", \"orange\", \"pineaple\"))\n", + "print(f\"The first fruit is {fruits_tuple[0]} and the last {fruits_tuple[-1]}\")\n", + "\n", + "fruits_list = list(fruits_tuple)\n", + "fruits_list[1] = \"kiwi\"\n", + "fruits_tuple_new = tuple(fruits_list)\n", + "print(f\"The update fruits are: {fruits_tuple_new}\")\n", + "\n", + "new_fruits = (\"strawberry\", \"blackberry\")\n", + "fruits_tuple_new = fruits_tuple_new + new_fruits\n", + "print(f\"The new update fruits are: {fruits_tuple_new}\")\n", + "\n", + "last_fruit_tuple = fruits_tuple_new + fruits_tuple_new[:3] + fruits_tuple_new[-3:]\n", + "print(f\"The last update fruits are: {last_fruit_tuple}, and there are {len(last_fruit_tuple)} fruits in total\")\n" ] }, { @@ -136,7 +196,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +223,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 20 unique words in the poem\n", + "There are 21 unique words in the new poem\n", + "Words present in both poems (sorted): ['a', 'also', 'and', 'are', 'as', 'away', 'but', 'deem', 'desire', 'destruction', 'dream', 'end', 'enough', 'fades', 'favor', 'fire', 'for', 'from', 'great', 'had', 'hate', 'hold', 'i', \"i've\", 'ice', 'if', 'in', 'is', 'it', \"it's\", '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": [ - "# Your code here" + "# Your code here\n", + "poem_set = set(poem.lower().replace(\",\", \"\").replace(\".\", \"\").replace(\"\\n\", \" \").split())\n", + "new_poem_set = set(new_poem.lower().replace(\",\", \"\").replace(\".\", \"\").replace(\"\\n\", \" \").split())\n", + "\n", + "unique_words_in_poem = poem_set.difference(new_poem_set)\n", + "print(f\"There are {len(unique_words_in_poem)} unique words in the poem\")\n", + "\n", + "unique_words_in_new_poem = new_poem_set.difference(poem_set)\n", + "print(f\"There are {len(unique_words_in_new_poem)} unique words in the new poem\")\n", + "\n", + "common_words = sorted(new_poem_set.union(poem_set))\n", + "print(f\"Words present in both poems (sorted): {common_words}\")" ] }, { @@ -193,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +283,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "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 +330,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ + "# Your code here\n", + "\n", "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "dict_ = dict(zip(keys, values))\n", + "print(dict_)" ] }, { @@ -275,17 +377,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The subject with the minimun score is Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "subjet_min = min(dict_, key=dict_.get)\n", + "\n", + "print(f\"The subject with the minimun score is {subjet_min}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -299,7 +413,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.2" } }, "nbformat": 4,