Skip to content

Instantly share code, notes, and snippets.

// how I'd "fix" https://akrzemi1.wordpress.com/2017/01/02/not-detecting-bugs/
//
// * Explicitly state "code assumes input vectors are same length"
// * Explicitly request inlining of loop's inner funcion
// * real world code will probably not use "v1, v2" naming
// * instead of relying "compiler will optimize away helper constructs",
// just don't use any.
//
// in general, it's a similar case, when instead of ...
// struct x = {a[++i], b[i]}
def hamming(s1,s2):
""" returns the hamming number (char differences) between two strings """
hamming_number = 0
for char1,char2 in itertools.izip_longest(s1,s2):
if char1!=char2:
hamming_number += 1
return hamming_number