diff --git a/__pycache__/functions.cpython-313.pyc b/__pycache__/functions.cpython-313.pyc new file mode 100644 index 0000000..218586b Binary files /dev/null and b/__pycache__/functions.cpython-313.pyc differ diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..71a41e3 --- /dev/null +++ b/functions.py @@ -0,0 +1,65 @@ +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. + """ + unique_set = set(lst) + unique_list = list(unique_set) + return unique_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. + """ + count_low_case = 0 + count_up_case = 0 + + for char in string: + if char.islower(): + count_low_case +=1 + elif char.isupper(): + count_up_case +=1 + return count_low_case, count_up_case + +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. + """ + punctuation = ",.;:)!?" + sentence_clean = ''.join([char for char in sentence if char not in punctuation]) + return sentence_clean + print(sentence_clean) + + +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. + """ + cleaned_sentence = remove_punctuation(sentence) + words = cleaned_sentence.split() + count_words = len(words) + return count_words diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..c41ee49 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,30 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " unique_set = set(lst)\n", + " unique_list = list(unique_set)\n", + " return unique_list\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1bc1a8d3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "lst= [1,2,3,3,3,3,4,5]\n", + "unique_list = get_unique_list(lst)\n", + "print(unique_list)\n" ] }, { @@ -60,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, "outputs": [], @@ -75,7 +98,36 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " count_low_case = 0\n", + " count_up_case = 0\n", + "\n", + " for char in string:\n", + " if char.islower():\n", + " count_low_case +=1\n", + " elif char.isupper():\n", + " count_up_case +=1\n", + " return count_low_case, count_up_case" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1c078df8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "lower case count is: 8\n", + "uppper case count is: 2\n" + ] + } + ], + "source": [ + "string = \"Hello World\"\n", + "case_count = count_case(string)\n", + "print(f'lower case count is: {case_count[0]}\\nuppper case count is: {case_count[1]}')" ] }, { @@ -92,12 +144,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "there are 7 words in sentence\n" + ] + } + ], "source": [ "import string\n", + "sentence = \"Note : this is an example !!! Good day : )\"\n", "\n", "def remove_punctuation(sentence):\n", " \"\"\"\n", @@ -109,7 +170,11 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " punctuation = \",.;:)!?\"\n", + " sentence_clean = ''.join([char for char in sentence if char not in punctuation])\n", + " return sentence_clean\n", + " print(sentence_clean)\n", + "\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +187,11 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " cleaned_sentence = remove_punctuation(sentence)\n", + " words = cleaned_sentence.split()\n", + " count_words = len(words)\n", + " return count_words\n", + "print(f'there are {word_count(sentence)} words in sentence')" ] }, { @@ -138,16 +207,6 @@ "*would give you as expected output: 7*" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, { "cell_type": "markdown", "id": "fb01ccba-2ecc-4eea-b9a9-2bf9d29e8804", @@ -168,12 +227,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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" + "def addition(a,b):\n", + " return a + b\n", + "\n", + "def substraction(a,b):\n", + " return a - b\n", + "\n", + "def multiplication(a,b):\n", + " return a * b\n", + "\n", + "def division(a,b):\n", + " if b == 0:\n", + " raise ValueError(\"Cannot divide by zero.\")\n", + " return a / b\n", + "\n", + "def calculate(operand1, operand2, operator):\n", + " if operator == '+':\n", + " return operand1 + operand2\n", + " elif operator == '-':\n", + " return operand1 - operand2\n", + " elif operator == '*':\n", + " return operand1 * operand2\n", + " elif operator == '/':\n", + " return operand1 / operand2\n", + " else:\n", + " raise ValueError(\"Unsupported operator. Use '+', '-', '*', or '/'.\")\n", + " \n", + "print(calculate(10, 5, '+')) \n", + "print(calculate(10, 5, '-')) \n", + "print(calculate(10, 5, '*')) \n", + "print(calculate(10, 5, '/'))\n", + " " ] }, { @@ -192,12 +292,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "7\n", + "24\n", + "5.0\n" + ] + } + ], "source": [ - "# your code goes here" + "def addition(*args):\n", + " return sum(args)\n", + "\n", + "def subtraction(*args):\n", + " # Start from the first number and subtract the rest\n", + " it = iter(args)\n", + " result = next(it) # Start with the first number\n", + " for num in it:\n", + " result -= num\n", + " return result\n", + "\n", + "def multiplication(*args):\n", + " result = 1\n", + " for num in args:\n", + " result *= num\n", + " return result\n", + "\n", + "def division(a, b):\n", + " if b == 0:\n", + " raise ValueError(\"Cannot divide by zero.\")\n", + " return a / b\n", + "\n", + "def calculate(operator, *operands):\n", + " if operator == '+':\n", + " return addition(*operands)\n", + " elif operator == '-':\n", + " return subtraction(*operands)\n", + " elif operator == '*':\n", + " return multiplication(*operands)\n", + " elif operator == '/':\n", + " if len(operands) != 2:\n", + " raise ValueError(\"Division requires exactly two operands.\")\n", + " return division(operands[0], operands[1])\n", + " else:\n", + " raise ValueError(\"Unsupported operator. Use '+', '-', '*', or '/'.\")\n", + "\n", + "# Testing the updated calculate function\n", + "print(calculate('+', 1, 2, 3)) # Output: 6\n", + "print(calculate('-', 10, 2, 1)) # Output: 7\n", + "print(calculate('*', 3, 4, 2)) # Output: 24\n", + "print(calculate('/', 10, 2)) # Output: 5.0" ] }, { @@ -276,13 +427,73 @@ "execution_count": null, "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" + ] + }, + { + "ename": "NameError", + "evalue": "name 'word_count' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 2\u001b[39m get_ipython().run_line_magic(\u001b[33m'\u001b[39m\u001b[33mload_ext\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mautoreload\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m 3\u001b[39m get_ipython().run_line_magic(\u001b[33m'\u001b[39m\u001b[33mautoreload\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33m2\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mfunctions\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m get_unique_list_f\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mfunctions\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m count_case_f\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\Ready2Use\\Desktop\\my-folder\\Ironhack-week1\\Day2\\lab-python-functions-extra\\functions.py:66\u001b[39m\n\u001b[32m 64\u001b[39m count_words = \u001b[38;5;28mlen\u001b[39m(words)\n\u001b[32m 65\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m count_words\n\u001b[32m---> \u001b[39m\u001b[32m66\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m'\u001b[39m\u001b[33mthere are \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mword_count\u001b[49m(sentence)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m words in sentence\u001b[39m\u001b[33m'\u001b[39m)\n", + "\u001b[31mNameError\u001b[39m: name 'word_count' is not defined" + ] + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\n", "%autoreload 2 \n", - "\n", - "# your code goes here" + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "b689d3f0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "from functions import get_unique_list_f\n", + "lst= [1,2,3,3,3,3,4,5]\n", + "print(get_unique_list_f(lst))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b7dc76d3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(8, 2)\n" + ] + } + ], + "source": [ + "from functions import count_case_f\n", + "string = \"Hello World\"\n", + "print(count_case_f(string))" ] }, { @@ -315,18 +526,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def get_fibonacci(n):\n", + " # Base cases: 0th Fibonacci number is 0, 1st Fibonacci number is 1\n", + " if n == 0:\n", + " return 0\n", + " elif n == 1:\n", + " return 1\n", + " # Recursive case: Sum of the two preceding numbers\n", + " else:\n", + " return get_fibonacci(n - 1) + get_fibonacci(n - 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ac29e480", + "metadata": {}, + "outputs": [], + "source": [ + "def generate_fibonacci_list(n):\n", + " # Create a list of Fibonacci numbers from 0 to n\n", + " fib_list = []\n", + " for i in range(n + 1):\n", + " fib_list.append(get_fibonacci(i))\n", + " return fib_list" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "dfa05f9c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The 42th Fibonacci number is: 267914296\n", + "Fibonacci numbers from 0 to 42: [0, 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]\n" + ] + } + ], + "source": [ + "# Calculate the 10th Fibonacci number\n", + "n = 42\n", + "fib_number = get_fibonacci(n)\n", + "print(f\"The {n}th Fibonacci number is: {fib_number}\")\n", + "\n", + "# Generate a list of Fibonacci numbers from 0 to 10\n", + "fib_list = generate_fibonacci_list(n)\n", + "print(f\"Fibonacci numbers from 0 to {n}: {fib_list}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +600,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,