RosettaCodeData/Task/Levenshtein-distance/Python/levenshtein-distance-4.py

14 lines
342 B
Python
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
>>> from functools import lru_cache
>>> @lru_cache(maxsize=4095)
def ld(s, t):
2026-04-30 12:34:36 -04:00
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)
2023-07-01 11:58:00 -04:00
>>> print( ld("kitten","sitting"),ld("rosettacode","raisethysword") )
3 8