hamming.rb

1
2
3
4
5
6
7
8
9
class Hamming
    VERSION = 1
    def self.compute(first_strand, second_strand)
        raise ArgumentError.new("Same length strands only") unless (first_strand.length == second_strand.length)
        first_strand.bytes.each_with_index.inject(0) do |sum, (byte, i)|
            sum += second_strand.bytes[i] == byte ? 0 : 1
        end
    end
end

Comments

Whenever you are looping through a collection and find yourself writing a conditional (if or unless) nested inside of the loop, take a moment to look through the available enumerable methods.

There are some very handy ones that might let you simplify.


This is an automated nitpick. Read more about this experiment.

rikki- commented 23 April 2016 at 13:42 UTC

You're not logged in right now. Please login via GitHub to comment