diff --git a/python_functions_exercises.ipynb b/python_functions_exercises.ipynb index 90ce7b7..26b6bff 100644 --- a/python_functions_exercises.ipynb +++ b/python_functions_exercises.ipynb @@ -20,11 +20,28 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3000.0555\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def find_max_of_three(num1, num2, num3):\n", + " if num1 > num2 and num1 > num3:\n", + " return num1\n", + " elif num2 > num1 and num2 > num3:\n", + " return num2\n", + " else:\n", + " return num3\n", + " \n", + "print(find_max_of_three(-1, 20.5, 3000.0555)) " ] }, { @@ -48,11 +65,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def calc_sum (numbers):\n", + " '''This function takes a list of numbers as input and returns the sum of the numbers\n", + " \n", + " '''\n", + " sum = 0\n", + " for number in numbers:\n", + " sum += number\n", + " return sum\n", + "\n", + "print(calc_sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))" ] }, { @@ -77,11 +112,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3628800\n" + ] + } + ], "source": [ - "# Your code here" + "def multiply(numbers : list[int]) -> int:\n", + " '''This function takes a list of numbers as input and returns the product of the numbers\n", + " \n", + " '''\n", + " product = 1\n", + " for number in numbers:\n", + " product *= number\n", + " return product\n", + "\n", + "print(multiply([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))" ] }, { @@ -106,11 +158,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "!dlroW ,olleH\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "def reverse_string(string):\n", + " '''This function takes a string as input and returns the reverse of the string\n", + " \n", + " '''\n", + " return string[::-1]\n", + "\n", + "print(reverse_string(\"Hello, World!\"))" ] }, { @@ -124,11 +191,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "120\n", + "1\n", + "Please enter a non-negative integer\n", + "None\n" + ] + } + ], + "source": [ + "def factorial(n : int) -> int:\n", + " '''This function takes a non-negative integer as input and returns the factorial of the integer\n", + " \n", + " '''\n", + " if n >= 0:\n", + " mul = 1\n", + " for i in range(1, n + 1):\n", + " mul *= i\n", + " return mul\n", + " else:\n", + " print(\"Please enter a non-negative integer\")\n", + "\n", + "print(factorial(5))\n", + "print(factorial(0))\n", + "print(factorial(-5))" ] }, { @@ -142,11 +234,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def is_inrange (number : float, lower : float, upper : float) -> bool:\n", + " '''This function takes three numbers as input and returns True if the first number is in the range of the second and third numbers, otherwise it returns False\n", + " \n", + " '''\n", + " if number >= lower and number <= upper:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "print(is_inrange(5, 1, 10))\n", + "print(is_inrange(-5, -5, 12.0))\n", + "print(is_inrange(5, 1, 4))" ] }, { @@ -176,10 +290,39 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase letters: 2\n", + "Lowercase letters: 8\n", + "(2, 8)\n" + ] + } + ], + "source": [ + "def count_upper_lower(string : str) -> tuple:\n", + " '''This function takes a string as input and returns a tuple containing the count of uppercase and lowercase letters in the string\n", + " \n", + " '''\n", + " upper = 0\n", + " lower = 0\n", + " for char in string:\n", + " if char.isupper():\n", + " upper += 1\n", + " elif char.islower():\n", + " lower += 1\n", + " print(f\"Uppercase letters: {upper}\")\n", + " print(f\"Lowercase letters: {lower}\")\n", + " return (upper, lower)\n", + "\n", + "\n", + "count = count_upper_lower(\"Hello, World!\")\n", + "print(count)\n", + "\n", "# Your code here" ] }, @@ -205,11 +348,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n", + "['f', 'd', 'a', 'g']\n" + ] + } + ], "source": [ - "# Your code here" + "def unique(lst_un):\n", + " unique_list = []\n", + " unique_set = set()\n", + " [unique_set.add(i) for i in lst_un]\n", + " unique_list = [element for element in unique_set]\n", + " \n", + " return unique_list # Your code here\n", + "\n", + "print(unique([1, 2, 3, 3, 3, 3, 4, 5]))\n", + "print(unique([\"a\", \"a\", \"d\", \"d\", \"f\", \"g\", \"g\", \"g\"]))" ] }, { @@ -223,11 +384,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def prime_checker (n: int) -> bool:\n", + " '''This function takes an integer as input and returns True if the integer is a prime number, otherwise it returns False\n", + " \n", + " '''\n", + " if n > 1:\n", + " for i in range(2, n):\n", + " if n % i == 0:\n", + " return False\n", + " return True\n", + " else:\n", + " return False \n", + " \n", + "print(prime_checker(6))\n", + "print(prime_checker(7))" ] }, { @@ -252,11 +436,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 8, 10]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "def find_even(lst):\n", + " '''This function takes a list of numbers as input and returns a list of even numbers from the input list\n", + " \n", + " '''\n", + " even_list = []\n", + " for number in lst:\n", + " if number % 2 == 0:\n", + " even_list.append(number)\n", + " return even_list\n", + "\n", + "find_even([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" ] }, { @@ -270,11 +476,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "False\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def perfect_number_checker(n: int) -> bool:\n", + " '''This function takes an integer as input and returns True if the integer is a perfect number, otherwise it returns False\n", + " \n", + " '''\n", + " sum = 0\n", + " for i in range(1, n//2 + 1):\n", + " if n % i == 0:\n", + " sum += i\n", + " if sum == n:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "print(perfect_number_checker(6))\n", + "print(perfect_number_checker(28))\n", + "print(perfect_number_checker(12))\n", + "print(perfect_number_checker(496))\n", + "print(perfect_number_checker(14))" ] }, { @@ -288,11 +524,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def palindrome_checker (string: str) -> bool:\n", + " '''This function takes a string as input and returns True if the string is a palindrome, otherwise it returns False\n", + " \n", + " '''\n", + " string = string.lower()\n", + " punctuation = \" .,:-!?()\"\n", + " for char in string:\n", + " if char in punctuation:\n", + " string = string.replace(char, \"\")\n", + " \n", + " if string == string[::-1]:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "print(palindrome_checker(\"madam\"))\n", + "print(palindrome_checker(\"Hello, World!\"))\n", + "print(palindrome_checker(\"Was it a car or a cat I saw\")) \n", + "print(palindrome_checker(\"Я не стар, брат Сеня\"))" ] }, { @@ -306,11 +572,66 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "97\n", + " 1 \n", + " 1 1 \n", + " 1 2 1 \n", + " 1 3 3 1 \n", + " 1 4 6 4 1 \n", + " 1 5 10 10 5 1 \n", + " 1 6 15 20 15 6 1 \n", + " 1 7 21 35 35 21 7 1 \n", + " 1 8 28 56 70 56 28 8 1 \n", + " 1 9 36 84 126 126 84 36 9 1 \n", + " 1 10 45 120 210 252 210 120 45 10 1 \n", + " 1 11 55 165 330 462 462 330 165 55 11 1 \n", + " 1 12 66 220 495 792 924 792 495 220 66 12 1 \n", + " 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 \n", + " 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 \n", + " 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 \n", + " 1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 \n", + " 1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1 \n", + " 1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1 \n", + " 1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1 \n" + ] + } + ], + "source": [ + "# Your code here\n", + "def pascal_triangle(n: int) :\n", + " '''This function takes an integer as input and prints the Pascal's triangle with n rows\n", + " \n", + " '''\n", + " triangle = [[1],[1,1]]\n", + " for i in range(2, n):\n", + " prev_row = triangle[i - 1]\n", + " row = [1]\n", + " for j in range(1, i):\n", + " row.append(prev_row[j - 1] + prev_row[j])\n", + " row.append(1)\n", + " \n", + " triangle.append(row)\n", + " \n", + " last_row = \" \"\n", + " for i in triangle[-1]:\n", + " last_row += str(i) + \" \"\n", + " last_row_len = len(last_row)\n", + " print(last_row_len)\n", + "\n", + " for row in triangle:\n", + " row_str = \" \"\n", + " for i in row:\n", + " row_str += str(i) + \" \"\n", + " print(' '*((last_row_len - len(row_str)) // 2), row_str, ' '*((last_row_len - len(row_str)) // 2))\n", + " \n", + "pascal_triangle(20)" ] }, { @@ -335,11 +656,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def pangram(string):\n", + " alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n", + " for letter in alphabet:\n", + " if letter not in string.lower():\n", + " return False\n", + " return True\n", + "#your code here\n", + "\n", + "\n", + "print(pangram(\"The quick brown fox jumps over the lazy dog\"))\n", + "print(pangram(\"The quick brown fox jumps over the dog\"))\n", + "print(pangram(\"Hello, World!\"))" ] }, { @@ -364,11 +707,44 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple-banana-kiwi-orange\n", + "black-green-red-white-yellow\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def sort_alpha(string):\n", + " \n", + " word_list = []\n", + " word = \"\" \n", + " string += \"-\"\n", + " \n", + " for c in string:\n", + " if c == \"-\":\n", + " word_list.append(word)\n", + " word = \"\"\n", + " else:\n", + " word += c\n", + " \n", + " word_list.sort()\n", + " \n", + " new_string = \"\"\n", + " for word in word_list:\n", + " new_string += word + \"-\"\n", + "\n", + " return new_string[:-1]\n", + "#your code here\n", + "\n", + "print(sort_alpha(\"banana-apple-kiwi-orange\"))\n", + "print(sort_alpha(\"green-red-yellow-black-white\"))" ] }, { @@ -382,11 +758,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900]\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def squares(n):\n", + " list_of_squares = [[i**2 for i in range(1, n + 1)]]\n", + " print(*list_of_squares)\n", + " return \n", + "\n", + "squares(30)\n" ] }, { @@ -400,11 +790,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1mHello, World!\u001b[0m\n", + "\u001b[3mHello, World!\u001b[0m\n" + ] + } + ], + "source": [ + "# Your code here\n", + "def deco_print(string, deco_type):\n", + " if deco_type == \"upper\":\n", + " print(string.upper())\n", + " elif deco_type == \"lower\":\n", + " print(string.lower())\n", + " elif deco_type == \"title\":\n", + " print(string.title())\n", + " elif deco_type == \"bold\":\n", + " print(f\"\\033[1m{string}\\033[0m\")\n", + " elif deco_type == \"italic\":\n", + " print(f\"\\033[3m{string}\\033[0m\")\n", + "\n", + " else:\n", + " print(string)\n", + "\n", + "deco_print(\"Hello, World!\", \"bold\")\n", + "deco_print(\"Hello, World!\", \"italic\")" ] }, { @@ -418,11 +834,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900]\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "squares_30 = '''\n", + "list_of_squares = [i**2 for i in range(1, 31)]\n", + "print(list_of_squares)\n", + "'''\n", + "\n", + "exec(squares_30)\n" ] }, { @@ -436,11 +866,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the main function\n", + "Calling a sub-function: \n", + "This is a sub-function\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def sub_function():\n", + " print(\"This is a sub-function\")\n", + "\n", + "def main_function():\n", + " print(\"This is the main function\")\n", + " print(\"Calling a sub-function: \")\n", + " sub_function()\n", + "\n", + "main_function()" ] }, { @@ -463,11 +912,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 111, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of local variables: 5\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def example_function(x, y):\n", + " a = 10.5\n", + " b = True\n", + " c = \"Str\"\n", + " return len(locals())\n", + "\n", + "num_local_vars = example_function(1, 7)\n", + "print(f\"Number of local variables: {num_local_vars}\")" ] }, { @@ -495,7 +960,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.1" } }, "nbformat": 4,