From b51797ecef16d17b6eff7aeebe1efe336beb1830 Mon Sep 17 00:00:00 2001 From: nimsilvestre Date: Mon, 20 Oct 2025 08:41:01 +0100 Subject: [PATCH] Final fix and update the LAB file --- your-code/main.ipynb | 344 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 305 insertions(+), 39 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index de27676..98d4424 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -7,9 +7,14 @@ "## Words" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -25,11 +30,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PLAY\n", + "FILLING\n", + "BAR\n", + "THEATRE\n", + "EASYGOING\n", + "DATE\n", + "LEAD\n", + "THAT\n", + "STORY\n", + "ISLAND\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "for word in words:\n", + " print(word.upper())\n", + "\n", + "# I decided to use a simple for loop here instead of a list comprehension.\n", + "# It makes it easier to visualise how the transformation applies to each word.\n" ] }, { @@ -41,11 +69,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['filling', 'theatre', 'easygoing', 'story', 'island']\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "long_words = []\n", + "\n", + "for word in words:\n", + " if len(word) >= 5:\n", + " long_words.append(word)\n", + "\n", + "print(long_words)\n", + "\n", + "# It’s clearer this way — I can trace how each word gets tested.\n", + "# len(word) >= 5 feels intuitive and keeps the logic transparent.\n" ] }, { @@ -57,13 +104,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "theatre\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "first_t = None\n", + "\n", + "for word in words:\n", + " if word.lower().startswith('t'):\n", + " first_t = word\n", + " break # stop after finding the first one\n", + "\n", + "print(first_t)\n", + "\n", + "# The break here feels like a stop button — once I get what I want, I move on.\n", + "# It’s a clean way to avoid unnecessary iterations.\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reflexão\n", + "# --------------------------------------------------------\n", + "# O que aprendi:\n", + "# - Percorrer listas é a forma mais direta de perceber padrões em dados.\n", + "# - O break é útil quando o objectivo é encontrar “o primeiro” de algo.\n", + "# - Transformar strings com loops ajuda a interiorizar o fluxo do ciclo.\n", + "#\n", + "# Conceito-chave:\n", + "# - Controlar quando parar é tão importante como saber quando continuar.\n", + "#\n", + "# A minha nota:\n", + "# Trabalhar com palavras faz-me pensar em ritmos — o loop é o compasso,\n", + "# e a condição é o momento em que decides mudar de nota.\n", + "# --------------------------------------------------------\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,11 +172,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "squares = []\n", + "\n", + "for n in range(1, 11):\n", + " squares.append(n**2)\n", + "\n", + "print(squares)\n", + "\n", + "# I prefer using ** instead of pow() — it feels more readable for this purpose.\n", + "# It's a clear example of how repetition builds predictable patterns.\n" ] }, { @@ -96,11 +206,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 9, 25, 49, 81]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "odd_squares = []\n", + "\n", + "for n in range(1, 11):\n", + " if n % 2 != 0:\n", + " odd_squares.append(n**2)\n", + "\n", + "print(odd_squares)\n", + "\n", + "# The condition n % 2 != 0 filters odd numbers neatly.\n", + "# It's like choosing specific frequencies within a range — only the ones that resonate.\n" ] }, { @@ -112,11 +241,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[64, 256, 576, 1024, 1600, 2304, 3136, 4096, 5184, 6400, 7744, 9216, 10816, 12544, 14400, 16384, 18496, 20736, 23104, 25600, 28224, 30976, 33856, 36864, 40000, 43264, 46656, 50176, 53824, 57600, 61504, 65536, 69696, 73984, 78400, 82944, 87616, 92416, 97344, 102400, 107584, 112896, 118336, 123904, 129600, 135424, 141376, 147456, 153664, 160000, 166464, 173056, 179776, 186624, 193600, 200704, 207936, 215296, 222784, 230400, 238144, 246016, 254016, 262144, 270400, 278784, 287296, 295936, 304704, 313600, 322624, 331776, 341056, 350464, 360000, 369664, 379456, 389376, 399424, 409600, 419904, 430336, 440896, 451584, 462400, 473344, 484416, 495616, 506944, 518400, 529984, 541696, 553536, 565504, 577600, 589824, 602176, 614656, 627264, 640000, 652864, 665856, 678976, 692224, 705600, 719104, 732736, 746496, 760384, 774400, 788544, 802816, 817216, 831744, 846400, 861184, 876096, 891136, 906304, 921600, 937024, 952576, 968256, 984064]\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "multiples_of_8 = []\n", + "\n", + "for n in range(8, 1000, 8):\n", + " multiples_of_8.append(n**2)\n", + "\n", + "print(multiples_of_8)\n", + "\n", + "# The step in range(8, 1000, 8) eliminates the need for if conditions.\n", + "# It's efficient — like setting the metronome to hit every 8th beat.\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# your code here" + "# Reflexão\n", + "# --------------------------------------------------------\n", + "# O que aprendi:\n", + "# - Loops com range() são previsíveis e seguros quando se conhece o intervalo.\n", + "# - A clareza do código aumenta quando o padrão é explícito (como o passo 8 no range).\n", + "# - Condições simples tornam o raciocínio mais transparente.\n", + "#\n", + "# Conceito-chave:\n", + "# - Um loop é tanto um processo lógico como um ritmo mental.\n", + "#\n", + "# A minha nota:\n", + "# Estes exercícios lembram-me de engenharia de som — repetir, filtrar e ajustar.\n", + "# O resultado é previsível, mas a precisão está no detalhe do controlo.\n", + "# --------------------------------------------------------\n" ] }, { @@ -128,7 +295,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -170,11 +337,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of people: 5\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "n_people = len(people)\n", + "print(\"Number of people:\", n_people)\n", + "\n", + "# len() does all the work — no need to loop when counting top-level items.\n" ] }, { @@ -186,11 +366,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "People who have kids: 4\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "people_with_kids = 0\n", + "\n", + "for person in people:\n", + " if person[\"n_kids\"] > 0:\n", + " people_with_kids += 1\n", + "\n", + "print(\"People who have kids:\", people_with_kids)\n", + "\n", + "# This structure is simple and clear.\n", + "# It’s like scanning through a list of files and counting which ones have content.\n" ] }, { @@ -202,11 +401,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total number of kids: 10\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "total_kids = 0\n", + "\n", + "for person in people:\n", + " total_kids += person[\"n_kids\"]\n", + "\n", + "print(\"Total number of kids:\", total_kids)\n", + "\n", + "# Using += feels natural — accumulation through repetition.\n", + "# It's like summing data rows; repetition builds the full picture.\n" ] }, { @@ -218,17 +435,71 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'name': 'Juan', 'age': 34, 'n_kids': 2}, {'name': 'Pepe', 'age': 27, 'n_kids': 0}, {'name': 'Sonia', 'age': 41, 'n_kids': 2}, {'name': 'Lucía', 'age': 22, 'n_kids': 3}, {'name': 'Leo', 'age': 55, 'n_kids': 5}]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "\n", + "people_next_year = []\n", + "\n", + "for person in people:\n", + " # Make a copy of the current dictionary to avoid modifying the original\n", + " new_person = person.copy()\n", + " \n", + " # Check if the name ends with 'a' (case insensitive just in case)\n", + " if new_person[\"name\"].lower().endswith(\"a\"):\n", + " new_person[\"n_kids\"] += 1\n", + "\n", + " # Add this updated version to the new list\n", + " people_next_year.append(new_person)\n", + "\n", + "print(people_next_year)\n", + "\n", + "# I wanted to keep the original data untouched,\n", + "# so I used .copy() to create a shallow copy of each dictionary.\n", + "# The endswith() check feels intuitive — like scanning the last note of each name.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reflexão\n", + "# --------------------------------------------------------\n", + "# O que aprendi:\n", + "# - Ao criar cópias em loops, evito alterar dados originais sem querer.\n", + "# - Condições com strings (como endswith()) são ferramentas poderosas e discretas.\n", + "# - Gerar uma nova lista é uma forma de simular transformação no tempo.\n", + "#\n", + "# Conceito-chave:\n", + "# - Um loop pode representar evolução — uma iteração é uma mudança controlada.\n", + "#\n", + "# A minha nota:\n", + "# Este exercício fez-me pensar em herança de estado —\n", + "# como pequenos detalhes (como a última letra de um nome)\n", + "# podem alterar o curso de um conjunto inteiro de dados.\n", + "# É programação, mas também é observação de padrões humanos.\n", + "# --------------------------------------------------------\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -242,7 +513,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" }, "toc": { "base_numbering": 1, @@ -285,11 +556,6 @@ "_Feature" ], "window_display": false - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } } }, "nbformat": 4,