Skip to content

Instantly share code, notes, and snippets.

@Mellen
Last active May 2, 2018 06:30
Show Gist options
  • Save Mellen/d3bc3f366753561afa8c2f9d17094266 to your computer and use it in GitHub Desktop.
Save Mellen/d3bc3f366753561afa8c2f9d17094266 to your computer and use it in GitHub Desktop.
A way of calculating the distance between 2 strings
def string_distance(s1, s2):
allchars = ''.join(sorted(set(s1+s2)))
s1vals = [allchars.index(c) for c in s1]
s2vals = [allchars.index(c) for c in s2]
diffs = [abs(x-y) for x,y in zip(s1vals,s2vals)]
lengthdiff = abs(len(s1)-len(s2))
return sum(diffs)+lengthdiff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment