Ruby Greed Koan – 如何改善我的汤匙?

前端之家收集整理的这篇文章主要介绍了Ruby Greed Koan – 如何改善我的汤匙?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在通过Ruby Koans工作,以便尝试学习Ruby,到目前为止,这么好.我已经到了这个写作时的贪婪的koan.我有一个工作的解决方案,但我觉得我已经拼凑在一起只是一堆if / then逻辑,而我不是拥抱Ruby图案.

在下面的代码中,有没有办法让我更充分地拥抱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 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,5,1]) => 1150 points
  22. # score([2,3,4,6,2]) => 0 points
  23. # score([3,3]) => 350 points
  24. # score([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.
  29.  
  30. # MY CODE BEGINS HERE
  31.  
  32. def score(dice)
  33.  
  34. # set up basic vars to handle total points and count of each number
  35. total = 0
  36. count = [0,0]
  37.  
  38. # for each die,make sure we've counted how many occurrencess there are
  39. dice.each do |die|
  40. count[ die - 1 ] += 1
  41. end
  42.  
  43. # iterate over each,and handle points for singles and triples
  44. count.each_with_index do |count,index|
  45. if count == 3
  46. total = doTriples( index + 1,total )
  47. elsif count < 3
  48. total = doSingles( index + 1,count,total )
  49. elsif count > 3
  50. total = doTriples( index + 1,total )
  51. total = doSingles( index + 1,count % 3,total )
  52. end
  53. end
  54.  
  55. # return the new point total
  56. total
  57.  
  58. end
  59.  
  60. def doTriples( number,total )
  61. if number == 1
  62. total += 1000
  63. else
  64. total += ( number ) * 100
  65. end
  66. total
  67. end
  68.  
  69. def doSingles( number,total )
  70. if number == 1
  71. total += ( 100 * count )
  72. elsif number == 5
  73. total += ( 50 * count )
  74. end
  75. total
  76. end
  77.  
  78. # MY CODE ENDS HERE
  79.  
  80. class AboutScoringProject < EdgeCase::Koan
  81. def test_score_of_an_empty_list_is_zero
  82. assert_equal 0,score([])
  83. end
  84.  
  85. def test_score_of_a_single_roll_of_5_is_50
  86. assert_equal 50,score([5])
  87. end
  88.  
  89. def test_score_of_a_single_roll_of_1_is_100
  90. assert_equal 100,score([1])
  91. end
  92.  
  93. def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores
  94. assert_equal 300,score([1,1])
  95. end
  96.  
  97. def test_score_of_single_2s_3s_4s_and_6s_are_zero
  98. assert_equal 0,score([2,6])
  99. end
  100.  
  101. def test_score_of_a_triple_1_is_1000
  102. assert_equal 1000,1])
  103. end
  104.  
  105. def test_score_of_other_triples_is_100x
  106. assert_equal 200,2])
  107. assert_equal 300,score([3,3])
  108. assert_equal 400,score([4,4])
  109. assert_equal 500,score([5,5])
  110. assert_equal 600,score([6,6])
  111. end
  112.  
  113. def test_score_of_mixed_is_sum
  114. assert_equal 250,3])
  115. assert_equal 550,5])
  116. end
  117.  
  118. end

非常感谢任何可以给我的帮助,因为我试图让我的头围绕Ruby.

解决方法

好的我可能写了一些有些不同的东西,比如说:
  1. def do_triples number,total
  2. total + (number == 1 ? 1000 : number * 100)
  3. end

如果你想做一些Ruby以外的几种语言可以做的事情,那么我认为在DIE和DRY之间的交替周二可能有以下的可能性,但是我不认为这些Ruby maxims是否真的适用于普通的子表达式消除.无论如何:

  1. def do_triples number,total
  2. total +
  3. if number == 1
  4. 1000
  5. else
  6. number * 100
  7. end
  8. end
  9.  
  10. def do_triples number,total
  11. if number == 1
  12. 1000
  13. else
  14. number * 100
  15. end + total
  16. end

猜你在找的Ruby相关文章