Recursion - 3 - Function - Vha - Jupyter Notebook
Recursion - 3 - Function - Vha - Jupyter Notebook
What is Recursion?
Recursion = a way of solving a problem by having a function calling itself
Why Recursion?
1. Recursive thinking is really important in programming and it helps you break down big
problems into smaller ones and easier to use
‣ Practice
2. The prominent usage of recursion in data structures like trees and graphs.
3. interview
4. It is used in many algorithms (divide and conquer, greedy and dynamic programming)
def recursionMethod(parameters):
if exit from condition satisfied:
return some value
else:
recursionMethod(modified parameters)
n is less than 1
localhost:8888/notebooks/Recursion_3_function_vha.ipynb 2/6
11/30/22, 11:36 AM Recursion_3_function_vha - Jupyter Notebook
localhost:8888/notebooks/Recursion_3_function_vha.ipynb 3/6
11/30/22, 11:36 AM Recursion_3_function_vha - Jupyter Notebook
Out[1]: 144
localhost:8888/notebooks/Recursion_3_function_vha.ipynb 4/6
11/30/22, 11:36 AM Recursion_3_function_vha - Jupyter Notebook
16
In [5]: ## Factorial###
def factorial(n):
assert n >= 0 and int(n) == n, 'The number must be positive integer only!'
if n in [0,1]:
return 1
else:
return n * factorial(n-1)
factorial(10)
Out[5]: 3628800
localhost:8888/notebooks/Recursion_3_function_vha.ipynb 5/6
11/30/22, 11:36 AM Recursion_3_function_vha - Jupyter Notebook
13
localhost:8888/notebooks/Recursion_3_function_vha.ipynb 6/6