-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathlevenshtein_distance.py
More file actions
42 lines (31 loc) · 957 Bytes
/
levenshtein_distance.py
File metadata and controls
42 lines (31 loc) · 957 Bytes
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
'''
@author: aaditkamat
@date: 31/12/2018
'''
def ind (m, n):
return 0 if m == n else 1
def dist(first, second):
first_length = len(first)
second_length = len(second)
m = []
for i in range(first_length + 1):
row = []
for j in range(second_length + 1):
row.append(0)
m.append(row)
for i in range(1, first_length + 1):
m[i][0] = i;
for i in range(second_length + 1):
m[0][i] = i;
for i in range(1, first_length + 1):
for j in range(1, second_length + 1):
values = [m[i - 1][j] + 1, m[i][j - 1] + 1, m[i - 1][j - 1] + ind(first[i - 1], second[j - 1])]
values.sort()
m[i][j] = values[0]
return m[first_length][second_length]
def main():
print('Enter two strings: ')
first = input()
second = input()
print(f'The Levenshtein distance between \"{first}\" and \"{second}\" is: {dist(first, second)}')
main()