Thanks but I did not solve my problem.
···
#
# A greed roll is scored as follows:
#
# * 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.
#
#
# 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
#
# More scoring examples are given in the tests below:
#
# Your goal is to write the score method.
def score(dice)
total = 0
h = Hash.new(0)
dice.each {|el| h[el] += 1}
dice.each {|number, count|
if number == 1 and count == 6 then return 2000 end
if number != 1 and count == 3 then total = total + 100 * count end
if number != 1 and count == 6 then return 600 end
if number == 1 and (count > 3) then total = total + ( 100 + ( count - 3)) end
if number == 1 and (count < 3) then total = total + 100 * count end
if number == 5 and (count > 3) then total = total + ( 50 * (count -3)) end
if number == 5 and (count < 3) then total = total + 50 * count end
}
end
class AboutScoringProject < EdgeCase::Koan
def test_score_of_an_empty_list_is_zero
assert_equal 0, score()
end
def test_score_of_a_single_roll_of_5_is_50
assert_equal 50, score([5])
end
def test_score_of_a_single_roll_of_1_is_100
assert_equal 100, score([1])
end
def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores
assert_equal 300, score([1,5,5,1])
end
def test_score_of_single_2s_3s_4s_and_6s_are_zero
assert_equal 0, score([2,3,4,6])
end
def test_score_of_a_triple_1_is_1000
assert_equal 1000, score([1,1,1])
end
def test_score_of_other_triples_is_100x
assert_equal 200, score([2,2,2])
assert_equal 300, score([3,3,3])
assert_equal 400, score([4,4,4])
assert_equal 500, score([5,5,5])
assert_equal 600, score([6,6,6])
end
def test_score_of_mixed_is_sum
assert_equal 250, score([2,5,2,2,3])
assert_equal 550, score([5,5,5,5])
end
end
And I now get this error: <0> expected but was <>.
When I make that one a comment :
def test_score_of_an_empty_list_is_zero
#assert_equal 0, score()
end
Then I see this error appear :
The answers you seek...
undefined method `>' for nil:NilClass
Please meditate on the following code:
./about_scoring_project.rb:43:in `score'
./about_scoring_project.rb:37:in `each'
./about_scoring_project.rb:37:in `score'
./about_scoring_project.rb:56:in `test_score_of_a_single_roll_of_5_is_50'
/home/roelof/koans/edgecase.rb:398:in `send'
/home/roelof/koans/edgecase.rb:398:in `meditate'
/home/roelof/koans/edgecase.rb:470:in `walk'
/home/roelof/koans/edgecase.rb:481:in `each_step'
/home/roelof/koans/edgecase.rb:479:in `each'
/home/roelof/koans/edgecase.rb:479:in `each_step'
path_to_enlightenment.rb:38:in `each_with_index'
/home/roelof/koans/edgecase.rb:478:in `each'
/home/roelof/koans/edgecase.rb:478:in `each_with_index'
/home/roelof/koans/edgecase.rb:478:in `each_step'
/home/roelof/koans/edgecase.rb:476:in `catch'
/home/roelof/koans/edgecase.rb:476:in `each_step'
/home/roelof/koans/edgecase.rb:469:in `walk'
/home/roelof/koans/edgecase.rb:491
path_to_enlightenment.rb:38
So something is not well here ?
Roelof
Date: Wed, 26 Sep 2012 03:26:55 +0900
From: lists@ruby-forum.com
Subject: Re: inject problem
To: ruby-talk@ruby-lang.org
Roelof Wobben wrote in post #1077503:
> h = Hash.new(0)
> dice.each {|el| dice[el] += 1}
> [...]
> But now I'm getting this error : undefined method `+' for nil:NilClass
You want h[el] += 1, not dice[el] += 1. See the original code by Jesús.
--
Posted via http://www.ruby-forum.com/\.