Created
November 2, 2021 23:42
-
-
Save eevmanu/b56e026f6a84f1f57cc0c5c01f6cf3e2 to your computer and use it in GitHub Desktop.
understand algorithm which implement variant of levenshtein distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/q/41275345/ | |
def dist(s1, s2): | |
cur = list(range(len(s2) + 1)) | |
prev = [0] * (len(s2) + 1) | |
print(f"{cur=}") | |
print(f"{prev=}") | |
input() | |
for i in range(len(s1)): | |
cur, prev = prev, cur | |
cur[0] = i + 1 | |
print(f"--{i=} {cur[0]=}") | |
print(f"--{cur=}") | |
print(f"--{prev=}") | |
input() | |
for j in range(len(s2)): | |
# deletion 1 operation | |
# substitution 2 operation (deletion and insertion) | |
sub = 0 if s1[i] == s2[j] else 2 | |
cur[j+1] = min(prev[j] + sub, cur[j] + 1, prev[j+1] + 1) | |
print(f"----{s1=} {s2=}") | |
print(f"----{s1[:i+1]=} {s2[:j+1]=}") | |
print(f"----{j=} {s1[i]=} {s2[j]=}") | |
print(f"----{prev[j]=} {sub=} {cur[j]=} {prev[j+1]=}") | |
print(f"----{(prev[j] + sub)=} {(cur[j] + 1)=} {(prev[j+1] + 1)=}") | |
print(f"----{cur[j+1]=}") | |
print(f"----{cur=}") | |
print(f"----{prev=}") | |
input() | |
return cur[-1] | |
cases=[ | |
# ('cat','bat'), | |
# ('bat','cat'), | |
('broom', 'ballroom'), | |
# ('boat','got'), | |
# ('foo', 'bar'), | |
# ('foobar', '') | |
] | |
for s1, s2 in cases: | |
print(f"{s1=} {s2=}") | |
print(f"{dist(s1, s2)=}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment