diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..349794e 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -59,9 +59,57 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "The total sum of the grades is: 19\n", + "New list (1st, 3rd, and 5th): [2, 4, 5]\n", + "Size of the new list: 3\n", + "Number of times 5 appears in the list: 1\n" + ] + } + ], "source": [ - "# Your code here" + "# grades_tracker.py\n", + "\n", + "def main():\n", + " num_students = 5\n", + " grades = []\n", + "\n", + " # Collect grades for 5 students\n", + " for i in range(1, num_students + 1):\n", + " while True:\n", + " try:\n", + " grade_input = input(f\"Enter the grade for student {i}: \")\n", + " grade = int(grade_input)\n", + " # Optional: validate range (e.g., 0-100)\n", + " if grade < 0:\n", + " print(\"Grade cannot be negative. Please try again.\")\n", + " continue\n", + " grades.append(grade)\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter an integer.\")\n", + "\n", + " # Total sum of grades\n", + " total_sum = sum(grades)\n", + " print(f\"\\nThe total sum of the grades is: {total_sum}\")\n", + "\n", + " # New list with 1st, 3rd, and 5th students (indexes 0, 2, 4) and sort ascending\n", + " selected = grades[0:5:2] # 0->2->4, given there are 5 elements\n", + " selected.sort()\n", + "\n", + " # Print requested results\n", + " print(f\"New list (1st, 3rd, and 5th): {selected}\")\n", + " print(f\"Size of the new list: {len(selected)}\")\n", + " count_fives = selected.count(5)\n", + " print(f\"Number of times 5 appears in the list: {count_fives}\")\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()\n" ] }, { @@ -95,11 +143,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original tuple: ('apple', 'banana', 'orange', 'grape', 'mango')\n", + "First fruit: apple\n", + "Last fruit: mango\n", + "After replacing second element: ('apple', 'strawberry', 'orange', 'grape', 'mango')\n", + "After concatenation with 2 more fruits: ('apple', 'strawberry', 'orange', 'grape', 'mango', 'pineapple', 'peach')\n", + "First three: ('apple', 'strawberry', 'orange')\n", + "Last three: ('grape', 'mango', 'pineapple')\n", + "Combined tuple: ('apple', 'strawberry', 'orange', 'grape', 'mango', 'pineapple', 'apple', 'banana', 'orange', 'grape', 'mango')\n", + "Length of combined tuple: 11\n" + ] + } + ], "source": [ - "# Your code here" + "# fruit_inventory.py\n", + "\n", + "def main():\n", + " # 1) Inicializa una tupla con 5 tipos de fruta\n", + " fruits = (\"apple\", \"banana\", \"orange\", \"grape\", \"mango\")\n", + " print(\"Original tuple:\", fruits)\n", + "\n", + " # 2) Output the first and last elements\n", + " first_fruit = fruits[0]\n", + " last_fruit = fruits[-1]\n", + " print(\"First fruit:\", first_fruit)\n", + " print(\"Last fruit:\", last_fruit)\n", + "\n", + " # 3) Replace the second element with a new fruit \n", + " # Second element index is 1\n", + " updated_fruits = fruits[:1] + (\"strawberry\",) + fruits[2:]\n", + " print(\"After replacing second element:\", updated_fruits)\n", + "\n", + " # 4) Concatenate a new tuple containing 2 additional fruits\n", + " extra_fruits = (\"pineapple\", \"peach\")\n", + " concatenated = updated_fruits + extra_fruits\n", + " print(\"After concatenation with 2 more fruits:\", concatenated)\n", + "\n", + " # 5) Split the resulting tuple into 2 tuples of 3 elements each\n", + " # (first 3 elements) and (last 3 elements)\n", + " first_three = concatenated[:3]\n", + " last_three = concatenated[3:6]\n", + " print(\"First three:\", first_three)\n", + " print(\"Last three:\", last_three)\n", + "\n", + " # 6) Combine the 2 tuples from step 5 with the original tuple into a new tuple\n", + " combined = first_three + last_three + fruits\n", + " print(\"Combined tuple:\", combined)\n", + " print(\"Length of combined tuple:\", len(combined))\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()\n" ] }, { @@ -163,11 +263,81 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique words in poem 1: 41\n", + "Unique words in poem 2: 42\n", + "\n", + "Words in poem 1 but not in poem 2:\n", + "['also', 'desire', 'destruction', 'favor', 'fire', 'for', 'great', 'hate', 'hold', 'ice', 'in', 'i’ve', 'perish', 'suffice', 'tasted', 'the', 'twice', 'will', 'world', 'would']\n", + "\n", + "Words in poem 2 but not in poem 1:\n", + "['a', 'are', 'as', 'away', 'deem', 'dream', 'fades', 'life', 'love', 'made', 'quest', 's', 'see', 'seen', 'side', 'still', 'test', 'though', 'today', 've', 'we']\n", + "\n", + "Words common to both poems (alphabetical):\n", + "['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_sets_simple.py\n", + "\n", + "import string\n", + "\n", + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "new_poem = \"\"\"Some say life is but a dream,\n", + "Some say it's a test.\n", + "From what I've seen and what I deem,\n", + "I side with those who see it as a quest.\n", + "\n", + "But if it had to end today,\n", + "I think I know enough of love,\n", + "To say that though it fades away,\n", + "It's still what we are made of.\"\"\"\n", + "\n", + "def words_set(text):\n", + " # Pasar a minúsculas y quitar puntuación simple\n", + " text = text.lower()\n", + " for ch in string.punctuation:\n", + " text = text.replace(ch, \" \")\n", + " return set(text.split())\n", + "\n", + "def main():\n", + " s1 = words_set(poem)\n", + " s2 = words_set(new_poem)\n", + "\n", + " print(\"Unique words in poem 1:\", len(s1))\n", + " print(\"Unique words in poem 2:\", len(s2))\n", + "\n", + " only1 = sorted(s1 - s2)\n", + " only2 = sorted(s2 - s1)\n", + " both = sorted(s1 & s2)\n", + "\n", + " print(\"\\nWords in poem 1 but not in poem 2:\")\n", + " print(only1)\n", + "\n", + " print(\"\\nWords in poem 2 but not in poem 1:\")\n", + " print(only2)\n", + "\n", + " print(\"\\nWords common to both poems (alphabetical):\")\n", + " print(both)\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" ] }, { @@ -202,11 +372,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100\n" + ] + } + ], "source": [ - "# Your code here" + "## grades dictionary\n", + "grades = {\n", + " 'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n", + " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n", + "}\n", + "\n", + "# Update Bob's Philosophy score to 100\n", + "grades['Bob']['Philosophy'] = 100\n", + "\n", + "# Optional: print to verify\n", + "print(grades['Bob']['Philosophy']) # 100\n" ] }, { @@ -239,14 +427,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" + ] + } + ], "source": [ + "# lists\n", "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", - "values = [75, 85, 60,90]\n", + "values = [75, 85, 60, 90]\n", "\n", - "# Your code here" + "# Convertir a diccionario\n", + "grades_dict = dict(zip(keys, values))\n", + "\n", + "print(grades_dict)\n", + "# {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n" ] }, { @@ -275,17 +476,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Subject with minimum score: Chemistry\n", + "Minimum score: 60\n" + ] + } + ], "source": [ - "# Your code here" + "min_subject = min(grades_dict, key=grades_dict.get)\n", + "min_score = grades_dict[min_subject]\n", + "\n", + "print(\"Subject with minimum score:\", min_subject)\n", + "print(\"Minimum score:\", min_score)\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -299,7 +514,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.0" } }, "nbformat": 4,