Skip to content

Commit d7e7596

Browse files
committed
commit
1 parent 4861981 commit d7e7596

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Recursion_ProdofArray.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Given an array of integers, Replace each element with the product of the rmaining elements.
2+
# input: {1,2,3,4,5}
3+
#output: {120,60,40,30,24}
4+
#Brute force approach
5+
6+
def prod_array(arr):
7+
result = []
8+
for i in range(len(arr)):
9+
prod = 1
10+
for j in range(len(arr)):
11+
if i != j:
12+
prod = prod * arr[j]
13+
result += [prod]
14+
print (result)
15+
return result
16+
17+
arr = [1,2,3,4,5]
18+
prod_array(arr)
19+
20+
21+
def rec_Prodarr(arr, prodleft, index):
22+
#Termination Condition
23+
if index >= len(arr):
24+
return 1
25+
26+
currentValue = arr[index]
27+
productTillcurrentIndex = currentValue * prodleft
28+
29+
productOfElementRightOfCurrentIndex = rec_Prodarr(arr, productTillcurrentIndex, index+1)
30+
31+
arr[index] = prodleft * productOfElementRightOfCurrentIndex
32+
33+
return currentValue * productOfElementRightOfCurrentIndex
34+
print (productOfElementRightOfCurrentIndex)
35+
36+
37+
arr = [1,2,3,4,5]
38+
rec_Prodarr(arr, 1, 0)

longestCommonPrefix.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def longestCommonPrefix(self,strs: List[strs]) -> strs:
2+
# end condition if we do not have char
3+
if not strs:
4+
return ''
5+
6+
# find min and max length word from string List
7+
m, M = min(strs), max(strs)
8+
9+
#enumerate min word this will give index and letter(0:m,1:i,2:l,3:a)
10+
for i, letters in enumerate(m):
11+
if letter != M[i] # Compare char with Max word char and if that is not equal then
12+
return m[:i] # return min strs word starting with 0 index to i index,
13+
return m
14+
15+
16+
17+

0 commit comments

Comments
 (0)