SCORECARD = {
"1" => 40,
"2" => 100,
"3" => 300,
"4" => 1200
}
def get_score(arr)
score_base = []
#level = getPoints(arr) / 10
score_base = calculateScore(arr)
end
def getPoints(points)
points.inject(0) do |sum, point|
sum + point
end
end
def calculateScore(lines)
score_base = []
lines.each do |line|
SCORECARD.each do |key, value|
if line == key
score_base << value
end
score_base
end
end
end
describe "Basic tests" do
Test.assert_equals(get_score([0, 1, 2, 3, 4]), 1640)
Test.assert_equals(get_score([0, 1, 1, 3, 0, 2, 1, 2]), 620)
Test.assert_equals(get_score([2, 0, 4, 2, 2, 3, 0, 0, 3, 3]), 3300)
end
describe "Special tests" do
Test.assert_equals(get_score([0]), 0)
Test.assert_equals(get_score([]), 0)
end
Test Results:
Basic tests
Expected: 1640, instead got: [0, 1, 2, 3, 4]
Expected: 620, instead got: [0, 1, 1, 3, 0, 2, 1, 2]
Expected: 3300, instead got: [2, 0, 4, 2, 2, 3, 0, 0, 3, 3]
STDERR
/runner/frameworks/ruby/cw-2.rb:60:in `block in describe': Expected: 1640, instead got: [0,
1, 2, 3, 4] (Test::Error)
from /runner/frameworks/ruby/cw-2.rb:46:in `measure'
from /runner/frameworks/ruby/cw-2.rb:51:in `describe'
from /runner/frameworks/ruby/cw-2.rb:202:in `describe'
from main.rb:46:in `<main>'
問題:calculateScore
メソッドのデバッグについて支援が必要です。基本的に、lines配列とSCORECARD
ハッシュを繰り返し処理し、現在の行がハッシュキーと等しいかどうかを確認し、最後に一致したキーの値をscore_base
配列にプッシュします。
すべての値をscore_base
配列にプッシュしたら、配列内の各要素にscore_base
ユーザーの現在のレベルを掛けてから、すべての要素を合計してtotalScore
。
出力は、lines配列内の要素のみを返すようです。誰かが何が起こっているのかを明らかにすることができますか?
編集:問題に対する私の解決策
```
SCORECARD = {
"1" => 40,
"2" => 100,
"3" => 300,
"4" => 1200
}
def get_score(arr)
totalScore = multiplyByLevel(arr)
end
def calculateScore(line)
score_base = []
score_base.push SCORECARD.fetch(line.to_s, 0)
score_base.reduce(:+)
end
def multiplyByLevel(points)
experience = 1.0
level = 0
totalScore = 0
points.each do |point|
experience = experience.round(1) + (point.to_f / 10)
if level < 2
totalScore = totalScore + (1 * calculateScore(point))
elsif level >= 2
totalScore = totalScore + (level.round(1) * calculateScore(point))
else
print "Outside loop, level is #{level}"
end
level = experience.to_i
end
totalScore
end
```
編集:BOBRODESフィードバックへの応答:リファクタリング計算スコア法
```
def calculateScore(lines)
score_base = []
lines.map {|line| score_base << SCORECARD[line.to_s]}
score_base.compact
end
#=> [40,100,300,1200]
```
あなたの基本的な問題は、#each
常にレシーバーを返すことです(レシーバーは左側にあるものです.each
)。あなたのコードを見てみましょう:
def calculateScore(lines)
score_base = []
lines.each do |line|
SCORECARD.each do |key, value|
if line == key
score_base << value
end
score_base
end # << returns SCORECARD
end # << returns lines
end
だから、あなたはscore_base
ここに置く必要があります:
def calculateScore(lines)
score_base = []
lines.each do |line|
SCORECARD.each do |key, value|
if line == key
score_base << value
end
end # << returns SCORECARD
end # << returns lines
score_base
end
これはsteenslagの答えの最初の部分の説明ですが、彼の残りのアドバイスも見ていきます。
コードに別の問題があります。のキーSCORECARD
は文字列ですが、のテスト入力の値lines
は整数です。したがって、質問した問題を修正すると、結果に対して常に空の配列が取得されます。
しかし、コードにはさらに大きな問題があります。それは、#map
1行の単純なコードで必要なものを取得するために使用できることです。を使用している間は確かにコードをかなり凝縮することができますが#each
、#map
値を反復処理しながら変換するように設計されており、値を反復処理するように#each
設計されています。値を(SCORECARD
キーからSCORECARD
値に)変換したいので、これ#map
が最も簡単な方法です。
私はすべての楽しみを奪ってあなたのためにそれをしたくないので、ここに簡単な例があり#map
ます:
[1, 2, 3, 4].map { |num| num + 1 } #=> [2, 3, 4, 5]
テスト入力でそのアイデアを取り入れて[0, 1, 2, 3, 4]
、を返すことができるかどうかを確認してください[nil, 40, 100, 300, 1200]
。次に、それらすべてを合計する方法を考え出し(nil
役立つと思われる値を削除する方法があります)、ワンライナーを作成します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加