From a10bae29643d21fe15ebfe635354164d37aa7b2a Mon Sep 17 00:00:00 2001 From: Aminmoh9 <amin.moh2017@gmail.com> Date: Sun, 16 Feb 2025 05:36:14 +0100 Subject: [PATCH] Lab is solved. --- python_functions_exercises.ipynb | 662 +++++++++++++++++++++++++++++-- 1 file changed, 618 insertions(+), 44 deletions(-) diff --git a/python_functions_exercises.ipynb b/python_functions_exercises.ipynb index 90ce7b7..6c0d527 100644 --- a/python_functions_exercises.ipynb +++ b/python_functions_exercises.ipynb @@ -24,7 +24,34 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def max_number(x,y,z):\n", + " if x >y and x>z:\n", + " return x\n", + " elif y>x and y>z:\n", + " return y\n", + " else:\n", + " return z" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max_number(1,2,3)" ] }, { @@ -48,11 +75,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def sum_all(list):\n", + " \n", + " sum_all=0\n", + " for number in list:\n", + " sum_all+=number\n", + " return sum_all\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_all([1,2,3,4,5])" ] }, { @@ -77,11 +130,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def mult_all(list):\n", + " \n", + " mult_all=1\n", + " for number in list:\n", + " mult_all*=number\n", + " return mult_all" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mult_all([1,2,3,4,5])" ] }, { @@ -106,11 +185,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "def reverse_string(string):\n", + " return string[::-1]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'4321dcba'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "reverse_string(\"abcd1234\")" ] }, { @@ -124,11 +225,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def factorial(n):\n", + " \n", + " if n ==0 or n == 1:\n", + " return 1\n", + " else:\n", + " result=1\n", + " for i in range(2,n+1):\n", + " result*=i\n", + " return result\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "factorial(5)" ] }, { @@ -142,11 +272,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def is_in_range(number,lower, upper):\n", + " return lower<= number <=upper" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_in_range(10, 5, 100)" ] }, { @@ -176,11 +328,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def count_upper_lower(string):\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" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase letters: 2\n", + "Lowercase letters: 3\n" + ] + }, + { + "data": { + "text/plain": [ + "(2, 3)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count_upper_lower(\"HellO\")" ] }, { @@ -205,11 +397,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def unique_elements(lst):\n", + " return list(set(lst))\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_elements([1,2,2,3,4,5,6,6])" ] }, { @@ -223,11 +439,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def is_prime(n):\n", + " if n<=1:\n", + " return False\n", + " for i in range(2,n):\n", + " if n % i ==0:\n", + " return False\n", + " \n", + " return True\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_prime(17)" ] }, { @@ -252,11 +496,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def even_numbers(lst):\n", + " even=[]\n", + "\n", + " for i in lst:\n", + " if i % 2 ==0:\n", + " even.append(i)\n", + " return even\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 8, 10]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "even_numbers([1,2,3,4,5,6,7,8,9,10])" ] }, { @@ -270,11 +542,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def is_perfec(n):\n", + " if n<=1:\n", + " return False\n", + " divisors_sum=0\n", + " for i in range(1,n):\n", + " if n % i ==0:\n", + " divisors_sum +=i\n", + " return divisors_sum ==n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "print(is_perfec(5))\n", + "print(is_perfec(6))\n", + "print(is_perfec(10))\n", + "print(is_perfec(28))\n" ] }, { @@ -288,11 +591,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def is_palidrome(string):\n", + " return string.lower() == string.lower() [::-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "print(is_palidrome(\"Hello\"))\n", + "print(is_palidrome(\"Madam\"))" ] }, { @@ -306,11 +630,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def pascal_triangle(n):\n", + " triangle=[]\n", + " for i in range(n):\n", + " row=[1]*(i+1)\n", + "\n", + " for j in range(1,i):\n", + " row[j]=triangle[i-1][j-1]+ triangle[i-1][j]\n", + " triangle.append(row)\n", + " \n", + " for row in triangle:\n", + " print(row)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1]\n", + "[1, 1]\n", + "[1, 2, 1]\n", + "[1, 3, 3, 1]\n", + "[1, 4, 6, 4, 1]\n" + ] + } + ], + "source": [ + "pascal_triangle(5)" ] }, { @@ -335,11 +691,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def is_pangram(string):\n", + "#your code here\n", + " string= string.lower()\n", + "\n", + " letter_set=set() # Create a set to store unique values\n", + "\n", + " for char in string:\n", + " if 'a' <= char <= 'z': #check if the character is letter\n", + " letter_set.add(char)\n", + " return len(letter_set) == 26" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_pangram(\"The quick brown fox jumps over the lazy dog\")" ] }, { @@ -364,11 +750,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def sort_hyphen_word(string):\n", + " words=[]\n", + " word=\"\"\n", + "\n", + " for char in string:\n", + " if char == \"-\":\n", + " words.append(word)\n", + " word=\"\"\n", + " else:\n", + " word +=char\n", + " words.append(word)\n", + " words.sort()\n", + "\n", + " sorted_string=\"\"\n", + " for i in range(len(words)):\n", + " sorted_string+=words[i]\n", + " if i < len(words)-1:\n", + " sorted_string += '-'\n", + " return sorted_string" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'black-green-red-white-yellow'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sort_hyphen_word(\"green-red-yellow-black-white\")" ] }, { @@ -382,11 +807,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def square_numbers(n):\n", + " for i in range(1,n+1):\n", + " squares= i**2\n", + " print(squares)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "4\n", + "9\n", + "16\n", + "25\n", + "36\n", + "49\n", + "64\n", + "81\n", + "100\n", + "121\n", + "144\n", + "169\n", + "196\n", + "225\n", + "256\n", + "289\n", + "324\n", + "361\n", + "400\n", + "441\n", + "484\n", + "529\n", + "576\n", + "625\n", + "676\n", + "729\n", + "784\n", + "841\n", + "900\n" + ] + } + ], + "source": [ + "square_numbers(30)" ] }, { @@ -400,11 +876,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1m\u001b[3mHello, World!\u001b[0m\u001b[0m\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def bold(func):\n", + " def wrapper(text):\n", + " return f\"\\033[1m{func(text)}\\033[0m\" \n", + " return wrapper\n", + "\n", + "\n", + "def italic(func):\n", + " def wrapper(text):\n", + " return f\"\\033[3m{func(text)}\\033[0m\" \n", + " return wrapper\n", + "\n", + "\n", + "@bold\n", + "@italic\n", + "def format_text(text):\n", + " return text\n", + "\n", + "\n", + "formatted_text = format_text(\"Hello, World!\")\n", + "print(formatted_text)\n", + "\n" ] }, { @@ -418,11 +924,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "code_string= \"\"\"\n", + "def is_perfect(n):\n", + " if n <= 1:\n", + " return False\n", + " divisors_sum = 0\n", + " for i in range(1, n):\n", + " if n % i == 0:\n", + " divisors_sum += i\n", + " return divisors_sum == n\n", + "\n", + "# Execute the function with a sample number\n", + "result = is_perfect(6)\n", + "\"\"\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "exec(code_string)\n", + "print(result)" ] }, { @@ -436,11 +973,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sum of 2 and 4: 6\n", + "Square of 6: 36\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def square(num):\n", + " print(f\"Square of {num}: {num ** 2}\")\n", + "\n", + "def sum(x, y):\n", + " result = x + y \n", + " print(f\"Sum of {x} and {y}: {result}\")\n", + " square(result)\n", + "\n", + "sum(2,4)\n" ] }, { @@ -463,11 +1018,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def count_local_variables():\n", + " a=1\n", + " b=3\n", + " c=5\n", + " local_vars=locals()\n", + " return len(local_vars)\n", + "\n", + "count_local_variables()" ] }, { @@ -481,7 +1055,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -495,7 +1069,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.12.7" } }, "nbformat": 4,