From 3e3eb60a9ca21488cbb1f8ae352f7fccbec48233 Mon Sep 17 00:00:00 2001 From: Raquel Marques Date: Sat, 19 Sep 2026 22:53:25 +0100 Subject: [PATCH] Done --- __pycache__/functions.cpython-314.pyc | Bin 0 -> 2395 bytes functions.py | 81 +++++ lab-python-functions.ipynb | 422 ++++++++++++++++++++++++-- 3 files changed, 482 insertions(+), 21 deletions(-) create mode 100644 __pycache__/functions.cpython-314.pyc create mode 100644 functions.py diff --git a/__pycache__/functions.cpython-314.pyc b/__pycache__/functions.cpython-314.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9347e323e4159e01667938613d60867fa71e6611 GIT binary patch literal 2395 zcmbVO%}*Og6rWvx0QQ>V4+sQGrfC9L0asOWDQZz^OZou?YEdIq7S`Y$!0i=A)1-y&rAYJ-Ado{(Ro~3+8bgp8b)?;yd4BKB+j+nDd%o41 zN+1~D{PWY7?{y<|BsSqtgT?6>ELy0Dt{@i`6?iLasHlFdx@s{3yGSt#?`Tnd9pz(H zB{YXQ3Qq*4(?#i_fE=ZS`q14GgdBDCwYNa3!|x+#K8a9CS?;uj-^3`XcLM1(G(U<^ z4;lqcI7!+4Ik7^vD8~ew26r)mm4yl0Xw<0%TT91`x(#Lt4U6vhT04fji5r&jRfA$` zQlZLmjoEeVZcxrVWdu7%J_7`1o4EB=cn-sC>oa2Xe$=|V?YZOrt$2{ zp4RCkuXu6dIMmYf5ibJAdEF3mMR@eO!a(7!&=|&EsXAT$oI_ycB7qMv@u!5r%*#6E zTdwVt4Q5-Lq`Fo9fHd*#vPssboTj^JTT``$Rdo&9;#2gV%SgGsFE~vvy+PfIe>w8x zN{s<1kVpW3170n3oI`z?)<;L#fxWJSWaepd^baj}sO26m-Cz3t!{4fP{&;@K(Gq(jqmEFG+LB?dxtww#FGHlmy1MZ(y38xY~ro9vFOzH|0K+>hb zhOtd8%w1+!8y8^W3-JWZ0Z;;oYvZcj09gL_8Q`)Fs9Eeb9Fu~rB zj1@rEWpEinKI+ANu@{p9FD5_Oiw9pTY8SAESr;rkO&(bhmjj^a15rObd=_336sg|U z+<}&A%^hV1_u_}i^uw9^GyBQmUz5WJTKdsv-+uC7?l4?DOQP)1aW6_`_TKp=I>>s! z%i`rY`9dC0R086-E{LO~mO<)|<&dO80n8n=auq?=U&Kh8MAY3s;j-)YwxAKy#Ez(r z1`!2<>x6A_JW;jlb;2hxK*O+gSkQY_lhiwOCb6i!Gn>56Cj@gK7iv}MMUVvI5GJz$ zmVmAtMum~c^EvZ2ARuKv0foM?AT%4eh*-@R!!IN}lvf#)3-Xd39|s)M9eVIIK{Vlx zy#l&CAR0gSIVNwY(qRu67yvyeS;R$gN(|%%S_?;+t9$W-Wcs_(e*gI6{_&^DaY+z^ zziHPWEC4xVhmR9T8w}HadDe^4Wx3rBN+@gsfCFV&M0a6?4B;r{Sol*(Xl9lyI3T(Jkj*d$VViAycl;(!-a-NEfD1(bxaV87xz7!2cjmNReUC@v0>1A7ao2KUM=)|0Oclr z=zfxYv$b%Ty|$md`8a#?Np`%o@K>tu(a4k3XzT8wmT4_Xy2;06XOm1O%R`1Eeg24~ zhL8R3x9hgvFzF0?6;6?j5dMUqpE^qxf1%Fy$%w33td{{mT@ Bi{StO literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..0451368 --- /dev/null +++ b/functions.py @@ -0,0 +1,81 @@ +def get_unique_list_f(lst): + """ + Takes a list as an argument and returns a new list with unique elements from the first list. + + Parameters: + lst (list): The input list. + + Returns: + list: A new list with unique elements from the input list. + """ + new_list = [] + + for element in lst: + if element in new_list: + continue + else: + new_list.append(element) + + return new_list + +def count_case_f(string): + """ + Returns the number of uppercase and lowercase letters in the given string. + + Parameters: + string (str): The string to count uppercase and lowercase letters in. + + Returns: + A tuple containing the count of uppercase and lowercase letters in the string. + """ + upper = 0 + lower = 0 + + for letter in string: + + if letter.isupper(): + upper += 1 + else: + lower += 1 + + return (upper,lower) + +import string + +def remove_punctuation_f(sentence): + """ + Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence. + + Parameters: + sentence (str): A string representing a sentence. + + Returns: + str: The sentence without any punctuation marks. + """ + new_sentence = "" + + for letter in sentence: + if letter in string.punctuation: + continue + else: + new_sentence += letter + return(new_sentence) + +def word_count_f(sentence): + """ + Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence. + Note: A word is defined as a sequence of letters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence. + + Parameters: + sentence (str): A string representing a sentence. + + Returns: + int: The number of words in the sentence. + """ + new_sentence = remove_punctuation_f(sentence) + + new_sentence = new_sentence.split() + + number = len(new_sentence) + + return (number) \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..627fe4c 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, "outputs": [], @@ -43,7 +43,36 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " new_list = []\n", + "\n", + " for element in lst:\n", + " if element in new_list:\n", + " continue\n", + " else:\n", + " new_list.append(element)\n", + " \n", + " return new_list\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "74f1e247", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 54, 'Raquel', 5, 8, 'Rafa']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_unique_list([1,1,1,2,3,4,4,4,4,1,1,1,54,\"Raquel\",5,8,\"Raquel\",\"Rafa\"])" ] }, { @@ -60,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], @@ -75,7 +104,38 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " upper = 0\n", + " lower = 0\n", + "\n", + " for letter in string:\n", + "\n", + " if letter.isupper():\n", + " upper += 1\n", + " else:\n", + " lower += 1\n", + "\n", + " return (upper,lower)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "21f4355b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 10)" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_case(\"RaquelMaques\")" ] }, { @@ -92,13 +152,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], "source": [ "import string\n", "\n", + "\n", "def remove_punctuation(sentence):\n", " \"\"\"\n", " Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence.\n", @@ -109,12 +170,19 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " new_sentence = \"\" \n", + "\n", + " for letter in sentence:\n", + " if letter in string.punctuation:\n", + " continue\n", + " else:\n", + " new_sentence += letter \n", + " return(new_sentence)\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", " Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence.\n", - " Note: A word is defined as a sequence of characters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence.\n", + " Note: A word is defined as a sequence of letters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence.\n", " \n", " Parameters:\n", " sentence (str): A string representing a sentence.\n", @@ -122,7 +190,37 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " new_sentence = remove_punctuation(sentence)\n", + "\n", + " new_sentence = new_sentence.split()\n", + "\n", + " number = len(new_sentence)\n", + "\n", + " return (number)\n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "c9aae241", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_count(\"Ola! o meu nome, é Raquel. E o teu?\")" ] }, { @@ -140,12 +238,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "word_count(\"Note : this is an example !!! Good day : )\")" ] }, { @@ -168,12 +277,109 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def addition(num1, num2):\n", + " \"\"\"\n", + " Returns the sum of two numbers.\n", + "\n", + " Parameters:\n", + " num1 (int or float): The first number.\n", + " num2 (int or float): The second number.\n", + "\n", + " Returns:\n", + " int or float: The sum of the two numbers.\n", + " \"\"\"\n", + " return num1 + num2\n", + "\n", + "def subtraction(num1, num2):\n", + " \"\"\"\n", + " Returns the difference between two numbers.\n", + "\n", + " Parameters:\n", + " num1 (int or float): The first number.\n", + " num2 (int or float): The second number.\n", + "\n", + " Returns:\n", + " int or float: The difference between the two numbers.\n", + " \"\"\"\n", + " return num1 - num2\n", + "\n", + "def multiplication(num1, num2):\n", + " \"\"\"\n", + " Returns the product of two numbers.\n", + "\n", + " Parameters:\n", + " num1 (int or float): The first number.\n", + " num2 (int or float): The second number.\n", + "\n", + " Returns:\n", + " int or float: The product of the two numbers.\n", + " \"\"\"\n", + " return num1 * num2\n", + "\n", + "def division(num1, num2):\n", + " \"\"\"\n", + " Returns the quotient of two numbers.\n", + "\n", + " Parameters:\n", + " num1 (int or float): The first number.\n", + " num2 (int or float): The second number.\n", + "\n", + " Returns:\n", + " int or float: The quotient of the two numbers.\n", + " \"\"\"\n", + " return num1 / num2\n", + "\n", + "def calculate(num1, num2, operation):\n", + " \"\"\"\n", + " Performs a calculation based on the given operation.\n", + "\n", + " Parameters:\n", + " num1 (int or float): The first number.\n", + " num2 (int or float): The second number.\n", + " operation (str): A string representing the operation to perform. It can be one of the following: \"+\", \"-\", \"*\", \"/\".\n", + "\n", + " Returns:\n", + " int or float: The result of the calculation.\n", + " \"\"\"\n", + " if operation == \"+\":\n", + " return addition(num1, num2)\n", + " elif operation == \"-\":\n", + " return subtraction(num1, num2)\n", + " elif operation == \"*\":\n", + " return multiplication(num1, num2)\n", + " elif operation == \"/\":\n", + " return division(num1, num2)\n", + " else:\n", + " raise ValueError(\"Invalid operation. Please choose from '+', '-', '*', or '/'.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "dcfaf4d7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate(10, 5, \"+\")\n", + "calculate(10, 5, \"-\")\n", + "calculate(10, 5, \"*\")\n", + "calculate(10, 5, \"/\")" ] }, { @@ -197,7 +403,117 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def addition(*args):\n", + " \"\"\"\n", + " Returns the sum of numbers.\n", + "\n", + " Parameters:\n", + " *args (int or float): A variable number of arguments.\n", + "\n", + " Returns:\n", + " int or float: The sum of the numbers.\n", + " \"\"\"\n", + " return sum(args)\n", + "\n", + "\n", + "def subtraction(*args):\n", + " \"\"\"\n", + " Returns the difference between two numbers.\n", + "\n", + " Parameters:\n", + " *args (int or float): A variable number of arguments.\n", + "\n", + " Returns:\n", + " int or float: The difference between the numbers.\n", + " \"\"\"\n", + " if len(args) < 2:\n", + " raise ValueError(\"At least two numbers are required for subtraction.\")\n", + " result = args[0]\n", + " for n in args[1:]:\n", + " result -= n\n", + " return result\n", + "\n", + "def multiplication(*args):\n", + " \"\"\"\n", + " Returns the product of numbers.\n", + "\n", + " Parameters:\n", + " *args (int or float): A variable number of arguments.\n", + "\n", + " Returns:\n", + " int or float: The product of the numbers.\n", + " \"\"\"\n", + " if len(args) == 0:\n", + " raise ValueError(\"At least one number is required for multiplication.\")\n", + " result = args[0]\n", + " for n in args[1:]:\n", + " result *= n\n", + " return result\n", + "\n", + "def division(*args):\n", + " \"\"\"\n", + " Returns the quotient of numbers.\n", + "\n", + " Parameters:\n", + " *args (int or float): A variable number of arguments.\n", + "\n", + " Returns:\n", + " int or float: The quotient of the numbers.\n", + " \"\"\"\n", + " if len(args) < 2:\n", + " raise ValueError(\"At least two numbers are required for division.\")\n", + " result = args[0]\n", + " for n in args[1:]:\n", + " if n == 0:\n", + " raise ValueError(\"Division by zero is not allowed.\")\n", + " result /= n\n", + " return result\n", + "\n", + "def calculate(*args, operation):\n", + " \"\"\"\n", + " Performs a calculation based on the given operation.\n", + "\n", + " Parameters:\n", + " *args (int or float): A variable number of arguments.\n", + " operation (str): A string representing the operation to perform. It can be one of the following: \"+\", \"-\", \"*\", \"/\".\n", + "\n", + " Returns:\n", + " int or float: The result of the calculation.\n", + " \"\"\"\n", + " if operation == \"+\":\n", + " return addition(*args)\n", + " elif operation == \"-\":\n", + " return subtraction(*args)\n", + " elif operation == \"*\":\n", + " return multiplication(*args)\n", + " elif operation == \"/\":\n", + " return division(*args)\n", + " else:\n", + " raise ValueError(\"Invalid operation. Please choose from '+', '-', '*', or '/'.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "6f3571b6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate(10, 5, operation=\"+\")\n", + "calculate(10, 5, operation=\"-\")\n", + "calculate(10, 5, operation=\"*\")\n", + "calculate(10, 5, operation=\"/\")" ] }, { @@ -273,16 +589,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "import functions\n", + "\n", + "functions.get_unique_list_f([1,1,1,2,3,4,4,4,4,1,1,1,54,\"Raquel\",5,8,\"Raquel\",\"Rafa\"])\n", + "functions.count_case_f(\"RaquelMaques\")\n", + "functions.remove_punctuation_f(\"Note : this is an example !!! Good day : )\")\n", + "functions.word_count_f(\"Note : this is an example !!! Good day : )\")\n" ] }, { @@ -315,18 +647,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def fibonacci(n):\n", + " \"\"\"\n", + " Returns the nth Fibonacci number.\n", + "\n", + " Parameters:\n", + " n (int): The position in the Fibonacci sequence.\n", + "\n", + " Returns:\n", + " int: The nth Fibonacci number.\n", + " \"\"\"\n", + " if n <= 0:\n", + " raise ValueError(\"n must be a positive integer.\")\n", + " elif n == 1:\n", + " return 0\n", + " elif n == 2:\n", + " return 1\n", + " else:\n", + " a, b = 0, 1\n", + " for _ in range(2, n):\n", + " a, b = b, a + b\n", + " return b\n", + "\n", + "def fibonacci_list(n):\n", + " if n <= 0:\n", + " raise ValueError(\"n must be a positive integer.\")\n", + " return [fibonacci(i) for i in range(1, n + 1)] \n" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "22cc4208", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fibonacci_list(10)\n", + "\n", + "fibonacci(10) " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +720,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,