-
Notifications
You must be signed in to change notification settings - Fork 1
/
BA5G.py
47 lines (38 loc) · 2.6 KB
/
BA5G.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
"""
https://www.youtube.com/watch?v=We3YDTzNXEk&ab_channel=TusharRoy-CodingMadeSimple
"""
def edit_distance(X, Y):
n = len(X)
m = len(Y)
dp = []
for i in range(m+1):
small = []
for j in range(n+1):
small.append(100000000)
dp.append(small)
dp[0][0] = 0
n += 1
m += 1
for i in range(1, n):
dp[0][i] = i
for i in range(1, m):
dp[i][0] = i
X = "U" + X
Y = "U" + Y
for i in range(1, m):
for j in range(1, n):
if Y[i] == X[j]:
dp[i][j] = dp[i-1][j-1]
else:
mn = min(dp[i][j-1], dp[i-1][j])
dp[i][j] = min(dp[i-1][j-1], mn) + 1
return dp[m-1][n-1]
if __name__ == "__main__":
X = "TGGAGCCATGGAAATGAGCCCTAAAGACTTCGGTCGCGTGAGGCAGCCGCTGCGTTATTCTTAGTACGCTTACCTAAGGCGTAATCTATACACACTAGCGTCCGCGCATTGGCGCCAGGATTGTGCCTGACGTAACTATACTGCTGGTACACACCGAGCGACCGTCTCACCCAAAAACATTCGATCGATGGCACGTGATACGCTTGTAAAGTAGAGTGGCGTAGGAGTTTGCCAGTAAATAAAGAGAGTAGTCGCAATCCCATCAATAGGTATTCCGGCTAACATAGTAAGAGGCCATGCCCTGTCTTGATCAAATAACGCATATGTAACAGAGAGACTAAGCAGTAGAGGTGGTCTCCGCTGGTCCGGATTGGTTACGCGACATTAGACCGCAGGTCTAGGCGCAGTTATCGACGACGAAATTGAATGAGCGACCTTTATGTAGCTGGCGAGCTAGGCGACTGTAGCTGGAGTGCGTAGGTGGTTATGGTCCCGGTGGCCTTGGTACACACATAAACTTTTTAAGCCTACCACCAAAACATTAACACAGCGAGCGAAGGGATTATAGAGTTATACCCTATTACGTGTCCGGATTTTCTGGTTGGTCTAATGAACTAGTGATATGGATGAACCTGCGAACTCGCGACTCCCCTTGCGTCGGCTAATATAGGATGGCGACCCTTCAGGAGGTATTGTATATAGGTAGAAAACGTTAAGAAATTTAAACTCCAAATGATGTTGGGAGCAGAACGTCGCCTAGGTGTTTGTTGACATTCAAAAGAGGCGATGCTTGACTTCCAGCCACCCATCTGCCTGTATCCGCTCATTATCCCTCAACGATTTCAACGCGGACTTTTTAACAGGATACTGAATTTGACGTGCATCAGTTCCAGGCAAGTT"
Y = "CGGAGCCAAATGAGCCCTAAAGAACGAGCCGCTGCGTTATTCTCACTGACACTCTAGGGTACGTCAGGGGTAATCTACACTAGCGTCCGCGCATTGCCGCCAGGAAACCATACTGTCCTAACTGGCACTCACACTGCGCAGCAGTTCGTTTGCCGTCTCCACCCAGGAGAAAACATTCGATCGTTGGCACGTAATACTCGCTTGTAAAGTAGATTGGCGTAGGGGTTTGCCAATAAATGATAAAACATGAGTACTGCCATAGTCCGGCTAACATGGTAAGGTGGGCCGAAGGCCTGTCTTGATATTGCACAAATAACTCAAACAATGTAACAGAGACACCATCCGGACCTAGAGGTGTTGGCTCGCTTCCGCGTCCGGACTGGTTACGCTTAGACCGCCGGCGTTAGCGGCGCACTTATCGATCGGATACCGACGAAAATGAAAACGAGCTACCTTTATGGAGCTGGCGGGAGTGCGCGCTAGTCCACAATAGTAGGCAAGGGAGCGCGTAGGGGTCGCGGTCGTGCTCTGCATTTTGGTACATGCGAGGCGCATAAACTAAGACGCGCATTAATCCTACCACCAAAACATTAACACAACGAGCGAAATACCGGATGTATGCCCTATTACAATTCTGATTTGATATCTGGTTGGTCTAATTATAGTGAAATGGCCCAAGGGCCTTCGCGCCCTTGCCTCGGTTATAGGATGAGCTTCTTTCGCCAGACCCTGCCTCAGGCGGCCGGCGTATTAGGTAGAAAACGTTAAACTTTGCTCTTTCAAATGATGTTGGGTGCAGGCGTCGCCTATGTTGATTATTCTATGTGGTCACGTTTTACCACTTGCTTGACTTCCAGCCACCCAATATCCTGCCTGTTTATCAACGCGGGCTTTTCGGTTACTGACGTGGCGTCAGTGCGCCAAGGCAAGTT"
with open('rosalind_ba5g.txt') as file:
f = file.read().split()
X = f[0]
Y = f[1]
d = edit_distance(X, Y)
print(d)