From 944aff9e35dc3a4679d2437533cf4780e82a089e Mon Sep 17 00:00:00 2001 From: Sherin Shaji Kuruvilla Date: Sun, 22 Jun 2025 23:53:51 +0200 Subject: [PATCH] Added Solution --- lab-python-data-structures.ipynb | 149 ++++++++++++++++++++++++++----- 1 file changed, 127 insertions(+), 22 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..91c00a4 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the grade of the student1: 5\n", + "Enter the grade of the student2: 5\n", + "Enter the grade of the student3: 5\n", + "Enter the grade of the student4: 10\n", + "Enter the grade of the student5: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sum of grades is 30\n", + "Student grade list:[5, 5, 5] Length of the list:3 No.of occurences of the score 5 in the list:3\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "studentGrades=[]\n", + "studentGrades.append(int(input(\"Enter the grade of the student1:\")))\n", + "studentGrades.append(int(input(\"Enter the grade of the student2:\")))\n", + "studentGrades.append(int(input(\"Enter the grade of the student3:\")))\n", + "studentGrades.append(int(input(\"Enter the grade of the student4:\")))\n", + "studentGrades.append(int(input(\"Enter the grade of the student5:\")))\n", + "print(f\"Sum of grades is {sum(studentGrades)}\")\n", + "selected_gradesList=studentGrades[0:5:2]\n", + "selected_gradesList.sort()\n", + "print(f\"Student grade list:{selected_gradesList} Length of the list:{len(selected_gradesList)} No.of occurences of the score 5 in the list:{selected_gradesList.count(5)}\")" ] }, { @@ -95,11 +125,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 128, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The first element is:apple\n", + "The last element is:banana\n", + "updated tuple is ('apple', 'pear', 'orange', 'strawberry', 'banana')\n", + "The updated fruits inventory: ('apple', 'pear', 'orange', 'strawberry', 'banana', 'pineapple', 'watermelon')\n", + "The final fruits inventory is ('apple', 'pear', 'orange', 'banana', 'pineapple', 'watermelon', 'apple', 'pear', 'orange', 'strawberry', 'banana', 'pineapple', 'watermelon') with a length of 13\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "fruits=('apple','grapes','orange','strawberry','banana')\n", + "print(f\"The first element is:{fruits[0]}\")\n", + "print(f\"The last element is:{fruits[-1]}\")\n", + "fruits_list=list(fruits)\n", + "fruits_list[1]='pear'\n", + "fruits=tuple(fruits_list)\n", + "print(f\"updated tuple is {fruits}\")\n", + "fruits=fruits+('pineapple','watermelon')\n", + "print(f\"The updated fruits inventory: {fruits}\")\n", + "first_tuple=fruits[0:3]\n", + "second_tuple=fruits[-3:]\n", + "final_fruits_inventory = first_tuple + second_tuple + fruits\n", + "print(f\"The final fruits inventory is {final_fruits_inventory} with a length of {len(final_fruits_inventory)}\")" ] }, { @@ -136,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 160, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +218,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 164, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of unique words present in poem:41\n", + "Number of unique words present in new_poem:42\n", + "The unique words present in poem but not in new_poem:{'perish', 'would', 'suffice', 'i’ve', 'great', 'also', 'destruction', 'fire', 'will', 'for', 'desire', 'hold', 'favor', 'the', 'hate', 'tasted', 'in', 'twice', 'world', 'ice'}\n", + "The unique words present in new_poem but not in poem:{'test', 'still', 'we', 'its', 'see', 'made', 'as', 'love', 'quest', 'away', 'seen', 'life', 'though', 'side', 'today', 'fades', 'deem', 'ive', 'a', 'dream', 'are'}\n", + "The list of unique words present in both poems in alphabetical order:['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" + "#your code here\n", + "import string\n", + "punctuated_poem =poem.lower().translate(str.maketrans('','',string.punctuation))\n", + "punctuated_new_poem=new_poem.lower().translate(str.maketrans('','',string.punctuation))\n", + "poem_set=set(punctuated_poem.split())\n", + "new_poem_set=set(punctuated_new_poem.split())\n", + "\n", + "print(f\"Number of unique words present in poem:{len(poem_set)}\")\n", + "print(f\"Number of unique words present in new_poem:{len(new_poem_set)}\")\n", + "\n", + "unique_words_of_poem=poem_set-new_poem_set\n", + "print(f\"The unique words present in poem but not in new_poem:{unique_words_of_poem}\")\n", + "\n", + "unique_words_of_new_poem=new_poem_set-poem_set\n", + "print(f\"The unique words present in new_poem but not in poem:{unique_words_of_new_poem}\")\n", + "\n", + "unique_words_of_bothpoems=poem_set.intersection(new_poem_set)\n", + "print(f\"The list of unique words present in both poems in alphabetical order:{sorted(unique_words_of_bothpoems)}\")\n" ] }, { @@ -193,7 +277,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 169, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +286,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 173, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "grades['Bob']['Philosophy']=100" ] }, { @@ -239,14 +324,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 211, "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" + "# Your code here\n", + "new_dictionary=dict(zip(keys,values))\n", + "print(new_dictionary)" ] }, { @@ -275,19 +370,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 209, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The subject with minimum score is Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "minimum_score_subject=min(new_dictionary,key=new_dictionary.get)\n", + "print(f\"The subject with minimum score is {minimum_score_subject}\")" ] } ], "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 +404,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,