Skip to content

Commit a8d7e2c

Browse files
committed
new
1 parent e1e717b commit a8d7e2c

File tree

10 files changed

+205
-13
lines changed

10 files changed

+205
-13
lines changed

Dynamic Programming/0-KnapSack-Weight,Value.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#A naive recursive implementation of 0-1 Knapsack Problem
2-
32
# Returns the maximum value that can be put in a knapsack of capacity W
43
def knapSack(W , wt , val , n): #Time Complexity is O(2^n)
54

@@ -17,6 +16,7 @@ def knapSack(W , wt , val , n): #Time Complexity is O(2^n)
1716
# max(---- eq for including nth wt-------------------,---- for not including------)
1817

1918

19+
#Using Dynamic programming
2020
def dynamicProg_KP(W, wt, val, n): #Time Complexity is O(nW)
2121
#creating a table using list comprehensions
2222
K = [[0 for x in range(W+1)] for x in range(n+1)] #here "+1" cuz we go from 0 to W,0 to n
@@ -34,6 +34,8 @@ def dynamicProg_KP(W, wt, val, n): #Time Complexity is O(nW)
3434
return K[n][W]
3535

3636

37+
38+
3739
# To test above functions
3840
val = [60, 100, 120]
3941
wt = [10, 20, 30]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#Using recursion - Time Complexity => O(2^n)
3+
def lcs(X, Y, m, n):
4+
5+
if m == 0 or n == 0:
6+
return 0;
7+
elif X[m-1] == Y[n-1]:
8+
return 1 + lcs(X, Y, m-1, n-1);
9+
else:
10+
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
11+
12+
13+
#Using tabulation method of dynamic programming - Time Complexity => O(mn)
14+
def lcs(X , Y):
15+
m = len(X)
16+
n = len(Y)
17+
18+
L = [[None for _ in range(n+1)] for _ in range(m+1)]
19+
20+
"""Following steps build L[m+1][n+1] in bottom up fashion
21+
Note: L[i][j] contains length of LCS of X[0..i-1]
22+
and Y[0..j-1]"""
23+
for i in range(m+1):
24+
for j in range(n+1):
25+
if i == 0 or j == 0 :
26+
L[i][j] = 0
27+
elif X[i-1] == Y[j-1]:
28+
L[i][j] = L[i-1][j-1]+1
29+
else:
30+
L[i][j] = max(L[i-1][j] , L[i][j-1])
31+
32+
return L[m][n]
33+
34+
35+
36+
X = "AGGTAB"
37+
Y = "GXTXAYB"
38+
print "Length of LCS is ", lcs(X , Y, len(X), len(Y))

Dynamic Programming/wordbreak.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dictionary = ["i","am","awesome"]
2+
3+
s = "iamaweso"
4+
5+
l = len(s)
6+
7+
K = [[0 for _in range(l)] for _ in range(l)]
8+
9+
for i

