2

私は Ruby Koans に取り組んでいますが、私が書いたメソッドの何が問題なのかを理解するのに少し苦労しています。私は about_scoring_project.rb にいて、サイコロ ゲームのスコア メソッドを書きました。

def score(dice)
  return 0 if dice == []
  sum = 0
  rolls = dice.inject(Hash.new(0)) { |result, element| result[element] += 1; result; }
  rolls.each { |key, value| 
    # special condition for rolls of 1
    if key == 1  
      sum += 1000 | value -= 3 if value >= 3
      sum += 100*value
      next
    end
    sum += 100*key | value -= 3 if value >= 3
    sum += 50*value if key == 5 && value > 0
  }
  return sum
end

運動に慣れていない人のために:

グリードはサイコロを5つ振ってポイントを貯めるサイコロゲームです。次の「score」関数は、サイコロの 1 回のロールのスコアを計算するために使用されます。

グリード ロールは次のように採点されます。

  • 3個セットで1000ポイント

  • 3 つの数字 (1 を除く) のセットは、その数字の 100 倍の価値があります。(例: スリーファイブは 500 ポイントです)。

  • 1 つ (3 つのセットの一部ではない) は 100 ポイントの価値があります。

  • 5 (3 つのセットの一部ではない) は 50 ポイントの価値があります。

  • それ以外はすべて0点です。

例:

スコア([1,1,1,5,1]) => 1150ポイント スコア([2,3,4,6,2]) => 0ポイント スコア([3,4,5,3,3]) => 350 ポイント score([1,5,1,2,4]) => 250 ポイント

以下のテストでは、その他のスコアリングの例が示されています。

あなたの目標は、score メソッドを書くことです。

ファイル内の最後のテストを実行しようとすると、問題が発生します。assert_equal 550, score([5,5,5,5])

何らかの理由で、550 ではなく 551 を返します。ご協力ありがとうございます。

4

14 に答える 14

4

これが私のアプローチです:

def score(dice)
  # Count how many what
  clusters = dice.reduce(Hash.new(0)) {|hash, num| hash[num] += 1; hash }

  # Since 1's are special, handle them first
  ones = clusters.delete(1) || 0
  score = ones % 3 * 100 + ones / 3 * 1000

  # Then singular 5's
  score += clusters[5] % 3 * 50

  # Then the triples other than triple-one
  clusters.reduce(score) {|s, (num, count)| s + count / 3 * num * 100 }
end
于 2015-08-19T11:18:55.607 に答える
2

これは、実際には|演算子 (ビットごとの OR) の結果を合計スコアに追加しているためです。

sum += 100*key | value -= 3 if value >= 3 # This is 501 in your case

証拠:

irb(main):004:0> value = 4
=> 4
irb(main):005:0> 100 * 5 | value -= 3 # This should be read as (500) | 1 which is 501
=> 501

そこで、次のように書き換えます。

if value >= 3
  sum += 100 * key
  value -= 3
end
于 2012-07-22T20:21:47.583 に答える
2

一緒に行きました

def score(dice)
  dice.uniq.map do |die|
    count = dice.count die
    if count > 2
      count -= 3
      die == 1 ? 1000 : 100 * die
    else 0
    end + case die
          when 1 then count * 100
          when 5 then count * 50
          else 0
          end
  end.inject(:+) || 0
end
于 2015-12-23T19:16:08.643 に答える
1

私のアプローチは次のとおりです。

def score(dice)
  calculator = ->(no, group_multipler, individual_multipler) { (no / 3 * group_multipler) + (no % 3 * individual_multipler) }
  dice.group_by {|i| i % 7 }.inject(0) do |total, (value, scores)|
    group_multipler, individual_multipler = case value
    when 1
      [1000, 100]
    when 5
      [500, 50]
    else 
      [value * 100, 0]
    end
    total += calculator.call(scores.size, group_multipler, individual_multipler)
  end
end
于 2014-01-30T14:39:14.253 に答える
0

これが私の解決策です。

def score(dice)
    score = 0
    # grab all the numbers and their amounts
    number_amounts = dice.reduce(Hash.new(0)) { |hash, numb| hash[numb] += 1; hash }

    # iterate through each pair
    number_amounts.each do |key, value|
      # case with number 1
      score += (value % 3) * 100 + value / 3 * 1000 if (key == 1)
      # case with number 5
      score += (value % 3) * 50 + value / 3 * key * 100 if (key == 5)
      # all numbers except 1 and 5
      score += (value / 3) * key * 100 if (key != 1 && key != 5)
    end
    score
end
于 2016-05-11T18:27:22.810 に答える
-2
def score(dice)
    ones = fives = rest = 0

    one_count = dice.count(1)
    if one_count > 2
        ones = 1000
        one_count -= 3
    end
    ones += one_count * 100

    five_count = dice.count(5)
    if five_count > 2
        fives = 500
        five_count -= 3
    end
    fives += five_count * 50

    [2,3,4,6].each do |num|
        if dice.count(num) > 2
            rest += num * 100
        end
    end

    return ones + fives + rest
end
于 2017-09-25T11:52:40.940 に答える