Skip to content

Instantly share code, notes, and snippets.

@wwalker
Created May 3, 2016 00:36
Show Gist options
  • Save wwalker/19adbfb5dfad48af9cfb6e1066e8b92c to your computer and use it in GitHub Desktop.
Save wwalker/19adbfb5dfad48af9cfb6e1066e8b92c to your computer and use it in GitHub Desktop.
class Hamming
def self.compute(a, b)
[a, b].min.size.times.count {
|i| a[i] != b[i]
}
end
end
class Hamming
def self.compute(a, b)
# select the shorter strand
c = [a, b].min
length = c.size
# length.times returns an iterator that will return 0, 1, 2, ...length -1
# count takes each element of an array and runs the block,
# keeping count of the nunmber of times the block returns true
# so this returns the number of strand matches.
length.times.count { |i|
a[i] != b[i]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment