Last active
May 2, 2018 06:30
-
-
Save Mellen/d3bc3f366753561afa8c2f9d17094266 to your computer and use it in GitHub Desktop.
A way of calculating the distance between 2 strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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