Module: Measurable::Hamming

Included in:
Measurable
Defined in:
lib/measurable/hamming.rb

Instance Method Summary collapse

Instance Method Details

#hamming(s1, s2) ⇒ Object

call-seq:

hamming(s1, s2) -> Integer

Count the number of different characters between strings s1 and s2, that is, how many substitutions are necessary to change s1 into s2 and vice-versa.

See: en.wikipedia.org/wiki/Hamming_distance

Arguments:

  • s1 -> A String.

  • s2 -> A String with the same size of s1.

Returns:

  • The number of characters in which s1 and s2 differ.

Raises:

  • ArgumentError -> The sizes of s1 and s2 don’t match.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/measurable/hamming.rb', line 20

def hamming(s1, s2)
  # TODO: Change this to a more specific, custom-made exception.
  raise ArgumentError if s1.size != s2.size

  s1.chars.zip(s2.chars).reduce(0) do |acc, c|
    acc += 1 if c[0] != c[1]
    acc
  end
end