ruby 鲁比·科恩斯:About_Scoring_Project(准确地说,是带有.map方法的迭代器)

wwwo4jvm  于 2023-10-17  发布在  Ruby
关注(0)|答案(3)|浏览(129)

我是新来的我在做Ruby Koans的时候遇到了一些问题,About_Scoring_Project。.map方法根据短语的顺序返回值,而不是变量的名称。我很困惑。

  1. def score(dice)
  2. total_score = 0
  3. side = 6
  4. count = Array.new
  5. side.times{count << 0} # [0, 0, 0, 0, 0, 0]
  6. # Tell the occured time of each side
  7. while i = dice.pop
  8. count[i-1] += 1
  9. end
  10. # Calculating total score
  11. i = 0
  12. count.map! do |item|
  13. i += 1
  14. if item >= 3 && i != 1
  15. total_score = i*100
  16. item - 3
  17. elsif item >= 3
  18. total_score = 1000
  19. item - 3
  20. else
  21. item
  22. end
  23. end
  24. # Count the rest
  25. total_score += ( count[0]*100 + count[4]*50 )
  26. total_score # return the total score
  27. end

虽然这一个作品,但当我最初写道:

  1. elsif item >= 3
  2. item - 3
  3. total_score = 1000

执行时数组计数结果为[1000,0,0,0,0]

  1. score([1,1,1]) # score([1,1,1]) = 101000 which should be 1000

也就是说,它将值1000赋予item而不是total_value。但是当我像上面指出的那样改变两个短语的顺序时,它就能正确工作。有人能帮我一下吗。我是Ruby和编程的新手。原谅我蹩脚的英语…
项目背景:

  1. # Greed is a dice game where you roll up to five dice to accumulate
  2. # points. The following "score" function will be used to calculate the
  3. # score of a single roll of the dice.
  4. #
  5. # A greed roll is scored as follows:
  6. #
  7. # * A set of three ones is 1000 points
  8. #
  9. # * A set of three numbers (other than ones) is worth 100 times the
  10. # number. (e.g. three fives is 500 points).
  11. #
  12. # * A one (that is not part of a set of three) is worth 100 points.
  13. #
  14. # * A five (that is not part of a set of three) is worth 50 points.
  15. #
  16. # * Everything else is worth 0 points.
  17. #
  18. #
  19. # Examples:
  20. #
  21. # score([1,1,1,5,1]) => 1150 points
  22. # score([2,3,4,6,2]) => 0 points
  23. # score([3,4,5,3,3]) => 350 points
  24. # score([1,5,1,2,4]) => 250 points
  25. #
  26. # More scoring examples are given in the tests below:
  27. #
  28. # Your goal is to write the score method.
irtuqstp

irtuqstp1#

.map方法根据短语的顺序返回值,而不是变量的名称。我很困惑。
或多或少是的。块只是返回最后一个表达式的值,很像方法。
如果您有:

  1. count.map! do |item|
  2. item - 3
  3. total_score = 1000
  4. end

那么total_score = 1000,它的计算结果是1000,是块的返回值。
如果您有:

  1. count.map! do |item|
  2. total_score = 1000
  3. item - 3
  4. end

那么item - 3是块的返回值。
map!又创建了一个新数组,其中包含块中的值。

展开查看全部
bmp9r5qi

bmp9r5qi2#

还有一种方法:

  1. def three_of_a_kind(dice, count)
  2. count * (dice == 1 ? 1000 : 100 * dice)
  3. end
  4. def no_set(dice, count)
  5. case dice
  6. when 1
  7. count * 100
  8. when 5
  9. count * 50
  10. else
  11. 0
  12. end
  13. end
  14. def score(rolls)
  15. rolls.group_by(&:itself).map do |dice, throws|
  16. n = throws.size
  17. three_of_a_kind(dice, n / 3) + no_set(dice, n % 3)
  18. end.inject(:+)
  19. end
  20. p score([1, 1, 1, 5, 1]) == 1150
  21. p score([2, 3, 4, 6, 2]) == 0
  22. p score([3, 4, 5, 3, 3]) == 350
  23. p score([1, 5, 1, 2, 4]) == 250

顺便说一下:

  1. side = 6
  2. count = Array.new
  3. side.times{count << 0}

就是:

  1. Array.new(6){ 0 }
展开查看全部
lmvvr0a8

lmvvr0a83#

  1. def score(dice)
  2. sum = 0
  3. dice.tally.each do |x, n|
  4. sum += n / 3 * (x == 1 ? 1000 : x * 100) + n % 3 * (x == 1 ? 100 : x == 5 ? 50 : 0)
  5. end
  6. sum
  7. end

相关问题