From 33b6a98dd580affca255a0fe5e55509c7b6989e1 Mon Sep 17 00:00:00 2001 From: Ricardo Santos Date: Fri, 16 Jan 2026 15:24:54 +0000 Subject: [PATCH] lab done: functions extra --- __pycache__/functions.cpython-313.pyc | Bin 0 -> 2696 bytes functions.py | 71 +++++++ lab-python-functions.ipynb | 256 ++++++++++++++++++++++++-- 3 files changed, 308 insertions(+), 19 deletions(-) create mode 100644 __pycache__/functions.cpython-313.pyc create mode 100644 functions.py diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..79a5aeb37794ca63a6288ba7bdcae3c65502c38c GIT binary patch literal 2696 zcmbVOO>7fK6rTNYel`wx^9My~I-#hIQQ`xT9!!6frbsCYw8B||8duiF9w*zZXP22Z zArV48265o8h#Wb#H;x=R_Q1iBkz#F;dgzH8B|Y@mH~V8dDI%yN?d;5(_ukCD_wDy) zx4&OQ(7LYtWL@q-=ua_;n-mRpM`7>~<qg%E{2nIR$!USxqO3v92&lz@xyk zn}O~jT5PQ?3Te;`G#yFfH6K%pwtQJ_210{XK$GLqE@MDG%mu->l6w`vz;@?ZOJ$lg2I(1ZjK<14zF_ zC3s{54WhZT2vNmQF11oCtiv`Py(gl#*^Q6^fSKwC8|lJFFjFqIfDkFcYrkCyf^*?B z(3T@x`N9_n^&yO|44jdvx+q{4w47o{hfq#6)yfiK z*eTk?KpISD{zJ8RL*CZ0eCfFUqgwyqJ1xyLNJybnI15Kq860#GFJ99(p&0M~=} z0GBPG&0w$U+5~K=XId0C3{DWbdjAv0XMH82^Gc*>LpNF<8+gm&jloGg-}>fAm{$C5 ziwB1mgCHh;!i7)w(h0vSbnMGj7l7=n`D$^+WWMBakqRstgJdH=8MrHm*aG;5L39`% zPN1uy*CR;lt9{*sZua-o$fnvnJb35Ets8f4-n!XL4cs;Fb>Ekp`tYOCd!vuW9*i|p zgLkjq>$$J|)r<5auQjA4H+yR{FV)_9^24pAt%dsdnJ2j?=jvMag?hHxm#odPUf5|5 zmQ@f0SbheBhsZ#0Lv9XGIxT)lXm4Ft4p>(YSWg_V-ql{GQU_T|9hep**}d@xfTrSH z6Etbt*cEh8H3cK!ipf?vo+vt%ipeK2K-_XlFd)~9wpr=WnZ!Vryls;X`O?H3$dk|_ z*$?(e9K(Gk`X_*nT}FhF!0R6K7O(+0A;HGJu^?;*xYnfW|2=+2^QB22`BKK0#(n84 zU&;b^cEd)tStLw=3FbYqGhY*g8iF&T)DVY5EC^DeBR+sf4CbG}!}HJucpNykqmOUv z$A^he;a7%^E>*&w!Tm|uGD|XH}oHObh53J zdfBS4RvJ33od-~Eo_wJWH~WU`>hL>YNMY;F%Gj=lq7J@`9;w_WK0}UyL6t@E=LIb% z843_#YYm7{0no_iCQyMkD|m_Tqrx=@%8npB6GwCxV*oEWxa5Gk#j(pAfRnvG85LB^ zgWQKG?1*r??8f=YXcrxihzct9!QiPx$`&Q1ptu9x0EmD$!O4P9%wj;loVYOdKx=6o za~BfNGx#!)qqq)m4%Y!VmFbBuVFd65|G^T$)WJ3}O9F}x!%!P7TZ}{dn9{=v-9Fx) zJ3%b|KR5@T3~}DBGOf^xi%9G{qirNFi&(_Gs|Kj_6Wp~exGdO&hA3Cr806GX72%^G zV1?L$p9uW=iNFCD^wvSYVyzHzaG8AyYofODEOb!&kD{ZKzsxm`OxM++W^!~Vd3-y0 zypcRnJNGhq_-^9%c^CkU=eLHpy6YN#p^i8EQnmR2%oo#YaF+-;JvegU{#O*NhCf}; t=M|?^waJ$(4I7H;!#{xTwIWH<8#yTry%`;n`rl;wrIFpSl%xly{{oa)&uahx literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..4be4b6c --- /dev/null +++ b/functions.py @@ -0,0 +1,71 @@ +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. + """ + # your code goes here + new_list = list(set(lst)) + 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. + """ + # your code goes here + up = 0 + low = 0 + for char in string: + if char.isupper(): + up += 1 + elif char.islower(): + low += 1 + t = (up, low) + print(f"Uppercase count: {up}, Lowercase count: {low}") + return t + + +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. + """ + # your code goes here + new_string = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '').replace(':', '') + print(new_string) + return new_string + +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 characters 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. + """ + # your code goes here + cleaned_sentence = remove_punctuation_f(sentence) + words = cleaned_sentence.split() + count = len(words) + print(f"Word count: {count}") + return count + + diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..b7ad30a 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +54,11 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " # your code goes here\n", + " new_list = list(set(lst))\n", + " return new_list\n", + "\n", + "get_unique_list([1, 2, 2, 3, 4, 4, 5])" ] }, { @@ -63,8 +78,29 @@ "execution_count": null, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase letters: 2, Lowercase letters: 8\n" + ] + }, + { + "data": { + "text/plain": [ + "(2, 8)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "import string\n", + "\n", + "\n", "def count_case(string):\n", " \"\"\"\n", " Returns the number of uppercase and lowercase letters in the given string.\n", @@ -75,7 +111,19 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " up = 0\n", + " low = 0\n", + " for char in string:\n", + " if char.isupper():\n", + " up += 1\n", + " elif char.islower():\n", + " low += 1\n", + " t = (up, low)\n", + " print(f\"Uppercase count: {up}, Lowercase count: {low}\")\n", + " return t\n", + "\n", + "count_case(\"Hello World!\")" ] }, { @@ -92,10 +140,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note this is an example Good day \n", + "Word count: 7\n" + ] + }, + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import string\n", "\n", @@ -110,6 +177,9 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " new_string = sentence.replace(',', '').replace('.', '').replace('!', '').replace('?', '').replace(':', '')\n", + " print(new_string)\n", + " return new_string\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +192,16 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " cleaned_sentence = remove_punctuation(sentence)\n", + " words = cleaned_sentence.split()\n", + " count = len(words)\n", + " print(f\"Word count: {count}\")\n", + " return count\n", + "\n", + "\n", + "#remove_punctuation(\"Note : this is an example !!! Good day : \")\n", + "word_count(\"Note : this is an example !!! Good day : \")\n" ] }, { @@ -168,12 +247,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "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 calculation(operator, a, b): \n", + " if operator == '+':\n", + " return addition(a, b)\n", + " elif operator == '-':\n", + " return subtraction(a, b)\n", + " elif operator == '*':\n", + " return multiplication(a, b)\n", + " elif operator == '/':\n", + " return division(a, b)\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + "print(calculation('+', 10, 5))\n", + "print(calculation('-', 10, 5))\n", + "print(calculation('*', 10, 5))\n", + "print(calculation('/', 10, 5))" ] }, { @@ -192,12 +310,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17\n", + "3\n", + "100\n", + "1.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def addition(*args):\n", + " return sum(args)\n", + "\n", + "def subtraction(*args):\n", + " for arg in args:\n", + " if arg == args[0]:\n", + " result = arg\n", + " else:\n", + " result -= arg\n", + " return result\n", + "\n", + "def multiplication(*args):\n", + " for arg in args:\n", + " if 'result' not in locals():\n", + " result = arg\n", + " else:\n", + " result *= arg\n", + " return result\n", + "\n", + "def division(*args):\n", + " for arg in args:\n", + " if arg == args[0]:\n", + " result = arg\n", + " else:\n", + " result /= arg\n", + " return result\n", + "\n", + "\n", + "print(addition(10, 5, 2))\n", + "print(subtraction(10, 5, 2))\n", + "print(multiplication(10, 5, 2))\n", + "print(division(10, 5, 2))" ] }, { @@ -255,6 +416,42 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "227913fc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world This is a test\n", + "Hello world This is a test\n", + "Word count: 6\n", + "Uppercase count: 2, Lowercase count: 8\n" + ] + }, + { + "data": { + "text/plain": [ + "(2, 8)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import functions\n", + "\n", + "functions.remove_punctuation_f(\"Hello, world! This is a test.\")\n", + "functions.word_count_f(\"Hello, world! This is a test.\")\n", + "functions.get_unique_list_f([1, 2, 2, 3, 4, 4, 5])\n", + "functions.count_case_f(\"Hello World!\")\n" + ] + }, { "cell_type": "markdown", "id": "14f222c5-e7bb-4626-b45c-bd735001f768", @@ -315,18 +512,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def fib(n):\n", + " if n == 0:\n", + " return [0]\n", + " if n == 1:\n", + " return [0,1]\n", + " seq = fib(n-1)\n", + " seq.append(seq[-1] + (seq[-2]))\n", + " return seq\n", + "\n", + "fib(14)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +558,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,