我目前正在做exercism.io
而我正在进行汉明挑战,我现在已经陷入困境了,因为我需要做的是添加从哈希在一起。
挑战如下:
编写一个程序,可以计算两条DNA链之间的汉明差异。
他们开始为您服务的是(不要担心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
我已使用以下代码成功完成了其中四项:
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
我现在需要做的是将value
的{{1}}数字加在一起创建一个整数,例如:
key
所以我需要做的是将 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]]}
和G
的值加在一起并输出一个整数T
问题是,如何将两个值一起添加并输出一个整数而不输出整个哈希本身?
答案 0 :(得分:2)
从你的哈希开始,
h = {["A", [0]]=>["G", [1]], ["C", [0]]=>["T", [1]]}
首先使用Hash#values
来获取值。
> h.values
=> [["G", [1]], ["T", [1]]]
这些是数组,您需要每个Array#last
提供的最后一个条目。
> h.values.map(&:last)
=> [[1], [1]]
您的号码包含在另一个数组中,因此您可以再次使用last
,但Array#flatten
在语义上很不错。
> h.values.map(&:last).flatten
=> [1, 1]
然后用一个简单的方法来总结它们是Enumerable#inject
。
> h.values.map(&:last).flatten.inject(:+)
=> 2
我不清楚你是否也想要哈希键中的数字,但是如果你这样做,你可以使用Hash#keys,或者因为你想要一切,Hash#to_a
。< / p>
> h.keys + h.values
=> [["A", [0]], ["C", [0]], ["G", [1]], ["T", [1]]]
> h.to_a
=> [[["A", [0]], ["G", [1]]], [["C", [0]], ["T", [1]]]]