来自 rubykoans, about_scoring_project.rb 里的一个解法 (其中第 8 行 sum += 1000 | value -= 3 if value >= 3 的逻辑稍微有点小错误)
A set of three ones is 1000 points
A set of three numbers (other than ones) is worth 100 times the number. (e.g. three fives is 500 points).
A one (that is not part of a set of three) is worth 100 points.-
A five (that is not part of a set of three) is worth 50 points.
Everything else is worth 0 points.
## 我的问题是
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