Skip to content

Instantly share code, notes, and snippets.

@bmwant
Created January 25, 2016 16:54
Show Gist options
  • Save bmwant/a7a22c620667813cc447 to your computer and use it in GitHub Desktop.
Save bmwant/a7a22c620667813cc447 to your computer and use it in GitHub Desktop.
Find distance between strings based on non-equal characters count
from collections import Counter
def find_letter_distance(string, other):
string_counter = Counter(string)
other_counter = Counter(other)
unique_symbols = set(string + other)
distance = 0
for char in unique_symbols:
string_occurences = string_counter[char]
other_occurences = other_counter[char]
distance += abs(string_occurences-other_occurences)
return distance
def test_find_letter_distance():
data_string1 = 'one'
data_other1 = 'two'
expected1 = 4
data_string2 = 'equal'
data_other2 = 'equal'
expected2 = 0
data_string3 = 'the length of this string'
data_other3 = ''
expected3 = len(data_string3)
assert find_letter_distance(data_string1, data_other1) == expected1
assert find_letter_distance(data_string2, data_other2) == expected2
assert find_letter_distance(data_string3, data_other3) == expected3
if __name__ == '__main__':
find_letter_distance('one', 'two')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment