From 9a8aab92611e7c7cb33ea60dfda57132e5b43830 Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Sun, 8 May 2016 18:55:10 -0700 Subject: [PATCH] finished recursive method --- hamming_recursive.rb | 24 ++++++++++++++++++++++++ hamming_test.rb | 16 +--------------- 2 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 hamming_recursive.rb diff --git a/hamming_recursive.rb b/hamming_recursive.rb new file mode 100644 index 0000000..cb2a3ba --- /dev/null +++ b/hamming_recursive.rb @@ -0,0 +1,24 @@ +class Hamming + + def self.compute(strand1, strand2, hamming_distance = 0) + + strand1_array = strand1.split(//) + strand2_array = strand2.split(//) + + if strand1_array.length != strand2_array.length + raise ArgumentError, "Strands must be the same length." + end + + return hamming_distance if strand1_array.empty? && strand2_array.empty? + + hamming_distance += 1 if strand1_array[0] != strand2_array[0] + + strand1_array.shift + strand2_array.shift + + return self.compute(strand1_array.join, strand2_array.join, hamming_distance) + + + end + +end diff --git a/hamming_test.rb b/hamming_test.rb index 2d1f9d7..c0faa6f 100644 --- a/hamming_test.rb +++ b/hamming_test.rb @@ -1,75 +1,61 @@ gem 'minitest', '>= 5.0.0' require 'minitest/autorun' -require_relative 'hamming' +require_relative 'hamming_recursive' 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