#NEWLINE# def hamming( str1 , str2 ) : #NEWLINE# #INDENT# 'Algorithm based on an old project of mine, ported from yavascript:\n    christabor.github.io/etude/09-02-2014/.\n\n    A superior algorithm exists at\n    wikipedia.org/wiki/Hamming_distance#Algorithm_example,\n    but copying it would defeat the purpose.' #NEWLINE# dist = 0 #NEWLINE# last_longer = ( len ( str1 ) < len ( str2 ) ) #NEWLINE# ( first , last ) = ( ( str1 , str2 ) if last_longer else ( str2 , str1 ) ) #NEWLINE# for ( k , letter ) in enumerate ( str1 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if ( str1 [ k ] != str2 [ k ] ) : #NEWLINE# #INDENT# dist += 1 #NEWLINE# #UNINDENT# #UNINDENT# except IndexError : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# for k in range ( len ( first ) , len ( last ) ) : #NEWLINE# #INDENT# dist += 1 #NEWLINE# #UNINDENT# print ( 'Hamming dist for `{}` and `{}` = {}' . format ( str1 , str2 , dist ) ) #NEWLINE# return last_longer