Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14-liner: someone told this is Levenshtein's idea not mine solution in Clear category for String Conversion by przemyslaw.daniel
from functools import lru_cache
@lru_cache(maxsize=1000)
def steps_to_convert(a, b):
if len(a)*len(b) == 0:
return max(len(a), len(b))
p = steps_to_convert(a[1:], b[1:])
if a[0] != b[0]:
q = steps_to_convert(a[1:], b)
r = steps_to_convert(a, b[1:])
return 1+min(p, q, r)
return p
if __name__ == '__main__':
assert steps_to_convert('nline1', 'line2p') == 3
assert steps_to_convert('line1', '1enil') == 4
assert steps_to_convert('1426542165482634872637362', '12587263483268715672') == 14
assert steps_to_convert('3142341324132431432413243322323',
'1331232312312312312312321321321311542514524152414') == 30
Nov. 22, 2016
Comments: