SHARE
TWEET

Long code for hamming number

allanbreyes Oct 29th, 2014 433 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def hamming(s1,s2):
  2.     """ returns the hamming number (char differences) between two strings """
  3.     # initialize hamming distance to 0 and find min/max lengths
  4.     hamming_number = 0
  5.     minlength = min(len(s1),len(s2))
  6.     maxlength = max(len(s1),len(s2))
  7.  
  8.     # iterate through strings up to minlength and compare by each char
  9.     for i in range(minlength):
  10.         if (s1[i] != s2[i]):
  11.             hamming_number += 1
  12.    
  13.     # add remaining extra characters
  14.     hamming_number += maxlength - minlength
  15.  
  16.     # return result
  17.     return hamming_number
RAW Paste Data
Top