Lists/listFlatten.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Implement Flatten Arrays.
3+
Given an array that may contain nested arrays,
4+
give a single resultant array.
5+
6+
function flatten(input){
7+
}
8+
9+
Example:
10+
11+
Input: var input = [2, 1, [3, [4, 5], 6], 7, [8]];
12+
flatten(input);
13+
Output: [2, 1, 3, 4, 5, 6, 7, 8]
14+
"""
15+
16+
17+
def flatten_rec(input):
18+
19+
flatList = []
20+
21+
def makelist(lis):
22+
for el in lis:
23+
if type(el) == list:
24+
makelist(el)
25+
else:
26+
flatList.append(el)
27+
28+
makelist(input)
29+
30+
return flatList
31+
32+
33+
34+
input = [2, 1, [3, [4, 5], 6], 7, [8]]
35+
print(flatten_rec(input))

Lists/max 3 num prod.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
import sys
22

33
def solution(lis):
4-
54
first = second = third = -sys.maxsize
65

76
for num in lis:
87

98
if num > first:
10-
119
second = first
12-
1310
third = second
14-
1511
first = num
1612

1713
elif num > second :
18-
1914
third = second
20-
2115
second = num
2216

23-
2417
elif num > third :
25-
2618
third = num
2719

2820
print(first, second, third)
29-
30-
3121
return first * second * third

Lists/product of ints.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ def index_prod(lst):
1515

1616
while i < len(lst):
1717

18-
1918
# Set index as cumulative product
2019
output[i] = product
2120

2221
# Cumulative product
2322
product *= lst[i]
2423

25-
# Move forward
2624
i +=1
2725

2826
print("output", output)

Lists/remove duplicates.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
'''remove duplicates from a given list'''
3+
4+
def remove_duplicates(lst):
5+
seen = set()
6+
res = []
7+
for x in lst:
8+
if x not in seen:
9+
res.append(x)
10+
seen.add(x)
11+
return res

Misc/Matrix Multiplication.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
4+
def matrixmult (A, B):
5+
rows_A = len(A)
6+
cols_A = len(A[0])
7+
rows_B = len(B)
8+
cols_B = len(B[0])
9+
10+
if cols_A != rows_B:
11+
print "Cannot multiply the two matrices. Incorrect dimensions."
12+
return
13+
14+
# Create the result matrix
15+
# Dimensions would be rows_A x cols_B
16+
C = [[0 for row in range(cols_B)] for col in range(rows_A)]
17+
#print C
18+
19+
for i in range(rows_A):
20+
for j in range(cols_B):
21+
for k in range(cols_A):
22+
C[i][j] += A[i][k] * B[k][j]
23+
return C

Misc/Simple Order Book.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
''' This question from AKuna Capital'''
2+
3+
4+
# a simple orderbook class
5+
import operator
6+
class OrderBook:
7+
def __init__(self):
8+
self.order_book = {}
9+
10+
def buy(self, order_id, price):
11+
self.order_book[order_id] = price
12+
13+
def sell(self, order_id):
14+
if order_id in self.order_book:
15+
del self.order_book[order_id]
16+
17+
def get_high_price(self):
18+
if not self.order_book:
19+
return -1
20+
sorted_prices = sorted(self.order_book.items(),key=operator.itemgetter(1))
21+
ret_val = "%g" % float(sorted_prices[-1][1]) #This line is to drop trailing ".0" from floats
22+
return ret_val
23+
24+
25+
book = OrderBook()
26+
for line in sys.stdin:
27+
elements = line.split(' ')
28+
time_order = int(elements[0])
29+
type_order = elements[1]
30+
id_order = int(elements[2])
31+
price_order = None
32+
if len(elements) == 4:
33+
price_order = float(elements[3])
34+
35+
if type_order == 'B':
36+
book.buy(id_order,price_order)
37+
38+
elif type_order == 'S':
39+
book.sell(id_order)
40+
41+
print(time_order, book.get_high_price())
42+
43+
44+
45+
46+
#Sample Input
47+
# 0 B 1 10.0
48+
# 10 B 2 20.5
49+
# 30 B 3 15.0
50+
# 60 S 2
51+
# 100 S 1
52+
# 200 S 3
53+
# 250 B 4 10.5
54+
# 300 S 4

Trees/print all nth level nodes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
'''
3+
4+
Print nodes at k distance from root
5+
Given a root of a tree, and an integer k. Print all the nodes which are at k distance from root.
6+
7+
For example, in the below tree, 4, 5 & 8 are at distance 2 from root.
8+
1
9+
/ \
10+
2 3
11+
/ \ /
12+
4 5 8
13+
14+
15+
'''
16+
17+
18+
def printNodeFunc(root,n,store = []):
19+
20+
if not root:
21+
return
22+
23+
if n==0:
24+
print(root.data)
25+
26+
else:
27+
printNodeFunc(root.left,n-1)
28+
printNodeFunc(root.right,n-1)
29+
30+
31+
32+
#Ans : 4,5,8

0 commit comments

Comments
 (0)