-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion_Function.py
More file actions
executable file
·99 lines (83 loc) · 3.44 KB
/
Recursion_Function.py
File metadata and controls
executable file
·99 lines (83 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#----------------------------------------------------------------------------------------#
# Recursion
#----------------------------------------------------------------------------------------#
import sys
''''
# Recursion is used for the
1. Repetative Problems
2. Complex Structures
> When implemanting the recursion always consider either result,
if don't it will spin into infinte loop and crash the system
'''
#Recursion
#The goal in the recursion of function to find the base case condtion if it finds it terminated
#----------------------------------------------------------------------------------------#
sys.setrecursionlimit(2000) # set the default limit to 2000 from 1000
print(sys.getrecursionlimit())
i=0
def greet():
global i
if i>=1998: # Base case to not get the warring for the limit
print("limit is reached for the recursion at ",i)
else:
i+=1
print("hello",i)
greet() # Calling the function itself and max range is 1000 to cppe with recursion
greet()
#2
def new_func(obj):
#some logic Like base case and condtion
return new_func(obj)
#----------------------------------------------------------------------------------------#
# Looping solution by recursion
#----------------------------------------------------------------------------------------#
def find_factorial_looping(n):
if n<0:
return 0
else:
factorial =1
for i in range(1,n+1):
factorial=factorial*i
return factorial
print(find_factorial_looping(5))
#----------------------------------------------------------------------------------------#
# Recursive solution
#----------------------------------------------------------------------------------------#
def find_factorial_recursive(n):
if n==1:
return 1
else:
return n*find_factorial_recursive(n-1) # here retrun is keeping the refference of implemented value and calling again and again
print(find_factorial_recursive(5))
#----------------------------------------------------------------------------------------#
# Advantages and Disadvantages of the Recursion:
#----------------------------------------------------------------------------------------#
'''
# Advantages of the Recursion:
1. Neet code
2. Sub- Problems
3. Easy Sequences
# 2. Disadvantages:
Hard to follow the logic
Memory , some time efficeint and some time high consuming
Debugging : hard to debug
'''
#----------------------------------------------------------------------------------------#
# Recursive function for the tower of hanoi:
#----------------------------------------------------------------------------------------#
disks=int(input('Number of disks to be placed: '))
source=str(input('Name of the source tower '))
helper=str(input('Name of the Helper '))
destination= str(input('Name of the destination tower '))
def hanoi(disks,source,helper, destination):
#Base condition
if (disks ==1):
print('disk {} moves from tower {} to tower {}' .format(disks,source,destination))
return 0
else:
#recursive call when function will call itself
hanoi(disks-1,source,destination,helper)
print("disk {} moves from tower {} to tower {}" .format(disks,source,destination))
hanoi(disks-1, helper, source,destination)
return hanoi
hanoi(disks,source,helper,destination)