Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 204 additions & 24 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
]
},
{
Expand All @@ -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",
Expand All @@ -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"
]
},
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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"
]
},
{
Expand All @@ -145,7 +181,8 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"word_count('hi, how was your day')"
]
},
{
Expand All @@ -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,'+')"
]
},
{
Expand All @@ -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='+')"
]
},
{
Expand Down Expand Up @@ -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')"
]
},
{
Expand Down Expand Up @@ -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)<num:\n",
" #seq=seq.append(seq[-1])\n",
" x=(seq[-1]+seq[-2])\n",
" seq.append(x)\n",
" return seq\n",
"\n",
"fib_seq(10)\n",
"\n",
" "
]
}
],
Expand All @@ -340,7 +520,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down