diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..e102e56 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,114 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Imagine you are building a program for a teacher who wants to track the progress of their students throughout the semester. \n", + "# The teacher wants to input the grades of each student one by one, and get a summary of their performance. There are in total 5 students.\n", + "students = {}\n", + "def input_grades():\n", + " for i in range(1, 6):\n", + " name = input(f\"Enter the name of student {i}: \")\n", + " grades = []\n", + " for j in range(1, 4): # Assuming 3 grades per student\n", + " grade = float(input(f\"Enter grade {j} for {name}: \"))\n", + " grades.append(grade)\n", + " students[name] = grades\n", + "\n", + "def calculate_average(grades):\n", + " return sum(grades) / len(grades)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "#example usage\n", + "input_grades()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Summary of Student Grades:\n", + "Velia: Grades: [3.0, 4.0, 5.0], Average: 4.00\n", + "Cesc: Grades: [4.0, 5.0, 6.0], Average: 5.00\n", + "Arnau: Grades: [3.0, 4.0, 5.0], Average: 4.00\n", + "Biel : Grades: [2.0, 3.0, 4.0], Average: 3.00\n", + "Mariona: Grades: [4.0, 5.0, 6.0], Average: 5.00\n", + "Biel: Grades: [3.0, 4.0, 5.0], Average: 4.00\n" + ] + } + ], + "source": [ + "#The program will prompt the teacher to enter the grades of each student. Once the teacher has entered all the grades, \n", + "# the program will calculate the total sum of the grades and display it on the screen. \n", + "def display_summary():\n", + " print(\"\\nSummary of Student Grades:\")\n", + " for student, grades in students.items():\n", + " average = calculate_average(grades)\n", + " print(f\"{student}: Grades: {grades}, Average: {average:.2f}\")\n", + "display_summary()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "# Then, the program will create a new list by selecting only the grades of the first, third, and fifth students entered by the teacher, and sort them in ascending order.\n", + "def select_and_sort_grades():\n", + " selected_grades = []\n", + " for i, (student, grades) in enumerate(students.items()):\n", + " if i in [0, 2, 4]: # Selecting 1st, 3rd, and 5th students (0-indexed)\n", + " selected_grades.extend(grades)\n", + " selected_grades.sort()\n", + " print(\"\\nSelected and Sorted Grades:\", selected_grades)\n", + " return selected_grades # Return the list\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Selected and Sorted Grades: [3.0, 3.0, 4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 6.0]\n", + "\n", + "Final Summary:\n", + "Selected Grades: [3.0, 3.0, 4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 6.0]\n", + "Total Number of Grades: 9\n", + "Occurrences of score 5: 3\n" + ] + } + ], + "source": [ + "#Finally, the program will print out the new list, along with its length and the number of occurrences of the score 5 in the list. \n", + "# This will give the teacher a good overview of the performance of the selected students, and help them identify any potential issues early on.\n", + "def print_final_summary(selected_grades):\n", + " print(\"\\nFinal Summary:\")\n", + " print(\"Selected Grades:\", selected_grades)\n", + " print(\"Total Number of Grades:\", len(selected_grades))\n", + " print(\"Occurrences of score 5:\", selected_grades.count(5))\n", + "\n", + "selected = select_and_sort_grades()\n", + "print_final_summary(selected)\n" ] }, { @@ -95,11 +198,142 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('apple', 'banana', 'cherry', 'strawberrie', 'orange')" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "#Initializing a tuple with 5 different types of fruit:\n", + "fruit_tuple = (\"apple\", \"banana\", \"cherry\", \"strawberrie\", \"orange\")\n", + "fruit_tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "First fruit: apple\n", + "Last fruit: orange\n" + ] + } + ], + "source": [ + "#Output the first and last elements of the tuple, so we can see the full range of fruits the store offers.\n", + "print(\"\\nFirst fruit:\", fruit_tuple[0])\n", + "print(\"Last fruit:\", fruit_tuple[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Fruit Tuple: ('apple', 'kiwi', 'cherry', 'strawberrie', 'orange')\n" + ] + } + ], + "source": [ + "#Replaces the second element of the tuple with a new fruit and prints the updated tuple.\n", + "fruit_list = list(fruit_tuple) # Convert tuple to list to modify it\n", + "fruit_list[1] = \"kiwi\" # Replacing \"banana\" with \"kiwi\"\n", + "fruit_tuple = tuple(fruit_list) # Convert back to tuple\n", + "print(\"\\nUpdated Fruit Tuple:\", fruit_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Fruit Inventory: ('apple', 'kiwi', 'cherry', 'strawberrie', 'orange', 'grape', 'mango')\n" + ] + } + ], + "source": [ + "#Concatenates a new tuple containing 2 additional fruits to the original tuple, so you can add them to the store inventory, \n", + "# and prints the resulting tuple to see the updated inventory.\n", + "new_fruits = (\"grape\", \"mango\")\n", + "fruit_tuple = fruit_tuple + new_fruits # Concatenate the new fruits\n", + "print(\"\\nUpdated Fruit Inventory:\", fruit_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "First Tuple: ('apple', 'kiwi', 'cherry')\n", + "Second Tuple: ('strawberrie', 'orange', 'grape', 'mango')\n" + ] + } + ], + "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 4 elements), so you can organize the inventory more effectively.\n", + "def split_fruit_tuple(fruit_tuple):\n", + " first_tuple = fruit_tuple[:3] # First 3 elements\n", + " second_tuple = fruit_tuple[3:] # Last 4 elements\n", + " print(\"\\nFirst Tuple:\", first_tuple)\n", + " print(\"Second Tuple:\", second_tuple)\n", + " return first_tuple, second_tuple\n", + "first_tuple, second_tuple = split_fruit_tuple(fruit_tuple)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Combined Tuple: ('apple', 'kiwi', 'cherry', 'strawberrie', 'orange', 'grape', 'mango', 'apple', 'kiwi', 'cherry', 'strawberrie', 'orange', 'grape', 'mango')\n", + "Length of Combined Tuple: 14\n" + ] + } + ], + "source": [ + "#Combines the 2 tuples from the previous step with the original tuple into a new tuple, \n", + "# and prints the resulting tuple and its length, so you can see the final inventory after all the changes.\n", + "def combine_tuples(original, first, second):\n", + " combined_tuple = original + first + second\n", + " print(\"\\nCombined Tuple:\", combined_tuple)\n", + " print(\"Length of Combined Tuple:\", len(combined_tuple))\n", + " return combined_tuple\n", + "combined_tuple = combine_tuples(fruit_tuple, first_tuple, second_tuple)" ] }, { @@ -136,7 +370,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +397,129 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'fire', 'hold', 'tasted', 'if', 'from', 'it', 'i', 'say', 'but', 'suffice', 'world', 'the', 'had', 'would', 'those', 'also', 've', 'favor', 'with', 'of', 'enough', 'think', 'great', 'and', 'twice', 'destruction', 'that', 'end', 'in', 'ice', 'who', 'for', 'is', 'to', 'will', 'what', 'perish', 'know', 'hate', 'some', 'desire'}\n", + "{'if', 's', 'side', 'dream', 'seen', 'we', 'from', 'it', 'see', 'i', 'say', 'are', 'but', 'fades', 'quest', 'had', 'made', 'those', 've', 'love', 'away', 'with', 'of', 'enough', 'think', 'and', 'deem', 'test', 'that', 'end', 'a', 'life', 'who', 'as', 'is', 'though', 'to', 'what', 'know', 'today', 'some', 'still'}\n" + ] + } + ], "source": [ - "# Your code here" + "#Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n", + "def create_word_set(poem):\n", + " import re\n", + " words = re.findall(r'\\b\\w+\\b', poem.lower()) # Extract words, ignoring punctuation and case\n", + " return set(words)\n", + "poem_set = create_word_set(poem)\n", + "new_poem_set = create_word_set(new_poem)\n", + "\n", + "print(poem_set)\n", + "print(new_poem_set)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Number of unique words in the original poem: 41\n", + "Number of unique words in the new poem: 42\n" + ] + } + ], + "source": [ + "#Print the number of unique words in each set.\n", + "print(\"\\nNumber of unique words in the original poem:\", len(poem_set))\n", + "print(\"Number of unique words in the new poem:\", len(new_poem_set))" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Unique words in the first poem: {'fire', 'hold', 'tasted', 'suffice', 'world', 'the', 'would', 'also', 'favor', 'great', 'twice', 'destruction', 'in', 'ice', 'for', 'will', 'perish', 'hate', 'desire'}\n", + "Length of unique words in the first poem: 19\n" + ] + } + ], + "source": [ + "#Identify and print the unique words present in the first poem but not in the second one.\n", + "def unique_words_in_first(poem_set, new_poem_set):\n", + " unique_words = poem_set - new_poem_set\n", + " print(\"\\nUnique words in the first poem:\", unique_words)\n", + " return unique_words # Return the set of unique words\n", + "unique_words = unique_words_in_first(poem_set, new_poem_set)\n", + "length_unique = len(unique_words)\n", + "print(\"Length of unique words in the first poem:\", length_unique)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Unique words in the second poem: {'s', 'side', 'dream', 'we', 'seen', 'see', 'are', 'fades', 'quest', 'made', 'love', 'away', 'deem', 'test', 'a', 'life', 'as', 'though', 'today', 'still'}\n", + "Length of unique words in the second poem: 20\n" + ] + } + ], + "source": [ + "#Identify and print the unique words present in the second poem but not in the first one.\n", + "def unique_words_in_second(new_poem_set, poem_set):\n", + " unique_words_2 = new_poem_set - poem_set\n", + " print(\"\\nUnique words in the second poem:\", unique_words_2)\n", + " return unique_words_2 # Return the set of unique words\n", + "unique_words_2 = unique_words_in_second(new_poem_set, poem_set)\n", + "length_unique_2 = len(unique_words_2)\n", + "print(\"Length of unique words in the second poem:\", length_unique_2)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Common words in both poems (sorted): ['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 've', 'what', 'who', 'with']\n", + "Length of common words in both poems: 22\n" + ] + } + ], + "source": [ + "#Identify and print the unique words present in both poems and print it in alphabetical order.\n", + "def common_words(poem_set, new_poem_set):\n", + " common = poem_set & new_poem_set # Intersection of both sets\n", + " sorted_common = sorted(common) # Sort the common words alphabetically\n", + " print(\"\\nCommon words in both poems (sorted):\", sorted_common)\n", + " return sorted_common # Return the sorted list of common words\n", + "common_words_list = common_words(poem_set, new_poem_set)\n", + "length_common = len(common_words_list)\n", + "print(\"Length of common words in both poems:\", length_common)" ] }, { @@ -193,7 +545,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +554,73 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n", + " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "grades" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Philosophy score for Bob to 100.\n" + ] + } + ], + "source": [ + "#Update the score of a specific student in a specific subject, so the teacher can keep track of their progress.\n", + "def update_grade(student_name, subject, new_score):\n", + " if student_name in grades and subject in grades[student_name]:\n", + " grades[student_name][subject] = new_score\n", + " print(f\"\\nUpdated {subject} score for {student_name} to {new_score}.\")\n", + " else:\n", + " print(f\"\\n{student_name} or {subject} not found in the records.\")\n", + "update_grade('Bob', 'Philosophy', 100)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Grades:\n", + "Alice: {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n", + "Bob: {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}\n" + ] + } + ], + "source": [ + "#Print the updated grades for all students, so the teacher can see the changes made.\n", + "def print_grades():\n", + " print(\"\\nUpdated Grades:\")\n", + " for student, subjects in grades.items():\n", + " print(f\"{student}: {subjects}\")\n", + "print_grades()" ] }, { @@ -239,14 +653,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Grades Dictionary: {'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" + "# Convert the lists to a dictionary\n", + "grades = dict(zip(keys, values))\n", + "print(\"\\nGrades Dictionary:\", grades)" ] }, { @@ -256,6 +681,30 @@ "2. Get the subject with the minimum score from the previous dictionary." ] }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Subject with minimum score: Chemistry with score 60\n" + ] + } + ], + "source": [ + "#Get the subject with the minimum score from the previous dictionary\n", + "def get_min_subject(grades):\n", + " min_subject = min(grades, key=grades.get) # Get the subject with the minimum score\n", + " min_score = grades[min_subject]\n", + " print(f\"\\nSubject with minimum score: {min_subject} with score {min_score}\")\n", + " return min_subject, min_score # Return the subject and its score\n", + "min_subject, min_score = get_min_subject(grades)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -285,7 +734,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -299,7 +748,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,