Skip to content

Commit eac83fd

Browse files
added recursive function
1 parent 36e5e7c commit eac83fd

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

.ipynb_checkpoints/Python Toolkit-checkpoint.ipynb

+38-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"cell_type": "markdown",
3333
"metadata": {},
3434
"source": [
35-
"### Checking for PEP-8 errors\n",
35+
"#### Checking for PEP-8 errors\n",
3636
"\n",
3737
" import pycodestyle\n",
3838
"\n",
@@ -84,7 +84,7 @@
8484
"cell_type": "markdown",
8585
"metadata": {},
8686
"source": [
87-
"### Help function\n",
87+
"#### Help function\n",
8888
"\n",
8989
"Using before just about anything in the parentheses will bring up useful information on that call\n",
9090
"\n",
@@ -481,6 +481,39 @@
481481
"- return (data)"
482482
]
483483
},
484+
{
485+
"cell_type": "markdown",
486+
"metadata": {},
487+
"source": [
488+
"#### Recursive Functions\n",
489+
"Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition fulfills the condition of recursion, we call this function a recursive function.\n",
490+
"\n",
491+
"Termination condition:\n",
492+
"A recursive function has to terminate to be used in a program. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not met in the calls.\n",
493+
"\n",
494+
"##### Fibonacci Sequence\n",
495+
" def fib(n):\n",
496+
" if n < 1:\n",
497+
" return \"N must be an integer greater then 1\" #will notify that positive integer is needed\n",
498+
" elif n in [1,2]:\n",
499+
" return 1 #since addition of first two elements differs from the rest, need the elif statement\n",
500+
" else:\n",
501+
" return fib(n-1) + fib(n-2) #recursive part that takes the number provided, and returns the proper calculation\n",
502+
" \n",
503+
" for i in range(1, 10):\n",
504+
" print(fib(i))\n",
505+
" \n",
506+
"##### Flatten a List \n",
507+
" def flat_list(L, result=[]):\n",
508+
" print('Current L:', L) #Optional, to display process\n",
509+
" for i in L:\n",
510+
" if type(i) == list:\n",
511+
" flat_list(i, result)\n",
512+
" else:\n",
513+
" result.append(i)\n",
514+
" return result"
515+
]
516+
},
484517
{
485518
"cell_type": "markdown",
486519
"metadata": {},
@@ -542,7 +575,7 @@
542575
"source": [
543576
"#### Regular Expressions\n",
544577
"- import re - Import the Regular Expressions module\n",
545-
"- re.search(r\"abc\",string) - Returns a match object if the regex \"abc\" is found in s, otherwise None\n",
578+
"- re.search(r\"abc\",string) - Returns a match object if the regex \"abc\" is found in string, otherwise None\n",
546579
"- re.match(r\"abc\",string) - same as search, but is specific\n",
547580
"- re.split(r'dilimiter',string) - splits string by delimiter provided\n",
548581
"- re.sub(r\"abc\",\"xyz\",string) - Returns a string where all instances matching regex \"abc\" are replaced by \"xyz\"\n",
@@ -581,7 +614,7 @@
581614
" notmatch_tag = re.match(r\"<(\\w+)>\", string)\n",
582615
" #Print the first group capture\n",
583616
" print(\"Close your {} tag!\".format(notmatch_tag.group(1)))\n",
584-
"##### lookaround\n",
617+
"##### lookaround\n",
585618
"- regex(?=reference) = lookup word before the reference\n",
586619
"- regex(?!reference) = avoid looking up the word before reference\n",
587620
"- (?<=reference)regex = lookup word after the reference\n",
@@ -705,7 +738,7 @@
705738
"name": "python",
706739
"nbconvert_exporter": "python",
707740
"pygments_lexer": "ipython3",
708-
"version": "3.7.4"
741+
"version": "3.7.5"
709742
},
710743
"toc": {
711744
"base_numbering": 1,

Python Toolkit.ipynb

+36-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"cell_type": "markdown",
3333
"metadata": {},
3434
"source": [
35-
"### Checking for PEP-8 errors\n",
35+
"#### Checking for PEP-8 errors\n",
3636
"\n",
3737
" import pycodestyle\n",
3838
"\n",
@@ -84,7 +84,7 @@
8484
"cell_type": "markdown",
8585
"metadata": {},
8686
"source": [
87-
"### Help function\n",
87+
"#### Help function\n",
8888
"\n",
8989
"Using before just about anything in the parentheses will bring up useful information on that call\n",
9090
"\n",
@@ -481,6 +481,39 @@
481481
"- return (data)"
482482
]
483483
},
484+
{
485+
"cell_type": "markdown",
486+
"metadata": {},
487+
"source": [
488+
"#### Recursive Functions\n",
489+
"Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition fulfills the condition of recursion, we call this function a recursive function.\n",
490+
"\n",
491+
"Termination condition:\n",
492+
"A recursive function has to terminate to be used in a program. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not met in the calls.\n",
493+
"\n",
494+
"##### Fibonacci Sequence\n",
495+
" def fib(n):\n",
496+
" if n < 1:\n",
497+
" return \"N must be an integer greater then 1\" #will notify that positive integer is needed\n",
498+
" elif n in [1,2]:\n",
499+
" return 1 #since addition of first two elements differs from the rest, need the elif statement\n",
500+
" else:\n",
501+
" return fib(n-1) + fib(n-2) #recursive part that takes the number provided, and returns the proper calculation\n",
502+
" \n",
503+
" for i in range(1, 10):\n",
504+
" print(fib(i))\n",
505+
" \n",
506+
"##### Flatten a List \n",
507+
" def flat_list(L, result=[]):\n",
508+
" print('Current L:', L) #Optional, to display process\n",
509+
" for i in L:\n",
510+
" if type(i) == list:\n",
511+
" flat_list(i, result)\n",
512+
" else:\n",
513+
" result.append(i)\n",
514+
" return result"
515+
]
516+
},
484517
{
485518
"cell_type": "markdown",
486519
"metadata": {},
@@ -705,7 +738,7 @@
705738
"name": "python",
706739
"nbconvert_exporter": "python",
707740
"pygments_lexer": "ipython3",
708-
"version": "3.7.4"
741+
"version": "3.7.5"
709742
},
710743
"toc": {
711744
"base_numbering": 1,

0 commit comments

Comments
 (0)