From 82fb3dac937bae625642523aad6b3b2c1cce0bf1 Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Fri, 26 Dec 2025 00:25:31 +0100 Subject: [PATCH] Updated lab functions extra --- __pycache__/functions.cpython-313.pyc | Bin 0 -> 1800 bytes functions.py | 28 ++ lab-python-functions.ipynb | 424 ++++++++++++++++++++++++-- 3 files changed, 426 insertions(+), 26 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..3815dc2168b2bc73077a27139c98541902693198 GIT binary patch literal 1800 zcmcIk&u8unn;+A;prlY$j%_^{hLy zo50QLrJx=V2a8--5X6<#I)Yp!7-~4*>>wDk(x)T#0 zBPjc?v)0ctLVt@+SfqZVn}9|G6;J`+Mhq3CImB>51}ha5uyP>+R;l1*^jIv(nYh1F zR0{mCGwR*}(?DM%ii_wx%6y6tmF|D>E%@%hNm;l@5pNBz1bts`86^Z9u<%9@T=f2N)WwI5b$FUH6jqB`Cg1FcvarTP3c%%Uxex-i+d0()|7lHb=_CY zhX&(v-_?c`523Q=7 zm|?TRTmS=WWbRdg$x-eyi&lI!fU6up;j6)Jy-rB%9&E8;tk~zU)fD{vOE7ixR*N6S z*I%aF@%6U0UcY^|-%)2<>OxCdU@G{Z0!o2_AalQg2IT+9>=bG+if#eX{L>o_kQclj zqS|MIYTGc@D_f!Pgac3yWCCJd#C@W{Q-v5*kRTcp*G)<{(Aros2Q=MiHJoiAJz? TqRBXM62tf|?vBZLHrU`Fg#lsS literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..08aadc2 --- /dev/null +++ b/functions.py @@ -0,0 +1,28 @@ +# functions.py + +import string + +# 1. Returns a new list with unique elements (keeps first appearance) +def get_unique_list_f(lst): + unique = [] + for item in lst: + if item not in unique: + unique.append(item) + return unique + + +# 2. Counts uppercase and lowercase letters in a string +def count_case_f(string): + upper_count = sum(1 for char in string if char.isupper()) + lower_count = sum(1 for char in string if char.islower()) + return upper_count, lower_count + +# 3. Removes punctuation marks from a sentence +def remove_punctuation_f(sentence): + return ''.join(char for char in sentence if char not in string.punctuation) + +# 4. Counts the number of words in a given sentence after removing punctuation +def word_count_f(sentence): + clean_sentence = remove_punctuation_f(sentence) + words = clean_sentence.split() + return len(words) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..a897534 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +51,17 @@ " 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", + " \n", + " # Returns a new list with unique elements (keeps first appearance)\n", + " unique = []\n", + " for item in lst:\n", + " if item not in unique:\n", + " unique.append(item)\n", + " return unique\n", + "\n", + "# quick test\n", + "print(get_unique_list([1, 2, 3, 3, 3, 3, 4, 5])) # expected list: [1, 2, 3, 4, 5]" ] }, { @@ -60,10 +78,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 8)\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +101,15 @@ " 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", + " \n", + " # Counts uppercase and lowercase letters\n", + " upper_count = sum(1 for char in string if char.isupper())\n", + " lower_count = sum(1 for char in string if char.islower())\n", + " return upper_count, lower_count\n", + "# quick test\n", + "print(count_case(\"Hello World!\")) # expected output: (2, 8)\n", + " " ] }, { @@ -92,10 +126,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world How are you\n", + "5\n" + ] + } + ], "source": [ "import string\n", "\n", @@ -111,6 +154,11 @@ " \"\"\"\n", " # your code goes here\n", "\n", + " # Removes punctuation marks from the sentence\n", + " return ''.join(char for char in sentence if char not in string.punctuation)\n", + "# quick test\n", + "print(remove_punctuation(\"Hello, world! How are you?\")) # expected output: \"Hello world How are you\"\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", @@ -122,7 +170,13 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " # counts the number of words in the sentence after removing punctuation\n", + " clean_sentence = remove_punctuation(sentence)\n", + " words = clean_sentence.split()\n", + " return len(words)\n", + "# quick test\n", + "print(word_count(\"Hello, world! How are you?\")) # expected output: 5" ] }, { @@ -140,12 +194,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# call the word_count function with a test string and print the result\n", + "print(word_count(\"Note : this is an example !!! Good day : )\")) # expected output: 7" ] }, { @@ -168,12 +232,75 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "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", + "\n", + "# define the four functons for additioon, subtraction, multiplication, and division\n", + "def add(a, b):\n", + " return a + b\n", + "def subtract(a, b):\n", + " return a - b\n", + "def multiply(a, b):\n", + " return a * b\n", + "def divide(a, b):\n", + " return a / b\n", + "# test the functions with some example inputs\n", + "print(add(10, 5)) # expected output: 15\n", + "print(subtract(10, 5)) # expected output: 5\n", + "print(multiply(10, 5)) # expected output: 50\n", + "print(divide(10, 5)) # expected output: 2.0\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "04210158", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n" + ] + } + ], + "source": [ + "# Define another function called \"calculate\" that takes three arguments: two operands and an operator.\n", + "def calculate(a, b, operator):\n", + " if operator == \"+\":\n", + " return add(a, b)\n", + " elif operator == \"-\":\n", + " return subtract(a, b)\n", + " elif operator == \"*\":\n", + " return multiply(a, b)\n", + " elif operator == \"/\":\n", + " return divide(a, b)\n", + " else:\n", + " raise ValueError(\"Invalid operator\")\n", + "# test the calculate function with some example inputs\n", + "print(calculate(10, 5, \"+\")) # expected output: 15\n", + "print(calculate(10, 5, \"-\")) # expected output: 5\n", + "print(calculate(10, 5, \"*\")) # expected output: 50\n", + "print(calculate(10, 5, \"/\")) # expected output: 2.0\n" ] }, { @@ -192,12 +319,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17\n", + "3\n", + "100\n", + "10.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# Update the previous exercise so it allows for adding, subtracting, and multiplying more than 2 numbers.\n", + "# The calculator will be able to perform basic arithmetic operations like addition, subtraction, multiplication, and division on multiple numbers.\n", + "# Hint: use args or kwargs. Recommended external resource: [Args and Kwargs in Python](https://www.geeksforgeeks.org/args-kwargs-python/)\n", + "def calculate_multiple(operator, *args):\n", + " if operator == \"+\":\n", + " result = 0\n", + " for num in args:\n", + " result += num\n", + " return result\n", + " elif operator == \"-\":\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result -= num\n", + " return result\n", + " elif operator == \"*\":\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + " elif operator == \"/\":\n", + " result = args[0]\n", + " for num in args[1:]:\n", + " result /= num\n", + " return result\n", + " else:\n", + " raise ValueError(\"Invalid operator\")\n", + "# test the calculate_multiple function with some example inputs\n", + "print(calculate_multiple(\"+\", 10, 5, 2)) # expected output: 17\n", + "print(calculate_multiple(\"-\", 10, 5, 2)) # expected output: 3\n", + "print(calculate_multiple(\"*\", 10, 5, 2)) # expected output: 100\n", + "print(calculate_multiple(\"/\", 100, 5, 2)) # expected output: 10.0\n" ] }, { @@ -273,16 +442,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "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", + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Note this is an example Good day \n", + "7\n" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", "\n", - "# your code goes here" + "# your code goes here\n", + "\n", + "#import the functions from the funtions.py file\n", + "\n", + "from functions import (get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f)\n", + "\n", + "# quick tests\n", + "print(get_unique_list_f([1,2,3,3,4,4,5]))\n", + "print(count_case_f(\"Hello World\"))\n", + "print(remove_punctuation_f(\"Note : this is an example !!! Good day : )\"))\n", + "print(word_count_f(\"Note : this is an example !!! Good day : )\"))" ] }, { @@ -315,18 +507,198 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n", + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "\n", + "# create a function that calculates the Fibonacci number for a given input. For example, the 10th Fibonacci number is 55.\n", + "def fibonacci(n):\n", + " if n <= 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + " else:\n", + " a, b = 0, 1\n", + " for _ in range(2, n + 1):\n", + " a, b = b, a + b\n", + " return b\n", + "# test the fibonacci function with some example inputs\n", + "print(fibonacci(10)) # expected output: 55\n", + "\n", + "# create another function that generates a list of Fibonacci numbers from 0 to n.\n", + "def fibonacci_list(n):\n", + " fib_list = []\n", + " for i in range(n):\n", + " fib_list.append(fibonacci(i))\n", + " return fib_list\n", + "# test the fibonacci_list function with some example inputs\n", + "print(fibonacci_list(10)) # expected output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b4f2805", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "377\n" + ] + } + ], + "source": [ + "# Write a Python function that uses recursion to compute the Fibonacci sequence up to a given number n.\n", + "def fibonacci_recursive(n):\n", + " if n <= 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + " else:\n", + " return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)\n", + "# quick test\n", + "print(fibonacci_recursive(14)) # expected output: 377" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "83a12077", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "# call the fibonacci_recursive function with a test value and print the result\n", + "print(fibonacci_recursive(4)) # expected output: 55\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a5bae1d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n" + ] + } + ], + "source": [ + "# create the function fibonacci_sequence_recursive that generates a list of Fibonacci numbers up to n using recursion\n", + "def fibonacci_sequence_recursive(n):\n", + " if n <= 0:\n", + " return []\n", + " elif n == 1:\n", + " return [0]\n", + " elif n == 2:\n", + " return [0, 1]\n", + " else:\n", + " seq = fibonacci_sequence_recursive(n - 1)\n", + " seq.append(seq[-1] + seq[-2])\n", + " return seq\n", + "# quick test\n", + "print(fibonacci_sequence_recursive(10)) # expected output: [0, 1," + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "ee9e1a9b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], + "source": [ + "# create a function fibonacci_sequence_recursive that generates a list of Fibonacci numbers up to n using recursion\n", + "# the list must star from 1\n", + "def fibonacci_sequence_recursive(n):\n", + " if n <= 0:\n", + " return []\n", + " elif n == 1:\n", + " return [1]\n", + " elif n == 2:\n", + " return [1, 1]\n", + " else:\n", + " seq = fibonacci_sequence_recursive(n - 1)\n", + " seq.append(seq[-1] + seq[-2])\n", + " return seq\n", + "\n", + "# quick test\n", + "print(fibonacci_sequence_recursive(14)) # expected output: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "4e089a1a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 3]\n" + ] + } + ], + "source": [ + "# call the fibonacci_sequence_recursive function with a test value and print the result\n", + "print(fibonacci_sequence_recursive(4)) # expected output: [1, 1," + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "5822f1eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445]\n" + ] + } + ], "source": [ - "# your code goes here" + "# call the fibonacci_sequence_recursive function with a test value and print the result\n", + "print(fibonacci_sequence_recursive(55)) # expected output: [1, 1," ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +712,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,