Here is the question:
Given two words with the same number of letters in each, work out how many letters need to be changed to get from the first word to the second. A more complex version of edit distance is commonly used in spelling auto-correct algorithms on phones and word processors to find candidate corrections.
The two words should be read from the user with one word per line. For example:
Word 1: hello
Word 2: jelly
2
this is all I got:
w1 = input('Word 1: ')
w2 = input('Word 2: ')
for i in w1:
for o in w2:
print(i, o)
How do I do this?
4 Answers 4
You can try something like:
sum(c1 != c2 for c1,c2 in zip(w1,w2))
zip(w1,w2)
creates a generator that returns tuples consisting of corresponding letters of w1
and w2
. i.e.:
>>> list(zip(w1,w2))
[('h', 'j'), ('e', 'e'), ('l', 'l'), ('l', 'l'), ('o', 'y')]
We iterate over these tuples (c1
gets assigned to each first char and c2
to each second char) and check if c1 != c2
. We add up all the instances for which this condition is satisfied to arrive at out answer.
>>> w1 = 'hello'
>>> w2 = 'jelly'
>>>
>>> sum(c1 != c2 for c1,c2 in zip(w1,w2))
2
Using difflib:
>>> import difflib
>>> w1, w2 = 'hello', 'jelly'
>>> matcher = difflib.SequenceMatcher(None, w1, w2)
>>> m = sum(size for start, end, size in matcher.get_matching_blocks())
>>> n = max(map(len, (w1, w2))) # n = len(w1)
>>> n - m
2
1 Comment
difflib
is generally a good place to look when solving these types of problems. But for something this simple, it seems like overkill.A functional approach:
>>> from itertools import starmap
>>> from operator import ne
>>> sum(starmap(ne, zip(word1, word2)))
2
3 Comments
zip
approach:: sum(map(str.__ne__, a, b))
zip()
stops after the shortest word, while map()
goes on until the longest word is exhausted, using None
as a fill value, in case the words have different lengths.str.__ne__
will end up with a TypeError
which is also in effect an assertion that the lengths are equal as stated in the problem text.If the words are always going to be the same length you can use zip to loop through both lists at once:
w1 = input('Word 1: ')
w2 = input('Word 2: ')
changes=0
for i, o in zip(w1, w2):
if i != o:
changes+=1
print "Changes needed: "+str(changes)