Skip to content

Instantly share code, notes, and snippets.

@orsinium
Created May 4, 2017 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orsinium/8e69fb4684054bf1ec7ac9986f9da7fc to your computer and use it in GitHub Desktop.
Save orsinium/8e69fb4684054bf1ec7ac9986f9da7fc to your computer and use it in GitHub Desktop.
Расстояние Дамерау между двумя текстами.
def f(s1, s2):
if not s1 or not s2:
return len(s1)+len(s2)
elif s1[-1]==s2[-1]:
return f(s1[:-1], s2[:-1])
else:
#вставка/удаление
a = min(f(s1[:-1], s2), f(s1, s2[:-1]))
#замена
b = f(s1[:-1], s2[:-1])
return min(a, b)+1
if __name__ == '__main__':
while 1:
print(f(input('1: '), input('2: ')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment