From 7e59edb2b5d7a60c9b60cbbe513799936f8f9fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romina=20Guti=C3=A9rrez?= Date: Mon, 16 Mar 2026 14:26:18 +0100 Subject: [PATCH] Complete lab python functions extra --- .ipynb_checkpoints/functions-checkpoint.py | 0 .../lab-python-functions-checkpoint.ipynb | 589 ++++++++++++++++++ __pycache__/functions.cpython-313.pyc | Bin 0 -> 1353 bytes functions.py | 37 ++ lab-python-functions.ipynb | 291 ++++++++- 5 files changed, 892 insertions(+), 25 deletions(-) create mode 100644 .ipynb_checkpoints/functions-checkpoint.py create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb create mode 100644 __pycache__/functions.cpython-313.pyc create mode 100644 functions.py diff --git a/.ipynb_checkpoints/functions-checkpoint.py b/.ipynb_checkpoints/functions-checkpoint.py new file mode 100644 index 0000000..e69de29 diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..015e207 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,589 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Functions" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to define and call functions, pass arguments, return values, and handle scope. " + ] + }, + { + "cell_type": "markdown", + "id": "e253e768-aed8-4791-a800-87add1204afa", + "metadata": {}, + "source": [ + "## Challenge 1: completing functions based on docstring\n", + "\n", + "Complete the functions below according to the docstring, and test them by calling them to make sure they are well implemented." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "def get_unique_list(lst):\n", + " \"\"\"\n", + " Takes a list as an argument and returns a new list with unique elements from the first list.\n", + "\n", + " Parameters:\n", + " lst (list): The input list.\n", + "\n", + " Returns:\n", + " list: A new list with unique elements from the input list.\n", + " \"\"\"\n", + " # your code goes here\n", + " unique_list = []\n", + "\n", + " for item in lst:\n", + " if item not in unique_list:\n", + " unique_list.append(item)\n", + " return unique_list\n", + "\n", + "print(get_unique_list([1,2,3,3,3,3,4,5]))" + ] + }, + { + "cell_type": "markdown", + "id": "214c2c27-c4fa-46cf-bfbf-26fb25b25882", + "metadata": {}, + "source": [ + "Example:\n", + "\n", + "*Input [1,2,3,3,3,3,4,5] -> type: list*\n", + "\n", + "*Expected Output [1,2,3,4,5] -> type: list*" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2\n", + "Lower count: 8\n" + ] + } + ], + "source": [ + "def count_case(string):\n", + " \"\"\"\n", + " Returns the number of uppercase and lowercase letters in the given string.\n", + "\n", + " Parameters:\n", + " string (str): The string to count uppercase and lowercase letters in.\n", + "\n", + " Returns:\n", + " A tuple containing the count of uppercase and lowercase letters in the string.\n", + " \"\"\"\n", + " # your code goes here\n", + " upper_no = 0\n", + " lower_no = 0\n", + "\n", + " for characters in string:\n", + " if characters.isupper():\n", + " upper_no += 1\n", + " elif characters.islower():\n", + " lower_no += 1\n", + " return upper_no, lower_no\n", + "\n", + "result =count_case (\"Hello World\")\n", + "print (\"Uppercase count: \", result [0])\n", + "print (\"Lower count: \", result [1])\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "2c902b2e-369c-4a40-a166-ca5bc554cab3", + "metadata": {}, + "source": [ + "Example:\n", + "\n", + "*Input: \"Hello World\"* \n", + "\n", + "*Expected Output: Uppercase count: 2, Lowercase count: 8* " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "\n", + "def remove_punctuation(sentence):\n", + " \"\"\"\n", + " Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence.\n", + "\n", + " Parameters:\n", + " sentence (str): A string representing a sentence.\n", + "\n", + " Returns:\n", + " str: The sentence without any punctuation marks.\n", + " \"\"\"\n", + " # your code goes here\n", + " result = \"\"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " result += char\n", + " return result\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", + " \n", + " Parameters:\n", + " sentence (str): A string representing a sentence.\n", + "\n", + " Returns:\n", + " int: The number of words in the sentence.\n", + " \"\"\"\n", + " # your code goes here\n", + " clean_sentence = remove_punctuation(sentence)\n", + " word=clean_sentence.split()\n", + " return len(word)" + ] + }, + { + "cell_type": "markdown", + "id": "52814e4d-7631-4c80-80b6-2b206f7e7419", + "metadata": {}, + "source": [ + "*For example, calling*\n", + "```python \n", + "word_count(\"Note : this is an example !!! Good day : )\")\n", + "``` \n", + "\n", + "*would give you as expected output: 7*" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note this is an example Good day \n", + "7\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "print(remove_punctuation(\"Note : this is an example !!! Good day : )\"))\n", + "print(word_count(\"Note : this is an example !!! Good day : )\"))" + ] + }, + { + "cell_type": "markdown", + "id": "fb01ccba-2ecc-4eea-b9a9-2bf9d29e8804", + "metadata": {}, + "source": [ + "## Challenge 2: Build a Calculator\n", + "\n", + "In this exercise, you will build a calculator using Python functions. The calculator will be able to perform basic arithmetic operations like addition, subtraction, multiplication, and division.\n", + "\n", + "Instructions\n", + "- Define four functions for addition, subtraction, multiplication, and division.\n", + "- Each function should take two arguments, perform the respective arithmetic operation, and return the result.\n", + "- Define another function called \"calculate\" that takes three arguments: two operands and an operator.\n", + "- The \"calculate\" function should use a conditional statement to determine which arithmetic function to call based on the operator argument.\n", + "- The \"calculate\" function should then call the appropriate arithmetic function and return the result.\n", + "- Test your \"calculate\" function by calling it with different input parameters.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15\n", + "5\n", + "50\n", + "2.0\n" + ] + } + ], + "source": [ + "# ADDITION\n", + "def addition (number1, number2):\n", + " return number1 + number2\n", + "\n", + "#SUBSTRACTION\n", + "def substraction (number1, number2):\n", + " return number1 - number2 \n", + "\n", + "#MULTIPLICATION\n", + "def multiplication (number1, number2):\n", + " return number1 * number2\n", + "\n", + "#DIVISION\n", + "def division (number1, number2):\n", + " if number2 == 0:\n", + " return \"Error: division by zero\"\n", + " return number1 / number2\n", + "\n", + "\n", + "\n", + "#CALCULATE\n", + "def calculate (number1, number2, operator):\n", + " if operator == \"+\":\n", + " return addition (number1, number2)\n", + "\n", + " elif operator == \"-\":\n", + " return substraction (number1, number2)\n", + "\n", + " elif operator == \"*\":\n", + " return multiplication (number1, number2)\n", + "\n", + " elif operator == \"/\":\n", + " return division (number1, number2)\n", + "\n", + " else:\n", + " return \"Invalid operator\"\n", + " \n", + " #TEST\n", + "print(calculate(10, 5, \"+\"))\n", + "print(calculate(10, 5, \"-\"))\n", + "print(calculate(10, 5, \"*\"))\n", + "print(calculate(10, 5, \"/\"))\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "d1784c63-208c-4553-9686-acac6c0a9dda", + "metadata": {}, + "source": [ + "### Bonus: args and kwargs\n", + "\n", + "Update the previous exercise so it allows for adding, subtracting, and multiplying more than 2 numbers.\n", + "\n", + "The calculator will be able to perform basic arithmetic operations like addition, subtraction, multiplication, and division on multiple numbers.\n", + "\n", + "*Hint: use args or kwargs. Recommended external resource: [Args and Kwargs in Python](https://www.geeksforgeeks.org/args-kwargs-python/)*" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n", + "24\n", + "10.0\n" + ] + } + ], + "source": [ + "# ADD ARGS\n", + "def add(*args):\n", + " total= 0\n", + " for num in args:\n", + " total += num\n", + " return total\n", + "\n", + "#SUBSTRACT ARGS\n", + "def subs (*args):\n", + " result = args [0]\n", + " for num in args [1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "#MULTIPLY ARGS\n", + "def mult (*args):\n", + " m_result = 1\n", + " for num in args:\n", + " m_result *= num\n", + " return m_result\n", + "\n", + "#DIVIDE ARGS\n", + "def divi (*args):\n", + " d_result = args [0]\n", + " for num in args [1:]:\n", + " if num == 0:\n", + " return \"Error: division by zero\"\n", + " d_result/= num\n", + " return d_result\n", + "\n", + "\n", + "#CALCULATE ARGS\n", + "def calculate (operator, *args):\n", + " if operator == \"+\":\n", + " return add (*args)\n", + "\n", + " elif operator == \"-\":\n", + " return subs (*args)\n", + "\n", + " elif operator == \"*\":\n", + " return mult (*args)\n", + "\n", + " elif operator == \"/\":\n", + " return divi (*args)\n", + "\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + " #TEST\n", + "print(calculate(\"+\", 1,2,3,4))\n", + "print(calculate(\"-\", 10,2,3))\n", + "print(calculate(\"*\", 2,3,4))\n", + "print(calculate(\"/\", 100,2,5))\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "af2de734-2a0e-4458-9cb8-57284b7108ae", + "metadata": {}, + "source": [ + "## Challenge 3: importing functions from a Python file" + ] + }, + { + "cell_type": "markdown", + "id": "56780ce8-6610-4fb5-a4ef-22cd46af83d2", + "metadata": {}, + "source": [ + "Moving the functions created in Challenge 1 to a Python file.\n", + "\n", + "- In the same directory as your Jupyter Notebook, create a new Python file called `functions.py`.\n", + "- Copy and paste the functions you created earlier in the Jupyter Notebook into the functions.py file. Rename the functions to `get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f`. Add the _f suffix to each function name to ensure that you're calling the functions from your file.\n", + "- Save the `functions.py` file and switch back to the Jupyter Notebook.\n", + "- In a new cell, import the functions from functions.py\n", + "- Call each function with some sample input to test that they're working properly.\n", + "\n", + "There are several ways to import functions from a Python module such as functions.py to a Jupyter Notebook:\n", + "\n", + "1. Importing specific functions: If you only need to use a few functions from the module, you can import them individually using the from keyword. This way, you can call the functions directly using their names, without having to use the module name. For example:\n", + "\n", + "```python\n", + "from function_file import function_name\n", + "\n", + "function_name(arguments)```\n", + "\n", + "2. Importing the entire module: You can import the entire module using the import keyword followed by the name of the module. Then, you can call the functions using the module_name.function_name() syntax. Example:\n", + "\n", + "```python\n", + "import function_file\n", + "\n", + "function_file.function_name()\n", + "```\n", + "\n", + "3. Renaming functions during import: You can also rename functions during import using the as keyword. This is useful if you want to use a shorter or more descriptive name for the function in your code. For example:\n", + "\n", + "```python\n", + "from function_file import function_name as f\n", + "\n", + "f.function_name(arguments)\n", + "```\n", + "\n", + "Regardless of which method you choose, make sure that the functions.py file is in the same directory as your Jupyter Notebook, or else specify the path to the file in the import statement or by using `sys.path`.\n", + "\n", + "You can find examples on how to import Python files into jupyter notebook here: \n", + "- https://medium.com/cold-brew-code/a-quick-guide-to-understanding-pythons-import-statement-505eea2d601f\n", + "- https://www.geeksforgeeks.org/absolute-and-relative-imports-in-python/\n", + "- https://www.pythonforthelab.com/blog/complete-guide-to-imports-in-python-absolute-relative-and-more/\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "14f222c5-e7bb-4626-b45c-bd735001f768", + "metadata": {}, + "source": [ + "To ensure that any changes made to the Python file are reflected in the Jupyter Notebook upon import, we need to use an IPython extension that allows for automatic reloading of modules. Without this extension, changes made to the file won't be reloaded or refreshed in the notebook upon import.\n", + "\n", + "For that, we will include the following code:\n", + "```python\n", + "%load_ext autoreload\n", + "%autoreload 2 \n", + "```\n", + "\n", + "You can read more about this here: https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", + "metadata": {}, + "outputs": [], + "source": [ + "# IPython extension to reload modules before executing user code.\n", + "%load_ext autoreload\n", + "%autoreload 2 \n", + "\n", + "# your code goes here\n", + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "df81382d-8d66-42c7-812d-0492a46440ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Hello world\n", + "7\n" + ] + } + ], + "source": [ + "print(get_unique_list_f([1,2,2,3,4,4,5]))\n", + "print(count_case_f(\"Hello World\"))\n", + "print(remove_punctuation_f(\"Hello, world!!!\"))\n", + "print(word_count_f(\"Note : this is an example !!! Good day : )\"))" + ] + }, + { + "cell_type": "markdown", + "id": "06d0eb13-8dcb-4783-8f74-8f71beb2d0b4", + "metadata": {}, + "source": [ + "## Bonus: recursive functions\n", + "\n", + "The Fibonacci sequence is a mathematical sequence that appears in various fields, including nature, finance, and computer science. It starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.\n", + "\n", + "Write a Python function that uses recursion to compute the Fibonacci sequence up to a given number n.\n", + "\n", + "To accomplish this, create a function that calculates the Fibonacci number for a given input. For example, the 10th Fibonacci number is 55.\n", + "Then create another function that generates a list of Fibonacci numbers from 0 to n.\n", + "Test your function by calling it with different input parameters." + ] + }, + { + "cell_type": "markdown", + "id": "b1686493-48bb-47f1-b3c9-75994816b277", + "metadata": {}, + "source": [ + "Example:\n", + "\n", + "*Expected output for n = 14:*\n", + "\n", + "*Fibonacci sequence: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]*" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "a1d55cea-96c3-4853-8220-17c0904a8816", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here\n", + "\n", + "def fibonacci (num):\n", + " if num<= 1:\n", + " return num\n", + " else:\n", + " return fibonacci (num-1) + fibonacci (num-2)\n", + "\n", + "def fibo_sequence (num):\n", + " sequence = []\n", + " for num in range (1, num+1):\n", + " sequence.append(fibonacci(num))\n", + " return sequence\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "c80ba14d-004c-437d-bb4b-089f559655bd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fibonacci sequence: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], + "source": [ + "num =14\n", + "print(\"Fibonacci sequence:\", fibo_sequence(num))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "201b41e0-7414-426a-ae8f-83b08f0572c8", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..775d6359b12208f4d20225c2e88b7bcafeb5c85d GIT binary patch literal 1353 zcmaKsOK;Oa5XWczNL%7Slcuy35C(FRcHM-Bs*bd?-pS0a{oDVfn@a5hw71Gf_niW; zCKf%zwX|*yr&TCHiChN;C1nB_DXHj{l7?O_>FBj3VkOo^L9t3?UF63stc7J`M@ia;=(V!9^U8%V| zs4OzURh-X{qiaGZwWoRI$MDFC)=BR9nmpJsGVMaE@Fw@oIP^=0bao>FMy6TlVmAB( zP;4RrzQPB*2I3JMiiCI`A*y9{J8WmfMrg<437i1|xTNG$SVIbjaHY*VE} z#cXA(Rw-PFFJUfoTFHT>7MW1Wy0|1ui3VIA;*~J*Q1c&CW@(WYAhSr1pGF_gr?XV4)*Bh{RiZf!U#F}5dLlkKV2RL4layxqF? ze7ciLx9_&@w(q^T*N^=gg7oNy0Y-Xd@{=~k#E#qZy@kKTd?j0y8d6r~F&f)!uOWH? zocr?K2QZiU!?q_^_HS!L&zv}gpyFRgB;aIy(*0ojPkG+uo2v}r`s4x zmeIQZh*tipL^#+!FfMZQ=(gmwHasr5yngh3%=$cLeN9>@vsUr>6``>`Eyt`9^ZH4=R%mDRri+65L& literal 0 HcmV?d00001 diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..8d40fa3 --- /dev/null +++ b/functions.py @@ -0,0 +1,37 @@ +import string + +def get_unique_list_f(lst): + unique = [] + for item in lst: + if item not in unique: + unique.append(item) + return unique + + +def count_case_f(text): + upper = 0 + lower = 0 + + for char in text: + if char.isupper(): + upper += 1 + elif char.islower(): + lower += 1 + + return upper, lower + + +def remove_punctuation_f(sentence): + result = "" + + for char in sentence: + if char not in string.punctuation: + result += char + + return result + + +def word_count_f(sentence): + clean_sentence = remove_punctuation_f(sentence) + words = clean_sentence.split() + return len(words) \ No newline at end of file diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..015e207 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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,15 @@ " 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", + " unique_list = []\n", + "\n", + " for item in lst:\n", + " if item not in unique_list:\n", + " unique_list.append(item)\n", + " return unique_list\n", + "\n", + "print(get_unique_list([1,2,3,3,3,3,4,5]))" ] }, { @@ -60,10 +76,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2\n", + "Lower count: 8\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +100,21 @@ " 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", + " upper_no = 0\n", + " lower_no = 0\n", + "\n", + " for characters in string:\n", + " if characters.isupper():\n", + " upper_no += 1\n", + " elif characters.islower():\n", + " lower_no += 1\n", + " return upper_no, lower_no\n", + "\n", + "result =count_case (\"Hello World\")\n", + "print (\"Uppercase count: \", result [0])\n", + "print (\"Lower count: \", result [1])\n", + " " ] }, { @@ -92,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, "outputs": [], @@ -110,6 +149,11 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " result = \"\"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " result += char\n", + " return result\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +166,10 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + " clean_sentence = remove_punctuation(sentence)\n", + " word=clean_sentence.split()\n", + " return len(word)" ] }, { @@ -140,12 +187,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "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" + "# your code goes here\n", + "print(remove_punctuation(\"Note : this is an example !!! Good day : )\"))\n", + "print(word_count(\"Note : this is an example !!! Good day : )\"))" ] }, { @@ -168,12 +226,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "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" + "# ADDITION\n", + "def addition (number1, number2):\n", + " return number1 + number2\n", + "\n", + "#SUBSTRACTION\n", + "def substraction (number1, number2):\n", + " return number1 - number2 \n", + "\n", + "#MULTIPLICATION\n", + "def multiplication (number1, number2):\n", + " return number1 * number2\n", + "\n", + "#DIVISION\n", + "def division (number1, number2):\n", + " if number2 == 0:\n", + " return \"Error: division by zero\"\n", + " return number1 / number2\n", + "\n", + "\n", + "\n", + "#CALCULATE\n", + "def calculate (number1, number2, operator):\n", + " if operator == \"+\":\n", + " return addition (number1, number2)\n", + "\n", + " elif operator == \"-\":\n", + " return substraction (number1, number2)\n", + "\n", + " elif operator == \"*\":\n", + " return multiplication (number1, number2)\n", + "\n", + " elif operator == \"/\":\n", + " return division (number1, number2)\n", + "\n", + " else:\n", + " return \"Invalid operator\"\n", + " \n", + " #TEST\n", + "print(calculate(10, 5, \"+\"))\n", + "print(calculate(10, 5, \"-\"))\n", + "print(calculate(10, 5, \"*\"))\n", + "print(calculate(10, 5, \"/\"))\n", + " " ] }, { @@ -192,12 +303,76 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "5\n", + "24\n", + "10.0\n" + ] + } + ], "source": [ - "# your code goes here" + "# ADD ARGS\n", + "def add(*args):\n", + " total= 0\n", + " for num in args:\n", + " total += num\n", + " return total\n", + "\n", + "#SUBSTRACT ARGS\n", + "def subs (*args):\n", + " result = args [0]\n", + " for num in args [1:]:\n", + " result -= num\n", + " return result\n", + "\n", + "#MULTIPLY ARGS\n", + "def mult (*args):\n", + " m_result = 1\n", + " for num in args:\n", + " m_result *= num\n", + " return m_result\n", + "\n", + "#DIVIDE ARGS\n", + "def divi (*args):\n", + " d_result = args [0]\n", + " for num in args [1:]:\n", + " if num == 0:\n", + " return \"Error: division by zero\"\n", + " d_result/= num\n", + " return d_result\n", + "\n", + "\n", + "#CALCULATE ARGS\n", + "def calculate (operator, *args):\n", + " if operator == \"+\":\n", + " return add (*args)\n", + "\n", + " elif operator == \"-\":\n", + " return subs (*args)\n", + "\n", + " elif operator == \"*\":\n", + " return mult (*args)\n", + "\n", + " elif operator == \"/\":\n", + " return divi (*args)\n", + "\n", + " else:\n", + " return \"Invalid operator\"\n", + "\n", + " #TEST\n", + "print(calculate(\"+\", 1,2,3,4))\n", + "print(calculate(\"-\", 10,2,3))\n", + "print(calculate(\"*\", 2,3,4))\n", + "print(calculate(\"/\", 100,2,5))\n", + " " ] }, { @@ -273,16 +448,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, "outputs": [], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", - "%autoreload 2 \n", + "%autoreload 2 \n", "\n", - "# your code goes here" + "# your code goes here\n", + "from functions import get_unique_list_f, count_case_f, remove_punctuation_f, word_count_f" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "df81382d-8d66-42c7-812d-0492a46440ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "(2, 8)\n", + "Hello world\n", + "7\n" + ] + } + ], + "source": [ + "print(get_unique_list_f([1,2,2,3,4,4,5]))\n", + "print(count_case_f(\"Hello World\"))\n", + "print(remove_punctuation_f(\"Hello, world!!!\"))\n", + "print(word_count_f(\"Note : this is an example !!! Good day : )\"))" ] }, { @@ -315,20 +515,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "def fibonacci (num):\n", + " if num<= 1:\n", + " return num\n", + " else:\n", + " return fibonacci (num-1) + fibonacci (num-2)\n", + "\n", + "def fibo_sequence (num):\n", + " sequence = []\n", + " for num in range (1, num+1):\n", + " sequence.append(fibonacci(num))\n", + " return sequence\n", + " \n", + " " ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "c80ba14d-004c-437d-bb4b-089f559655bd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fibonacci sequence: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]\n" + ] + } + ], + "source": [ + "num =14\n", + "print(\"Fibonacci sequence:\", fibo_sequence(num))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "201b41e0-7414-426a-ae8f-83b08f0572c8", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -340,7 +581,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,