From 942ff3bfd74671bbe7d73a78e473fb5168ea22b4 Mon Sep 17 00:00:00 2001 From: Jessica Date: Sat, 7 May 2016 09:51:01 -0700 Subject: [PATCH] completed hamming recursive --- hamming.rb | 13 +++++++++++++ hamming_test.rb | 14 -------------- 2 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 hamming.rb diff --git a/hamming.rb b/hamming.rb new file mode 100644 index 0000000..67eceba --- /dev/null +++ b/hamming.rb @@ -0,0 +1,13 @@ +class Hamming + + def self.compute(distance = 0, strand1, strand2) + raise ArgumentError if strand1.length != strand2.length + + return distance if strand1.empty? && strand2.empty? + + distance += 1 if strand1[0] != strand2[0] + + return self.compute(distance, strand1[1..-1], strand2[1..-1]) + end + +end \ No newline at end of file diff --git a/hamming_test.rb b/hamming_test.rb index 2d1f9d7..0ddf0a1 100644 --- a/hamming_test.rb +++ b/hamming_test.rb @@ -4,72 +4,58 @@ class HammingTest < Minitest::Test def test_identical_strands - skip assert_equal 0, Hamming.compute('A', 'A') end def test_long_identical_strands - skip assert_equal 0, Hamming.compute('GGACTGA', 'GGACTGA') end def test_complete_distance_in_single_nucleotide_strands - skip assert_equal 1, Hamming.compute('A', 'G') end def test_complete_distance_in_small_strands - skip assert_equal 2, Hamming.compute('AG', 'CT') end def test_small_distance_in_small_strands - skip assert_equal 1, Hamming.compute('AT', 'CT') end def test_small_distance - skip assert_equal 1, Hamming.compute('GGACG', 'GGTCG') end def test_small_distance_in_long_strands - skip assert_equal 2, Hamming.compute('ACCAGGG', 'ACTATGG') end def test_non_unique_character_in_first_strand - skip assert_equal 1, Hamming.compute('AGA', 'AGG') end def test_non_unique_character_in_second_strand - skip assert_equal 1, Hamming.compute('AGG', 'AGA') end def test_large_distance - skip assert_equal 4, Hamming.compute('GATACA', 'GCATAA') end def test_large_distance_in_off_by_one_strand - skip assert_equal 9, Hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT') end def test_empty_strands - skip assert_equal 0, Hamming.compute('', '') end def test_disallow_first_strand_longer - skip assert_raises(ArgumentError) { Hamming.compute('AATG', 'AAA') } end def test_disallow_second_strand_longer - skip assert_raises(ArgumentError) { Hamming.compute('ATA', 'AGTG') } end end