diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5a108c3 Binary files /dev/null and b/.DS_Store differ diff --git a/your-code/challenges.ipynb b/your-code/challenges.ipynb index ba91b3f..7138e5a 100644 --- a/your-code/challenges.ipynb +++ b/your-code/challenges.ipynb @@ -13,11 +13,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "#only one element, we must use a comma\n", + "tup = (\"I\",) " ] }, { @@ -31,11 +34,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "print(type(tup)) # expected output: " ] }, { @@ -61,7 +73,13 @@ "source": [ "# Your code here\n", "\n", + "letters = (\"r\", \"o\", \"n\", \"h\", \"h\", \"a\", \"c\", \"k\", \"k\")\n", + "\n", "# Your explanation here\n", + "\n", + "# I couldn't really append them using .append() because tuples don't have that method\n", + "# I just created a new one by concatenating the old tuple with the new elements\n", + "\n", "# You can :) " ] }, @@ -84,7 +102,17 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# Tuples are immutable, so this will not work.\n", + "\n", + "try:\n", + " tup[0] = \"X\"\n", + "except TypeError as e:\n", + " print(\"Oops:\", e)\n", + "\n", + "# As expected, tuples are immutable — I can’t change an element once it’s created\n", + "# If I need a mutable version, I’d have to convert it to a list" ] }, { @@ -102,11 +130,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Oops: 'tuple' object does not support item assignment\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# Tuples are immutable, so this will not work.\n", + "try:\n", + " tup[0] = \"X\"\n", + "except TypeError as e:\n", + " print(\"Oops:\", e)" ] }, { @@ -120,11 +162,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Iron', 'hack')\n", + "('I',)\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# create two tuples to combine\n", + "\n", + "tup1 = (\"Iron\",)\n", + "tup2 = (\"hack\",)\n", + "\n", + "# Add tup1 and tup2 into tup3 using the + operator\n", + "tup3 = tup1 + tup2\n", + "\n", + "print(tup3)\n", + "# Check if tup3 equals to tup (probably not)\n", + "\n", + "print(tup)\n", + "# Makes sense, because tup was (\"I\",) and tup3 is (\"Iron\", \"hack\")" ] }, { @@ -136,11 +201,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "len_tup1 + len_tup2 = 2\n", + "len_tup3 = 2\n", + "Are they equal? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "len_tup1 = len(tup1)\n", + "len_tup2 = len(tup2)\n", + "len_tup3 = len(tup3)\n", + "\n", + "# Count the number of elements in tup1 and tup2 and compare with tup3\n", + "\n", + "print(\"len_tup1 + len_tup2 =\", len_tup1 + len_tup2)\n", + "print(\"len_tup3 =\", len_tup3)\n", + "print(\"Are they equal?\", len_tup1 + len_tup2 == len_tup3)" ] }, { @@ -152,11 +237,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter 'h' is not directly inside tup3 (only in the string 'hack')\n", + "Index of 'h' inside 'hack' is: 0\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "try:\n", + " index_h = tup3.index(\"h\")\n", + "except ValueError:\n", + " print(\"Letter 'h' is not directly inside tup3 (only in the string 'hack')\")\n", + " # So we can check inside the second element\n", + " index_h = tup3[1].index(\"h\")\n", + "\n", + "print(\"Index of 'h' inside 'hack' is:\", index_h)" ] }, { @@ -176,11 +279,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a False\n", + "b False\n", + "c False\n", + "d False\n", + "e False\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " print(letter, letter in tup3) \n", + "\n", + "# expected False for all, because 'a' etc are inside the string, not tuple items" ] }, { @@ -203,11 +325,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter 'a' appears 1 time(s) inside the word 'hack'\n", + "Letter 'b' appears 0 time(s) inside the word 'hack'\n", + "Letter 'c' appears 1 time(s) inside the word 'hack'\n", + "Letter 'd' appears 0 time(s) inside the word 'hack'\n", + "Letter 'e' appears 0 time(s) inside the word 'hack'\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "for letter in letters:\n", + " count = tup3[1].count(letter)\n", + " print(f\"Letter '{letter}' appears {count} time(s) inside the word 'hack'\")" ] }, { @@ -223,7 +361,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -248,11 +386,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[7, 20, 12, 44, 29, 23, 81, 30, 15, 1, 56, 11, 41, 64, 53, 60, 10, 58, 76, 49, 0, 92, 86, 96, 78, 73, 85, 59, 98, 88, 42, 99, 63, 39, 79, 14, 52, 55, 54, 36, 50, 31, 27, 100, 8, 65, 6, 16, 71, 18, 66, 26, 4, 93, 2, 77, 80, 47, 89, 94, 25, 72, 70, 43, 68, 28, 13, 48, 21, 97, 32, 9, 57, 22, 45, 5, 46, 24, 33, 67]\n", + "Length of list: 80\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# I'll use random.sample() because it returns unique elements by default\n", + "sample_list_1 = random.sample(range(0, 101), 80)\n", + "\n", + "print(sample_list_1)\n", + "print(\"Length of list:\", len(sample_list_1)) # should be 80" ] }, { @@ -264,11 +417,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set1: 80\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "set1 = set(sample_list_1)\n", + "print(\"Length of set1:\", len(set1))\n", + "\n", + "# It should still be 80 because all numbers from random.sample() are unique.\n", + "# If there were duplicates, the length would be smaller.\n" ] }, { @@ -287,11 +454,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 43, 17, 42, 48, 63, 4, 91, 79, 33, 76, 24, 12, 3, 85, 77, 74, 45, 68, 98, 4, 39, 28, 83, 93, 80, 35, 94, 56, 16, 30, 64, 59, 0, 29, 4, 32, 13, 69, 37, 89, 20, 15, 94, 69, 63, 26, 9, 58, 84, 93, 75, 27, 36, 44, 11, 30, 61, 50, 18, 2, 19, 90, 89, 52, 14, 45, 20, 44, 55, 43, 31, 49, 61, 16, 2, 31, 12, 1, 16]\n", + "Length of list2: 80\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "# I'll use random.choices this time (it allows duplicates)\n", + "sample_list_2 = random.choices(range(0, 101), k=80)\n", + "print(sample_list_2)\n", + "print(\"Length of list2:\", len(sample_list_2))\n" ] }, { @@ -303,11 +484,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Length of set2: 62\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "set2 = set(sample_list_2)\n", + "print(\"Length of set2:\", len(set2))\n", + "\n", + "# Probably smaller now, because choices() can repeat numbers.\n", + "# Sets automatically remove duplicates.\n" ] }, { @@ -321,9 +516,25 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Common elements between set1 and set2: {0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13, 14, 18, 20, 23, 24, 25, 26, 27, 31, 33, 36, 42, 43, 45, 52, 53, 55, 56, 59, 63, 65, 67, 68, 70, 71, 73, 77, 78, 79, 85, 86, 93, 94, 100}\n", + "Number of elements in common: 45\n", + "Total unique elements in both sets: 94\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "set3 = set1.difference(set2)\n", + "print(\"Elements in set1 but not in set2:\", set3)\n", + "print(\"Length of set3:\", len(set3))\n", + "\n", + "# difference() keeps only elements unique to set1.\n" ] }, { @@ -335,11 +546,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements in set2 but not in set1: {3, 35, 37, 69, 74, 75, 17, 19, 83, 84, 90, 91, 61}\n", + "Length of set4: 13\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "set4 = set2.difference(set1)\n", + "print(\"Elements in set2 but not in set1:\", set4)\n", + "print(\"Length of set4:\", len(set4))\n" ] }, { @@ -351,11 +575,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements shared between set1 and set2: {0, 1, 2, 4, 5, 9, 11, 12, 13, 14, 15, 16, 18, 20, 24, 26, 27, 28, 29, 30, 31, 32, 33, 36, 39, 42, 43, 44, 45, 48, 49, 50, 52, 55, 56, 58, 59, 63, 64, 68, 76, 77, 79, 80, 85, 89, 93, 94, 98}\n", + "Length of set5: 49\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "set5 = set1.intersection(set2)\n", + "print(\"Elements shared between set1 and set2:\", set5)\n", + "print(\"Length of set5:\", len(set5))\n" ] }, { @@ -375,11 +612,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "len(set1): 80\n", + "len(set2): 62\n", + "len(set3): 31\n", + "len(set4): 13\n", + "len(set5): 49\n", + "Does the formula hold? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# If not already defined, let's define them again to avoid the NameError:\n", + "set3 = set1.difference(set2)\n", + "set4 = set2.difference(set1)\n", + "set5 = set1.intersection(set2)\n", + "\n", + "# Now print the lengths of all sets\n", + "print(\"len(set1):\", len(set1))\n", + "print(\"len(set2):\", len(set2))\n", + "print(\"len(set3):\", len(set3))\n", + "print(\"len(set4):\", len(set4))\n", + "print(\"len(set5):\", len(set5))\n", + "\n", + "# Relationship idea:\n", + "# len(set1) + len(set2) = len(set3) + len(set4) + 2 * len(set5)\n", + "# This makes sense because elements in the intersection are counted twice.\n", + "check_formula = len(set1) + len(set2) == len(set3) + len(set4) + 2 * len(set5)\n", + "print(\"Does the formula hold?\", check_formula)\n" ] }, { @@ -391,11 +659,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "An empty set was created: set()\n", + "Type check: \n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "set6 = set()\n", + "\n", + "print(\"An empty set was created:\", set6)\n", + "print(\"Type check:\", type(set6))\n", + "\n", + "# Just a small reminder to myself:\n", + "# Using {} creates a dictionary, not a set.\n", + "# So we need to use set() to make it a real empty set.\n" ] }, { @@ -407,11 +693,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "After updating, set6 now contains: {0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 36, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 76, 77, 78, 79, 80, 81, 85, 86, 88, 89, 92, 93, 94, 96, 97, 98, 99, 100}\n", + "Length of set6: 80\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "set6.update(set3, set5)\n", + "\n", + "print(\"After updating, set6 now contains:\", set6)\n", + "print(\"Length of set6:\", len(set6))\n", + "\n", + "# Quick note:\n", + "# The update() method merges multiple sets into one.\n", + "# It doesn’t return anything (it changes the set directly).\n" ] }, { @@ -423,11 +727,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Are set1 and set6 equal? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "print(\"Are set1 and set6 equal?\", set1 == set6)\n", + "\n", + "# It should be True if the union of set3 and set5 equals all elements of set1.\n", + "# But sometimes it might be False depending on the random values generated before.\n" ] }, { @@ -439,11 +756,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Is set1 a subset of set2? False\n", + "Is set1 a subset of set3? False\n", + "Is set3 a subset of set1? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# Check subset relationships\n", + "print(\"Is set1 a subset of set2?\", set1.issubset(set2))\n", + "print(\"Is set1 a subset of set3?\", set1.issubset(set3))\n", + "print(\"Is set3 a subset of set1?\", set3.issubset(set1))\n", + "\n", + "# Just exploring relationships to understand how issubset() works.\n" ] }, { @@ -457,11 +791,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Are the aggregated sets equal? True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# Aggregate the smaller sets and compare with the big ones\n", + "agg_small = set3.union(set4, set5)\n", + "agg_big = set1.union(set2)\n", + "\n", + "print(\"Are the aggregated sets equal?\", agg_small == agg_big)\n", + "\n", + "# They should be equal because set3, set4, and set5 together \n", + "# represent all unique elements from both set1 and set2.\n" ] }, { @@ -473,11 +824,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Removed element: 0\n", + "Set1 after pop(): {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 36, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 76, 77, 78, 79, 80, 81, 85, 86, 88, 89, 92, 93, 94, 96, 97, 98, 99, 100}\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# Remove a random element from set1\n", + "removed_element = set1.pop()\n", + "\n", + "print(\"Removed element:\", removed_element)\n", + "print(\"Set1 after pop():\", set1)\n", + "\n", + "# Note: pop() removes a random element, since sets have no order.\n", + "# So the 'first' element doesn’t really exist — it’s just any element.\n" ] }, { @@ -497,9 +866,44 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "# Remove a random element from set1\n", + "removed_element = set1.pop()\n", + "\n", + "print(\"Removed element:\", removed_element)\n", + "print(\"Set1 after pop():\", set1)\n", + "\n", + "# Note: pop() removes a random element, since sets have no order.\n", + "# So the 'first' element doesn’t really exist — it’s just any element.\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reflexão\n", + "# --------------------------------------------------------\n", + "# O que aprendi:\n", + "# - Os sets removem automaticamente duplicados e tornam as comparações mais rápidas.\n", + "# - Métodos como union(), intersection() e difference() ajudam a compreender relações entre dados.\n", + "# - update() altera o set existente em vez de criar um novo, o que é útil em operações de junção.\n", + "#\n", + "# Conceito-chave:\n", + "# - Os sets definem-se pela unicidade, não pela ordem.\n", + "# - O método pop() remove um elemento aleatório, o que reforça essa falta de sequência.\n", + "#\n", + "# A minha nota:\n", + "# Trabalhar com sets é um pouco como comprimir memória — ficamos só com o essencial e eliminamos o que está a mais.\n", + "# É eficiente, mas abdicas do controlo da ordem, e às vezes é esse o preço da clareza.\n", + "# --------------------------------------------------------\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -515,7 +919,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ @@ -551,7 +955,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -592,7 +996,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -602,7 +1006,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -616,12 +1020,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.13.5" } }, "nbformat": 4,