From ab62511ecd602031dd0da1466641b608fc11b3ed Mon Sep 17 00:00:00 2001 From: FaayPi Date: Fri, 31 Jan 2025 15:59:02 +0100 Subject: [PATCH] Lab submission Python Functions Exercises by Fee --- python_functions_exercises.ipynb | 404 +++++++++++++++++++++++++++---- 1 file changed, 358 insertions(+), 46 deletions(-) diff --git a/python_functions_exercises.ipynb b/python_functions_exercises.ipynb index 90ce7b7..8623e72 100644 --- a/python_functions_exercises.ipynb +++ b/python_functions_exercises.ipynb @@ -20,11 +20,42 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "def max_value(x, y, z):\n", + " \n", + " max_value = 0\n", + " \n", + " if x > y and x > z:\n", + " max_value = x\n", + " if y > x and y > z:\n", + " max_value = y\n", + " if z > x and z > y:\n", + " max_value = z\n", + " else:\n", + " return \"not valid\"\n", + "\n", + " return max_value " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "print(max_value(1, 2, 3))" ] }, { @@ -48,11 +79,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "28\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def total_sum(list):\n", + "\n", + " total_sum = 0\n", + "\n", + " for i in list:\n", + " total_sum += i\n", + "\n", + " return total_sum \n", + "\n", + "print(total_sum([1, 5, 6, 2, 9, 5]))\n" ] }, { @@ -77,11 +126,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2700\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def multiples(list):\n", + "\n", + " total_mult = 1\n", + "\n", + " for i in list:\n", + " total_mult *= i\n", + "\n", + " return total_mult \n", + "\n", + "print(multiples([1, 5, 6, 2, 9, 5]))" ] }, { @@ -106,11 +174,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ollaH\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "\n", + "def reverse(word: str) -> str:\n", + " reversed = \"\"\n", + " for i in word:\n", + " reversed = i + reversed\n", + " return reversed\n", + "\n", + "print(reverse(\"Hallo\"))\n" ] }, { @@ -124,11 +208,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "24\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def factorial(n):\n", + " \n", + " factorial_number = 1\n", + "\n", + " for i in range(1,n+1):\n", + " factorial_number *= i\n", + " return factorial_number\n", + "\n", + "print(factorial(4))" ] }, { @@ -142,11 +243,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def checkrange(n):\n", + "\n", + " if n in range(101):\n", + " return True\n", + " else: \n", + " return False\n", + "\n", + "print(checkrange(99))\n" ] }, { @@ -176,11 +294,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 4, Lowercase count: 9\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def upper_lower_count(sentence: str) -> str:\n", + "\n", + " upper = 0\n", + " lower = 0\n", + "\n", + " for i in sentence:\n", + " if i.islower():\n", + " lower += 1\n", + " elif i.isupper():\n", + " upper += 1\n", + "\n", + " return f\"Uppercase count: {upper}, Lowercase count: {lower}\"\n", + "\n", + "print(upper_lower_count(\"Hallo Ich BiN es!\"))\n", + " \n", + "\n", + " " ] }, { @@ -205,11 +349,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 2, 5, 6]\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def list_unique(list):\n", + "\n", + " unique = []\n", + "\n", + " for i in list:\n", + " if i not in unique:\n", + " unique.append(i)\n", + " return unique\n", + "\n", + "print(list_unique([1, 4, 2, 4, 5, 6, 6]))" ] }, { @@ -223,11 +385,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "# Your code here\n", + "import math\n", + "\n", + "def prime(num: int) -> bool:\n", + " \n", + " if num == 2:\n", + " return True\n", + " \n", + " if num % 2 == 0:\n", + " return False\n", + " \n", + " for i in range(3, int(math.sqrt(num)) + 1, 2): #I got this hint from google. Otherwise I don't know how to calculate the root of a number ...\n", + " if num % i == 0:\n", + " return False\n", + " \n", + " return True\n", + "\n", + "print(prime(9))\n" ] }, { @@ -252,11 +439,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 2]\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "def even_numbers(list):\n", + "\n", + " even = []\n", + "\n", + " for i in list:\n", + " if i % 2 == 0:\n", + " even.append(i)\n", + " return even\n", + "\n", + "\n", + "print(even_numbers([1, 3, 4, 7, 2]))" ] }, { @@ -274,7 +480,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def perfect_number_checker(num: int) -> str\n", + "\n", + "\n", + "\n" ] }, { @@ -288,11 +499,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "no palindrom\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def palindrom(word: str) -> str:\n", + " reversed = \"\"\n", + " for i in word:\n", + " reversed = i + reversed\n", + "\n", + " if reversed == word:\n", + " return f\"palindrom! {word} / {reversed}\"\n", + " \n", + " else:\n", + " return \"no palindrom\"\n", + "\n", + "\n", + "print(palindrom(\"Hallo\"))" ] }, { @@ -310,7 +543,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def pascals_triangle(n: int):\n", + "\n", + " \n", + "\n" ] }, { @@ -335,11 +573,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "String is no pangram\n" + ] + } + ], "source": [ - "# Your code here" + "# Your code here\n", + "\n", + "def pangram(string):\n", + "#your code here\n", + "\n", + " alphabet = \"abcdefghijklmnopqrstuvwxyz\" # define letters of alphabet\n", + "\n", + " alphabet_dict = {letter: 0 for letter in alphabet} # create a dictionary with a key for each letter from the alphabet\n", + "\n", + " for i in string.lower(): # iterate over the string (lower letters) and if i matches key in dict --> value +=1\n", + " if i in alphabet_dict:\n", + " alphabet_dict[i] += 1\n", + "\n", + " if all(count > 0 for count in alphabet_dict.values()): # if all values > 0 --> return message \"string is pangram\", if not \"string is no pangram\"\n", + " return \"String is pangram\"\n", + " else:\n", + " return \"String is no pangram\" \n", + " \n", + "\n", + "print(pangram(\"abcdefghijklmnopqrz\"))\n", + "\n" ] }, { @@ -382,11 +648,57 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" + "execution_count": 94, + "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": [ + "# Your code here\n", + "\n", + "def squares():\n", + "\n", + "\n", + " for i in range(1,31):\n", + " num = i ** 2\n", + " print(num)\n", + "\n", + "squares()" ] }, { @@ -481,7 +793,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -495,7 +807,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.1" } }, "nbformat": 4,