Reputation: 1674
I'm currently doing exercism.io
and I'm on the Hamming challenge, I'm kind of stuck at this point because what I need to do is add the numbers that occur from the hash together.
The challenge is as follows:
Write a program that can calculate the Hamming difference between two DNA strands.
What they start you off with is this (don't worry about the methods that say skip
):
#!/usr/bin/env ruby
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'hamming'
# Test data version:
# ab84334 Merge pull request #106 from bennn/grep-meta
class HammingTest < Minitest::Test
def test_identical_strands
assert_equal 0, Hamming.compute('A', 'A')
end
def test_long_identical_strands
assert_equal 0, Hamming.compute('GGACTGA', 'GGACTGA')
end
def test_complete_distance_in_single_nucleotide_strands
assert_equal 1, Hamming.compute('A', 'G')
end
def test_complete_distance_in_small_strands
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
# Problems in exercism evolve over time,
# as we find better ways to ask questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of Hamming.
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 1, Hamming::VERSION
end
end
I've successfully done four of them with the following code:
class Hamming
def self.compute(x, y)
if x == y
0
else
strings = x, y
joined = strings.join
positions = (0...joined.length).group_by{|i| joined[i]}
length = strings.first.length
n = strings.length
diff = Hash[*positions.map{|k, v|
[k, v.group_by{|i| i % length}.reject{|i, is| is.length == n}.keys]}]
diff
end
end
end
What I need to do now is add the digits that are the value
of the key
together to create a single integer, for example for this:
10) Failure:
HammingTest#test_complete_distance_in_small_strands [hamming_test.rb:23]:
Expected: 2
Actual: nil
#<= {["A", [0]]=>["G", [1]], ["C", [0]]=>["T", [1]]}
So what I need to do is add the value of G
and of T
together and output a single integer 2
Question being, how do I add two values together and output a single integer without outputting the entire hash itself?
Upvotes: 1
Views: 110
Reputation: 18803
Starting with your hash,
h = {["A", [0]]=>["G", [1]], ["C", [0]]=>["T", [1]]}
First use Hash#values
to get out the values.
> h.values
=> [["G", [1]], ["T", [1]]]
These are arrays, and you want the last entry of each, which Array#last
provides.
> h.values.map(&:last)
=> [[1], [1]]
Your numbers are wrapped in another array, so you could use last
again, but Array#flatten
is semantically nice.
> h.values.map(&:last).flatten
=> [1, 1]
Then a simple way to sum them is with Enumerable#inject
.
> h.values.map(&:last).flatten.inject(:+)
=> 2
It's not clear to me whether you want the numbers out of your hash keys as well, but if you do, you can get them with Hash#keys, or since you want everything, Hash#to_a
.
> h.keys + h.values
=> [["A", [0]], ["C", [0]], ["G", [1]], ["T", [1]]]
> h.to_a
=> [[["A", [0]], ["G", [1]]], [["C", [0]], ["T", [1]]]]
Upvotes: 2