From a2375f430689b99b873875676ba9d99b59890c68 Mon Sep 17 00:00:00 2001 From: Jillian Boshart Date: Mon, 9 May 2016 21:04:14 -0700 Subject: [PATCH] Hamming solution. --- hamming.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 hamming.rb diff --git a/hamming.rb b/hamming.rb new file mode 100644 index 0000000..e15e271 --- /dev/null +++ b/hamming.rb @@ -0,0 +1,18 @@ +class Hamming + + def self.compute(x, y, distance=0) + raise (ArgumentError) if x.length != y.length + return distance if x.length == 0 + + if x[0] == y[0] + x = x[1..-1] + y = y[1..-1] + Hamming.compute(x, y, distance) + else + distance = distance + 1 + x = x[1..-1] + y = y[1..-1] + Hamming.compute(x, y, distance) + end + end +end