私は現在exercism.io
を行っていますが、私はハミングの挑戦をしています。私はこの時点で固執しています。ハッシュから一緒に発生する数字。次のようにハッシュキーの値を加算して1つの整数を出力する
課題である:
は、2つのDNA鎖間のハミング差分を算出することができるプログラムを書きます。
何彼らはあなたを始めると、これは(skip
を言う方法については心配しないでください)です。
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
:私は成功し、次のコードでそれらの4をやった
#!/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
私が今必要とするのは、key
のvalue
の数字を1つの整数を作成するために一緒に追加することです。たとえば、次のようにします。
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]]}
だから私は何をする必要があるか単一の整数にまとめて出力私は全体のハッシュを出力せずに2つの値を一緒に出力単一の整数を追加するにはどうすればよいという2
質問を、G
のとT
の値を追加することです自体?
ハミング距離の計算は、思ったよりはるかに簡単です。私は2行のコードでそれをやった。 – Aetherus
各テストケースの答えは、提供された2つの文字列の違いの数です。 2つの文字列の長さが一致しない場合は、エラーになります。各文字のインデックス位置は重要です –