[英]Ruby Koans Scoring Project
I'm working through the Ruby Koans, and I'm having a bit of trouble figuring out what is going wrong with a method I've written.我正在研究 Ruby Koans,我在弄清楚我编写的方法出了什么问题时遇到了一些麻烦。 I'm in about_scoring_project.rb, and I've written the score method for the dice game:我在 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
For those unfamiliar with the exercise:对于那些不熟悉该练习的人:
Greed is a dice game where you roll up to five dice to accumulate points. Greed 是一种骰子游戏,您最多可以掷五个骰子来累积点数。 The following "score" function will be used to calculate the score of a single roll of the dice.以下“分数”函数将用于计算单次掷骰子的分数。
A greed roll is scored as follows:贪婪卷的得分如下:
A set of three ones is 1000 points一套三个是1000分
A set of three numbers (other than ones) is worth 100 times the number.一组三个数字(除了一个)的价值是该数字的 100 倍。 (eg three fives is 500 points). (例如三个五是 500 分)。
A one (that is not part of a set of three) is worth 100 points.一个(不是一组三个的一部分)值 100 分。
A five (that is not part of a set of three) is worth 50 points.一个五(不是一组三个的一部分)值 50 分。
Everything else is worth 0 points.其他一切都值得0分。
Examples:例子:
score([1,1,1,5,1]) => 1150 points score([2,3,4,6,2]) => 0 points score([3,4,5,3,3]) => 350 points score([1,5,1,2,4]) => 250 points score([1,1,1,5,1]) => 1150 分 score([2,3,4,6,2]) => 0 分 score([3,4,5,3,3]) => 350 分 score([1,5,1,2,4]) => 250 分
More scoring examples are given in the tests below:下面的测试中给出了更多评分示例:
Your goal is to write the score method.您的目标是编写 score 方法。
I run into trouble when I try to run the last test in the file: assert_equal 550, score([5,5,5,5])
当我尝试运行文件中的最后一个测试时遇到了麻烦: assert_equal 550, score([5,5,5,5])
For some reason I am returning 551 instead of 550. Thanks for your help!出于某种原因,我返回 551 而不是 550。感谢您的帮助!
Here is my approach:这是我的方法:
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
My approach uses two lookup tables - one containing the scores for triples, the other for singles.我的方法使用两个查找表 - 一个包含三元组的分数,另一个包含单打的分数。 I work out the score for each number using the tables, and accumulate the total using inject
:我使用表格计算出每个数字的分数,并使用inject
累积总数:
def score(dice)
triple_scores = [1000, 200, 300, 400, 500, 600]
single_scores = [100, 0, 0, 0, 50, 0]
(1..6).inject(0) do |score, number|
count = dice.count(number)
score += triple_scores[number - 1] * (count / 3)
score += single_scores[number - 1] * (count % 3)
end
end
I went with我去了
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
This is because you're really adding the result of a |
这是因为您实际上是在添加|
的结果operator (Bitwise OR) to the total score:运算符(按位或)与总分:
sum += 100*key | value -= 3 if value >= 3 # This is 501 in your case
Proof:证明:
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
So rewrite it like this:所以像这样重写它:
if value >= 3
sum += 100 * key
value -= 3
end
My approach was:我的方法是:
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
My approach:我的方法:
def score(dice)
score = 0
score += dice.count(1) >= 3? (1000+ (dice.count(1) -3)*100): dice.count(1) * 100
score += dice.count(5) >= 3 ? (500 + (dice.count(5) -3)*50): dice.count(5) * 50
[2,3,4,6].each {|x| dice.count(x) >=3? score+= x*100:0}
return score
end
Here's my answer:这是我的答案:
def score(dice)
frequency = dice.inject(Hash.new(0)) do |h, el|
h[el] += 1
h
end
score_triples = { 1 => 1000 }
score_singles = { 1 => 100, 5 => 50 }
score = 0
frequency.each do |k, v|
score += v / 3 * score_triples.fetch(k, 100 * k)
score += v % 3 * score_singles.fetch(k, 0)
end
score
end
My approach used integer division and modulus division:我的方法使用整数除法和模数除法:
def score(dice)
points = 1000 * (dice.count(1) / 3)
points += 100 * (dice.count(1) % 3)
points += 50 * (dice.count(5) % 3)
(2..6).each do |i|
points += (100 * i) * (dice.count(i) / 3)
end
points
end
This was the first piece of code I ever wrote by myself (With a ton of help of stackoverflow, of course.) After watching all other answers I realize it is way overkill specially because it works for a 9 numbers dice (does that exist?)这是我自己编写的第一段代码(当然,在 stackoverflow 的大量帮助下。)看完所有其他答案后,我意识到这太过分了,特别是因为它适用于 9 个数字的骰子(是否存在? )
def score(dice)
if dice.empty?
return 0
end
var_score = 0
conteo = (0..9).to_a.each.map { |x| dice.count(x)}
#Evaluating 1
if ( conteo[1] / 3 ) >= 0
multiplier1 = conteo[1]/3
var_score += multiplier1 * 1000
end
if ( conteo[1] % 3 ) != 0
var_score += (conteo[1] % 3)*100
end
#Evaluating 5
if ( conteo[5] % 3 ) != 0
var_score += (conteo[5] % 3)* 50
end
#Evaluating numbers x 3
if (conteo[2..9].count { |x| x >= 3 }) > 0
triplets = conteo[2..9].map {|x| x / 3}
array_multiplicator = triplets.each_with_index.select {|num,index| (num > 0)}.map {|x| x[0]}
product_triplets = triplets.each_with_index.select {|num,index| (num > 0)}.map {|x| x[1]}.map {|x| (x+2)*100}
var_score += array_multiplicator.zip(product_triplets).map{|x| x.inject(&:*)}.sum
end
var_score
end
It took 29 lines, but this is my first Ruby花了 29 行,但这是我的第一个 Ruby
def score(dice)
return 0 if dice == []
sums = Array.new # To hold number of occurrences 1 - 6
for i in 0..6 # Initialize to 0... note [0] is not used
sums[i] = 0
end
total = 0 # To hold total
dice.each do |dots| # Number of dots showing on dice
sums[dots] += 1 # Increment the array members 1 - 6
end
if sums[1] > 2 then # If 3 1's
total += 1000
sums[1] -= 3 # Remove the 3 you took, in case there's more
end
if sums[2] > 2 then total += 200 # If 3 2's
end
if sums[3] > 2 then total += 300 #If 3 3's
end
if sums[4] > 2 then total += 400 #If 3 4's
end
if sums[5] > 2 then total += 500 #If 3 5's
sums[5] -= 3 #Remove the 5's you took
end
if sums[6] > 2 then total += 600 #If 3 6's
end
total += (sums[1] * 100) # If any ones are left
total += (sums[5] * 50) # Same for fives
return total
end
This is my solutions.这是我的解决方案。
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
def score(dice)
# Set up rules Hash
rules = { 1 => {:triples => 1000, :singles => 100}, 5 => {:triples => 100, :singles => 50} }
[2,3,4,6].each {|i| rules[i] = {:triples => 100, :singles => 0} }
# Count all ocourencies
counts = dice.each_with_object(Hash.new(0)) {|e, h| h[e] += 1}
#calculate total
total = 0
counts.each_pair{ | key, value |
total += value >= 3? (rules[key][:triples]*key + (value -3)*rules[key][:singles]): value * rules[key][:singles]
}
return total
end
I used the new ruby enumerable method tally
我使用了新的 ruby 可枚举方法tally
def score(dice)
return 0 if dice.empty?
ans = dice.tally.map do |k,v|
case k
when 1
three = (k * 1000) * (v/3)
val = (v%3) * 100
val + three
when 5
three = (k * 100) * (v/3)
val = (v%3) * 50
val + three
else
(k * 100) * (v/3)
end
end
ans.reduce(0, :+)
end
My attempt, feedback and refactoring suggestions most welcome:我的尝试、反馈和重构建议最受欢迎:
def score(dice)
score = 0
score_array = [[0, 100, 200, 1000, 1100, 1200], [0, 0, 0, 200, 200, 200], [0, 0, 0, 300, 300, 300], [0, 0, 0, 400, 400, 400], [0, 50, 100, 500, 550, 600], [0, 0, 0, 600, 600, 600]]
tally_hash = {1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0}
dice.sort.tally.each do |key, value|
tally_hash[key] += value
end
tally_hash.each do |key, value|
score += score_array[key -1][value]
end
return score
end
def score(dice)
score = 0
dice.uniq.each do |number|
count = dice.count number
weight = if number == 1 then 10 else number end
if count >= 3
score += weight * 100
count -= 3
end
if count > 0 and number == 1 or number == 5
score += count * weight * 10
end
end
score
end
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.