This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.py
57 lines (54 loc) · 1.64 KB
/
edit.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
48
49
50
51
52
53
54
55
56
57
from stringcolor import *
def Display(row):
for i in row:
print(i, end=" ")
def editDistance(source, target):
for i in source:
print(i)
slen = len(source)
tlen = len(target)
D = []
for i in range(slen + 1):
tmp = []
print("first loop")
for j in range(tlen + 1):
tmp.append(0)
D.append(tmp)
print(cs(D,"red"))
for i in range(1, slen + 1):
print("second loop")
D[i][0] = D[i-1][0] + 1
print(cs(D,"red"))
for j in range(1, tlen + 1):
print("third loop")
D[0][j] = D[0][j-1] + 1
print(cs(D,"red"))
for i in range(1, slen + 1):
print("forth loop")
for j in range(1, tlen + 1):
if source[i-1] == target[j-1]:
D[i][j] = D[i-1][j-1]
else:
D[i][j] = min(D[i-1][j] + 1,
D[i][j-1] + 1,
D[i-1][j-1] + 2)
print(cs(D,"red"))
Display(D)
def recursiveEditDistance(source, target, slen, tlen):
if slen == 0:
return tlen
if tlen == 0:
return slen
for i in source:
if source[slen - 1] == target[tlen - 1]:
return recursiveEditDistance(source, target, slen - 1, tlen - 1)
return min(recursiveEditDistance(source, target, slen, tlen - 1) + 1,
recursiveEditDistance(source, target, slen - 1, tlen) + 1,
recursiveEditDistance(source, target, slen - 1, tlen - 1) + 2)
city=['lahore','execution']
count=dict()
for i in city:
x=recursiveEditDistance(i, "lahore", len(i), len("execution"))
count[i]=x
print(count)
print(min(count, key=count.get))