From bd8fd1534ae357e947387f91c10120738539ccbe Mon Sep 17 00:00:00 2001 From: Juan Boberg Aguirre Date: Thu, 11 Jun 2026 16:06:07 +0200 Subject: [PATCH] Update lab-python-data-structures.ipynb --- lab-python-data-structures.ipynb | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..f141bb7 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -60,9 +60,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here" - ] + "source": "# Exercise 1: Working with Lists\n\n# Input grades for 5 students\ngrades = []\nfor i in range(1, 6):\n grade = int(input(f\"Enter grade for student {i}: \"))\n grades.append(grade)\n\n# Total sum of all grades\nprint(f\"Total sum of grades: {sum(grades)}\")\n\n# Select 1st, 3rd, 5th students (indices 0, 2, 4) using slicing, sort ascending\nselected = sorted(grades[0:5:2])\n\nprint(f\"Selected grades (sorted): {selected}\")\nprint(f\"Length of selected list: {len(selected)}\")\nprint(f\"Occurrences of score 5: {selected.count(5)}\")" }, { "cell_type": "markdown", @@ -98,9 +96,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here" - ] + "source": "# Exercise 2: Tuples\n\n# Initialize tuple with 5 fruits\nfruits = (\"apple\", \"banana\", \"cherry\", \"mango\", \"grape\")\n\n# First and last elements\nprint(f\"First fruit: {fruits[0]}, Last fruit: {fruits[-1]}\")\n\n# Replace second element (tuples are immutable — convert via list)\nfruits_list = list(fruits)\nfruits_list[1] = \"strawberry\"\nfruits = tuple(fruits_list)\nprint(f\"Updated tuple: {fruits}\")\n\n# Concatenate 2 additional fruits\nextra_fruits = (\"kiwi\", \"watermelon\")\nextended_fruits = fruits + extra_fruits\nprint(f\"Extended inventory: {extended_fruits}\")\n\n# Split into 2 tuples of 3 elements each\nfirst_half = extended_fruits[:3]\nsecond_half = extended_fruits[-3:]\nprint(f\"First half: {first_half}\")\nprint(f\"Second half: {second_half}\")\n\n# Combine both halves + original tuple\nfinal_tuple = first_half + second_half + fruits\nprint(f\"Final inventory: {final_tuple}\")\nprint(f\"Length of final tuple: {len(final_tuple)}\")" }, { "cell_type": "markdown", @@ -166,9 +162,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here" - ] + "source": "# Exercise 3: Sets\nimport string\n\ndef words_in_poem(poem):\n # Remove punctuation and convert to lowercase\n translator = str.maketrans(\"\", \"\", string.punctuation)\n return set(poem.lower().translate(translator).split())\n\nset1 = words_in_poem(poem)\nset2 = words_in_poem(new_poem)\n\nprint(f\"Unique words in poem 1: {len(set1)}\")\nprint(f\"Unique words in poem 2: {len(set2)}\")\n\nprint(f\"\\nWords only in poem 1: {set1 - set2}\")\nprint(f\"\\nWords only in poem 2: {set2 - set1}\")\n\ncommon = sorted(set1 & set2)\nprint(f\"\\nWords in both poems (alphabetical): {common}\")" }, { "cell_type": "markdown", @@ -205,9 +199,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here" - ] + "source": "# Exercise 4: Dictionaries\n\n# Update Bob's Philosophy score to 100\ngrades['Bob']['Philosophy'] = 100\n\nprint(grades)" }, { "cell_type": "markdown", @@ -242,12 +234,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", - "values = [75, 85, 60,90]\n", - "\n", - "# Your code here" - ] + "source": "# Bonus 1: Convert two lists into a dictionary using zip()\n\nkeys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\nvalues = [75, 85, 60, 90]\n\nsubject_scores = dict(zip(keys, values))\nprint(subject_scores)" }, { "cell_type": "markdown", @@ -278,9 +265,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here" - ] + "source": "# Bonus 2: Get the subject with the minimum score\n\nmin_subject = min(subject_scores, key=subject_scores.get)\nprint(f\"Subject with minimum score: {min_subject} ({subject_scores[min_subject]})\")" } ], "metadata": { @@ -304,4 +289,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file