diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..73767f8 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -29,7 +29,7 @@ { "cell_type": "code", "execution_count": null, - "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", + "id": "21b0002d-61af-4592-89c1-fe0ce8ba1f8a", "metadata": {}, "outputs": [], "source": [ @@ -43,7 +43,14 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + "\n", + " unique = []\n", + " for item in lst:\n", + " if item not in unique:\n", + " unique.append(item) \n", + " return unique \n", + "\n", + "print(get_unique_list([1, 2, 2, 3, 4, 3, 3, 5, 7, 8, 8, 8, 9, 1, 4]))\n" ] }, { @@ -60,10 +67,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2, Lowercase count: 8\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +90,115 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " \n", + " upper = sum(1 for char in string if char.isupper()) \n", + " lower = sum(1 for char in string if char.islower()) \n", + " return upper, lower \n", + "\n", + "text = \"Hello World\" \n", + "upper, lower = count_case(text) \n", + "print(f\"Uppercase count: {upper}, Lowercase count: {lower}\") \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e58b11e0-f467-4a4a-a1cd-aa82cdf4ef84", + "metadata": {}, + "outputs": [], + "source": [ + "# private notes: \n", + "\n", + "def count_case(string):\n", + "Cria uma função chamada count_case que recebe um argumento:\n", + "string → o texto onde vamos contar letras maiúsculas e minúsculas.\n", + "\n", + "# Contar letras maiúsculas \n", + "upper = sum(1 for char in string if char.isupper())\n", + "\n", + "\n", + "Isto faz várias coisas ao mesmo tempo:\n", + "\n", + "Percorre cada caractere da string (for char in string)\n", + "\n", + "Verifica se é maiúscula (char.isupper())\n", + "\n", + "Se for maiúscula, gera um 1\n", + "\n", + "O sum(...) soma todos esses 1’s\n", + "\n", + "Exemplo com \"Hello World\":\n", + "\n", + "H → maiúscula → conta 1\n", + "\n", + "e → minúscula → ignora\n", + "\n", + "l → minúscula → ignora\n", + "\n", + "l → minúscula → ignora\n", + "\n", + "o → minúscula → ignora\n", + "\n", + "espaço → ignora\n", + "\n", + "W → maiúscula → conta 1\n", + "\n", + "resto → ignora\n", + "\n", + "Resultado:\n", + "upper = 2\n", + "\n", + "# Contar letras minúsculas \n", + "\n", + "lower = sum(1 for char in string if char.islower())\n", + "Mesma lógica, mas agora:\n", + "\n", + "char.islower() verifica se é minúscula\n", + "\n", + "Cada minúscula gera um 1\n", + "\n", + "O sum() soma tudo\n", + "\n", + "Em \"Hello World\":\n", + "\n", + "e, l, l, o, o, r, l, d → 8 minúsculas\n", + "\n", + "Resultado:\n", + "lower = 8\n", + "\n", + "# Devolver os dois valores\n", + "return upper, lower\n", + "A função devolve um tuplo com os dois números:\n", + "(2, 8)\n", + "\n", + "# Usar a função \n", + "\n", + "text = \"Hello World\"\n", + "upper, lower = count_case(text)\n", + "\n", + "Aqui:\n", + "\n", + "Chamamos a função com \"Hello World\"\n", + "\n", + "Recebemos dois valores e guardamos em upper e lower\n", + "\n", + "# Imprimir o resultado \n", + "print(f\"Uppercase count: {upper}, Lowercase count: {lower}\")\n", + "\n", + "Uppercase count: 2, Lowercase count: 8\n", + "\n", + "Resumo rápido\n", + "A função percorre cada caractere da string.\n", + "\n", + "Conta quantos são maiúsculos (isupper()).\n", + "\n", + "Conta quantos são minúsculos (islower()).\n", + "\n", + "Devolve os dois valores.\n", + "\n", + "O código final imprime o resultado formatado.\n", + "\n" ] }, { @@ -92,10 +215,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello guys How are you today\n", + "6\n" + ] + } + ], "source": [ "import string\n", "\n", @@ -109,7 +241,14 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " pontuaction = \",.!?):\"\n", + " return \"\".join(char for char in sentence if char not in string.punctuation)\n", + "\n", + "text = \"Hello, guys! How are you today?\"\n", + "clean = remove_punctuation(text)\n", + "print(clean) \n", + "\n", + " \n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +261,11 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " cleaned = remove_punctuation(sentence)\n", + " words = cleaned.split()\n", + " return len(words) \n", + "\n", + "print(word_count(clean)) " ] }, { @@ -140,12 +283,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note this is an example Good day \n", + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "text = \"Note : this is an example !!! Good day : )\"\n", + "clean = remove_punctuation(text)\n", + "print(clean) \n", + "print(word_count(clean)) " ] }, { @@ -166,14 +321,230 @@ "- Test your \"calculate\" function by calling it with different input parameters.\n" ] }, + { + "cell_type": "code", + "execution_count": 28, + "id": "c8cf494b-4eb9-4ce9-803f-f906e096adcc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n", + "478\n", + "5022.0\n" + ] + } + ], + "source": [ + "def addition(a, b):\n", + " return a + b\n", + "\n", + "def subtraction(a, b):\n", + " return a - b\n", + "\n", + "def multiplication(a, b):\n", + " return a * b\n", + "\n", + "def division(a, b):\n", + " return a / b\n", + "\n", + "def calculate(a, b, op):\n", + " if op == \"+\":\n", + " return addition(a, b)\n", + " elif op == \"-\":\n", + " return subtraction(a, b)\n", + " elif op == \"*\":\n", + " return multiplication(a, b)\n", + " elif op == \"/\":\n", + " return division(a, b)\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + "print(calculate(10, 5, \"+\")) \n", + "print(calculate(10, 5, \"-\")) \n", + "print(calculate(10, 5, \"*\")) \n", + "print(calculate(10, 5, \"/\")) \n", + "print(calculate(133, 345, \"+\")) \n", + "print(calculate(10044, 2, \"/\")) \n" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", + "id": "3a0152bb-8075-4dcf-9d2a-3134f4cf2709", "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9f4f3d0-7637-405b-b6cf-df805fd0574e", + "metadata": {}, + "outputs": [], + "source": [ + "# bonus, a calculator with input function " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Calculator ---\n", + "1. Addition\n", + "2. Subtraction\n", + "3. Multiplication\n", + "4. Division\n", + "5. Exit\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose an option: 1\n", + "First number: 2\n", + "Second number: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The result is: 4.0\n", + "\n", + "--- Calculator ---\n", + "1. Addition\n", + "2. Subtraction\n", + "3. Multiplication\n", + "4. Division\n", + "5. Exit\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose an option: 4\n", + "First number: 6\n", + "Second number: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The result is: 2.0\n", + "\n", + "--- Calculator ---\n", + "1. Addition\n", + "2. Subtraction\n", + "3. Multiplication\n", + "4. Division\n", + "5. Exit\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose an option: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid option\n", + "\n", + "--- Calculator ---\n", + "1. Addition\n", + "2. Subtraction\n", + "3. Multiplication\n", + "4. Division\n", + "5. Exit\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose an option: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you. Good bye\n" + ] + } + ], "source": [ - "# your code goes here" + "def addition():\n", + " a = float(input(\"First number: \"))\n", + " b = float(input(\"Second number: \")) \n", + " print (f\"The result is: {a + b}\") \n", + "\n", + "def subtraction():\n", + " a = float(input(\"First number: \"))\n", + " b = float(input(\"Second number: \")) \n", + " print (f\"The result is: {a - b}\") \n", + "\n", + "def multiplication():\n", + " a = float(input(\"First number: \"))\n", + " b = float(input(\"Second number: \"))\n", + " print (f\"The result is: {a * b}\") \n", + "\n", + "def division():\n", + " a = float(input(\"First number: \"))\n", + " b = float(input(\"Second number: \"))\n", + " if b == 0:\n", + " print(\"Error: division by zero not allowed\")\n", + " else:\n", + " print (f\"The result is: {a / b}\") \n", + "\n", + "def calculator(): \n", + " print(\"\\n--- Calculator ---\") \n", + " print(\"1. Addition\")\n", + " print(\"2. Subtraction\")\n", + " print(\"3. Multiplication\")\n", + " print(\"4. Division\")\n", + " print(\"5. Exit\")\n", + "\n", + " choice = input(\"Choose an option: \") \n", + " if choice == \"1\":\n", + " addition()\n", + " elif choice == \"2\":\n", + " subtraction()\n", + " elif choice == \"3\":\n", + " multiplication()\n", + " elif choice == \"4\":\n", + " division()\n", + " elif choice == \"5\":\n", + " print(\"Thank you. Good bye\") \n", + " return\n", + " else:\n", + " print(\"Invalid option\") \n", + " calculator() \n", + "\n", + "calculator() \n", + " \n", + " \n", + "\n", + "\n" ] }, { @@ -192,12 +563,116 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-9\n" + ] + } + ], "source": [ - "# your code goes here" + "# using args \n", + "\n", + "def addition(*args):\n", + " result = 0\n", + " for num in args:\n", + " result += num\n", + " return result\n", + " \n", + "\n", + "def subtraction(*args):\n", + " if not args:\n", + " return 0\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "\n", + "def multiplying(*args):\n", + " if not args:\n", + " return 0\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result *= num\n", + " return result\n", + "\n", + "\n", + "def division(*args):\n", + " if not args:\n", + " return 0\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result /= num\n", + " return result\n", + "\n", + "\n", + "\n", + "\n", + "print(subtraction(10, 2, 5, 12))\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7f34997c-fefc-4cfc-bda7-93dc0b821757", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60\n" + ] + } + ], + "source": [ + "# using kwargs \n", + "\n", + "def calculate(operation, **kwargs):\n", + " numbers = kwargs.values()\n", + "\n", + " if operation == \"add\":\n", + " result = 0\n", + " for n in numbers:\n", + " result += n\n", + " return result\n", + "\n", + " elif operation == \"multiply\":\n", + " result = 1\n", + " for n in numbers:\n", + " result *= n\n", + " return result\n", + "\n", + " elif operation == \"subtract\":\n", + " nums = list(numbers)\n", + " result = nums[0]\n", + " for n in nums[1:]:\n", + " result -= n\n", + " return result\n", + "\n", + " elif operation == \"divide\":\n", + " nums = list(numbers)\n", + " result = nums[0]\n", + " for n in nums[1:]:\n", + " result /= n\n", + " return result\n", + "\n", + " else:\n", + " return \"Unknown operation\"\n", + "\n", + "\n", + "\n", + "\n", + "print(calculate(\"multiply\", x=2, y=3, z=10))" ] }, { @@ -273,16 +748,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, + "id": "4bebed16-76fd-4285-84a1-ac0291e2c2d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['.git', '.ipynb_checkpoints', 'functions.py', 'lab-python-functions.ipynb', 'README.md', '__pycache__']\n" + ] + } + ], + "source": [ + "import os\n", + "print(os.listdir())" + ] + }, + { + "cell_type": "code", + "execution_count": 6, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n", + "11\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", + "import string\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "\n", + "\n", + "from functions import word_count_f\n", + "\n", + "text = \"Note : this is an example !!! Good day : )\" \n", + "print(word_count_f(text))\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c796755e-a3ea-40ee-9c3f-9979e7afbc59", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello guys How are you today\n" + ] + } + ], + "source": [ + "from functions import remove_punctuation_f\n", + "\n", + "text = \"Hello, guys! How are you today?\"\n", + "clean = remove_punctuation_f(text)\n", + "print(clean) \n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, { @@ -315,13 +853,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], "source": [ - "# your code goes here" + "def fibonacci(n):\n", + " if n <= 1:\n", + " return n\n", + " return fibonacci(n - 1) + fibonacci(n - 2)\n", + "\n", + "def fibonacci_sequence(n):\n", + " if n == 0:\n", + " return [0]\n", + " if n == 1:\n", + " return [0, 1]\n", + " \n", + " seq = fibonacci_sequence(n - 1)\n", + " seq.append(fibonacci(n))\n", + " return seq\n", + "\n", + "\n", + "print(fibonacci_sequence(14))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "284b9936-1eef-40c9-b54a-f5c03311ed4a", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -340,7 +910,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,