Skip to content

Commit 5689793

Browse files
added code
1 parent 30e3bd6 commit 5689793

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

16. Recursion and backtracking/19. N Queen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ def n_queen(board,i,n):
2323

2424
thisQueenPosIsValid = n_queen(board,i+1,n)
2525

26+
board[i][j] = 0
27+
2628
if thisQueenPosIsValid:
2729
return True
2830

29-
board[i][j] = 0
31+
#board[i][j] = 0
3032

3133
unset_storage(board,i,j,n)
3234
#print_storage("C, j: "+str(j)+", i: "+str(i))

16. Recursion and backtracking/21. Sudoku Solver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ def sudoku_solver(sudoku,i,j,n):
2020

2121
can_sudoku_solve = sudoku_solver(sudoku,i,j+1,n)
2222

23+
sudoku[i][j] = 0
24+
2325
if can_sudoku_solve:
2426
return True
2527

26-
sudoku[i][j] = 0
28+
#sudoku[i][j] = 0
2729

2830
return False
2931

44. Dynamic Programming/09. Ladders Top Down.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def solve(n,k):
1111
ans = 0
1212
for i in range(1,k+1):
1313
if n-i>=0:
14-
ans += dp[n-i] if dp[n-i] !=0 else solve(n-i,k)
14+
ans += solve(n-i,k)
1515
dp[n] = ans
16-
return dp[n]
16+
return dp[n]
1717

1818
print(solve(4,3))
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
a = "AGGTAB"
2+
b = "GXTXAYB"
3+
op = ""
4+
MAX = 3000
5+
dp = [[-1]*MAX for i in range(MAX)]
6+
7+
def solve_topdown(a,b,i,j):
8+
9+
if j>=len(a) or i>=len(b):
10+
return 0
11+
12+
if dp[i][j] != -1:
13+
return dp[i][j]
14+
15+
ans = -1
16+
if a[j] == b[i]:
17+
ans = 1+solve_topdown(a,b,i+1,j+1)
18+
else:
19+
ans = max(solve_topdown(a,b,i+1,j),solve_topdown(a,b,i,j+1))
20+
21+
dp[i][j] = ans
22+
23+
return ans
24+
25+
26+
27+
28+
def solve(a,b):
29+
global op
30+
31+
la = len(a)
32+
lb = len(b)
33+
34+
dp = [[0]*(la+1) for i in range(lb+1)]
35+
36+
for i in range(1,lb+1):
37+
for j in range(1,la+1):
38+
if a[j-1]==b[i-1]:
39+
op += a[j-1]
40+
dp[i][j] = dp[i-1][j-1] + 1
41+
else:
42+
dp[i][j] = max(dp[i-1][j],dp[i][j-1])
43+
44+
return dp[lb][la],dp
45+
46+
def get_LCS(a,b,l,dp):
47+
48+
i = len(b)
49+
j = len(a)
50+
res = ""
51+
52+
while(l>0):
53+
if a[j-1]==b[i-1]:
54+
res = a[j-1] + res
55+
i-=1
56+
j-=1
57+
l-=1
58+
else:
59+
if (dp[i][j-1]>dp[i-1][j]):
60+
j-=1
61+
else:
62+
i-=1
63+
64+
return res
65+
66+
l,dp = solve(a,b)
67+
#print(l,dp)
68+
print(get_LCS(a,b,l,dp))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
MAX = 8
2+
dp = [[-1]*MAX for i in range(MAX)]
3+
4+
def get_LCS_len(a,b,i,j):
5+
global dp
6+
7+
if j>=len(a) or i>=len(b):
8+
return 0
9+
10+
if dp[i][j] != -1:
11+
return dp[i][j]
12+
13+
if a[j]==b[i]:
14+
dp[i][j] = get_LCS_len(a,b,i+1,j+1) + 1
15+
else:
16+
dp[i][j] = max(get_LCS_len(a,b,i+1,j),get_LCS_len(a,b,i,j+1))
17+
18+
return dp[i][j]
19+
20+
def get_LCS(a,b,l):
21+
global dp
22+
i = j = 0
23+
res = ""
24+
25+
while(l>0):
26+
if a[j]==b[i]:
27+
res += a[j]
28+
i+=1
29+
j+=1
30+
l-=1
31+
else:
32+
if (dp[i][j+1]>dp[i+1][j]):
33+
j+=1
34+
else:
35+
i+=1
36+
37+
return res
38+
39+
40+
a = "AGGTAB"
41+
b = "GXTXAYB"
42+
43+
l = get_LCS_len(a,b,0,0)
44+
print(get_LCS(a,b,l))
45+
print(dp)
46+

0 commit comments

Comments
 (0)