-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathRecursion.py
201 lines (165 loc) · 3.86 KB
/
Recursion.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#Power Of A Number
"""
Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer.
Note : For this question, you can assume that 0 raised to the power of 0 is 1
Input format :
Two integers x and n (separated by space)
Output Format :
x^n (i.e. x raise to the power n)
"""
Sample Input 1 :
3 4
Sample Output 1 :
81
Sample Input 2 :
2 5
Sample Output 2 :
32
Solution :
def Power_Number(X,N):
output=X**N
return output
str=input().split()
x,n=int(str[0]),int(str[1])
r=Power_Number(x,n)
print(r)
#Sum Of Array
"""
Given an array of length N, you need to find and return the sum of all elements of the array.
Do this recursively.
Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Output Format :
Sum
"""
Sample Input 1 :
3
9 8 9
Sample Output 1 :
26
Sample Input 2 :
3
4 2 1
Sample Output 2 :
7
Solution :
n=int(input())
li=[int(x) for x in input().split()]
sum = 0
for list_ele in li:
sum=sum+list_ele
print(sum)
#Check Number in Array
"""
Given an array of length N and an integer x, you need to find if x is present in the array or not. Return true or false.
Do this recursively.
Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x
Output Format :
'true' or 'false'
"""
Sample Input 1 :
3
9 8 10
8
Sample Output 1 :
true
Sample Input 2 :
3
9 8 10
2
Sample Output 2 :
false
Solution :
def NumberinArray(li):
if x in li:
print("true")
else:
print("false")
from sys import setrecursionlimit
setrecursionlimit(11000)
n = int(input())
li = [int(x) for x in input().split()]
x = int(input())
NumberinArray(li)
#First Index of Number - Question
"""
Given an array of length N and an integer x, you need to find and return the first index of integer x present in the array. Return -1 if it is not present in the array.
First index means, the index of first occurrence of x in the input array.
Do this recursively. Indexing in the array starts from 0.
Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x
Output Format :
first index or -1
"""
Sample Input :
4
9 8 10 8
8
Sample Output :
1
Solution :
def FirstIndex(a,x):
l=len(a)
if l==0:
return -1
if a[0]==x:
return 0
smallerList=a[1:]
smallerListOutput=FirstIndex(smallerList,x)
if smallerListOutput==-1:
return -1
else:
return smallerListOutput+1
from sys import setrecursionlimit
setrecursionlimit(11000)
n=int(input())
a=list(int(i) for i in input().strip().split(' '))
x=int(input())
r=FirstIndex(a,x)
print(r)
#Last Index Of Number Question
"""
Given an array of length N and an integer x, you need to find and return the last index of integer x present in the array. Return -1 if it is not present in the array.
Last index means - if x is present multiple times in the array, return the index at which x comes last in the array.
You should start traversing your array from 0, not from (N - 1).
Do this recursively. Indexing in the array starts from 0.
Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x
Output Format :
last index or -1
"""
Sample Input :
4
9 8 10 8
8
Sample Output :
3
Solution :
def LastIndex(a,x):
l=len(a)
if l==0:
return -1
smallerList=a[1:]
smallerListOutput=LastIndex(smallerList,x)
if smallerListOutput!= -1:
return smallerListOutput+1
else:
if a[0]==x:
return 0
else:
return -1
from sys import setrecursionlimit
setrecursionlimit(11000)
n=int(input())
a=list(int(i) for i in input().strip().split(' '))
x=int(input())
r=LastIndex(a,x)
print(r)