diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..cbfa4d4 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +54,7 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " return list(set(lst))\n" ] }, { @@ -60,10 +71,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 1 , Lowercase count: 4\n" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +94,12 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " count_upper=len(list(filter(lambda x:x.isupper(),string)))\n", + " count_lower=len(list(filter(lambda x:x.islower(),string)))\n", + " #return (('Uppercase count:',(count_upper)),('Lowercase count: ',(count_lower)))\n", + " return count_upper, count_lower\n", + "# x,y=count_case('Hello')\n", + "# print('Uppercase count: ',(x),', Lowercase count: ',(y))\n" ] }, { @@ -92,10 +116,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import string\n", "\n", @@ -109,7 +144,7 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " return sentence.translate(str.maketrans(\"\", \"\", \".,!?\"))\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,7 +157,8 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " no_punct=remove_punctuation(sentence)\n", + " return len(no_punct.split())\n" ] }, { @@ -145,7 +181,8 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "word_count('hi, how was your day')" ] }, { @@ -168,12 +205,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "def adding_func(a,b):\n", + " return a+b\n", + " \n", + "def subtract_func(a,b):\n", + " return a-b\n", + " \n", + "def mult_func(a,b):\n", + " return a*b\n", + "\n", + "def div_func(a,b):\n", + " return a/b\n", + "\n", + "def calculate(a,b,c):\n", + " if c == '+':\n", + " return adding_func(a,b)\n", + " elif c == '-':\n", + " return subtract_func(a,b)\n", + " elif c == '*':\n", + " return mult_func(a,b)\n", + " else:\n", + " return div_func(a,b)\n", + "\n", + "calculate(4,5,'+')" ] }, { @@ -192,12 +264,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "# your code goes here\n", + "from functools import reduce\n", + "\n", + "def adding_func(*args):\n", + " return reduce(lambda a,b:a+b,args)\n", + " \n", + "def subtract_func(*args):\n", + " return reduce(lambda a,b:a-b,args)\n", + " \n", + "def mult_func(*args):\n", + " return reduce(lambda a,b:a*b,args)\n", + "\n", + "def div_func(*args):\n", + " return a/b\n", + "\n", + "def calculate(*args:int,c):\n", + " if c == '+':\n", + " return adding_func(*args)\n", + " elif c == '-':\n", + " return subtract_func(*args)\n", + " elif c == '*':\n", + " return mult_func(*args)\n", + " else:\n", + " return div_func(*args)\n", + "\n", + "calculate(4,5,6,c='+')" ] }, { @@ -273,16 +382,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "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" + ] + }, + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# IPython extension to reload modules before executing user code.\n", "%load_ext autoreload\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\n", + "\n", + "word_count_f('hi how are you')" ] }, { @@ -315,12 +446,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, + "id": "0a1d9bc8-7917-4689-a8fe-74b1043a8f5f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "377" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def fibonacci(n):\n", + " if n <= 1:\n", + " return n\n", + " else:\n", + " return fibonacci(n-1) + fibonacci(n-2)\n", + "\n", + "fibonacci(14)" + ] + }, + { + "cell_type": "code", + "execution_count": 79, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def fib_seq(num:int):\n", + " seq=list(range(2))\n", + " while len(seq)