Reputation: 99
I need to find the edit distance between a word and its sorted word (ex: apple and aelpp), using only insertions and deletions recursively.
I have found some sources that used insertions, deletions, and substitutions, but I am not sure how to only use insertion and deletion.
This is the code I found:
def ld(s, t):
if not s: return len(t)
if not t: return len(s)
if s[0] == t[0]: return ld(s[1:], t[1:])
l1 = ld(s, t[1:])
l2 = ld(s[1:], t)
l3 = ld(s[1:], t[1:])
return 1 + min(l1, l2, l3)
What edits would need to be made to only find the number of insertions and deletions?
Upvotes: 1
Views: 534
Reputation: 1905
Remove l3
, which computes substitutions like so
def ld2(s, t):
if not s: return len(t)
if not t: return len(s)
if s[0] == t[0]: return ld2(s[1:], t[1:])
l1 = ld2(s, t[1:])
l2 = ld2(s[1:], t)
return 1 + min(l1, l2)
You can see that ld('apple', 'applx')
is equal to 1, while ld2
with the same parameters evaluates to 2.
Upvotes: 1