From: Roelof Wobben Date: 2012-09-26T03:51:34+09:00 Subject: Re: inject problem --_0f1251f3-5eb1-4ebc-ae6c-123942915b2d_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Thanks but I did not solve my problem. Here is the whole script : require File.expand_path(File.dirname(__FILE__) + '/edgecase') # Greed is a dice game where you roll up to five dice to accumulate # points. 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 # # * 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=2C1=2C1=2C5=2C1]) =3D> 1150 points # score([2=2C3=2C4=2C6=2C2]) =3D> 0 points # score([3=2C4=2C5=2C3=2C3]) =3D> 350 points # score([1=2C5=2C1=2C2=2C4]) =3D> 250 points # # More scoring examples are given in the tests below: # # Your goal is to write the score method. def score(dice) total =3D 0=20 h =3D Hash.new(0) dice.each {|el| h[el] +=3D 1}=20 dice.each {|number=2C count| if number =3D=3D 1 and count =3D=3D 6 then return 2000 end if number !=3D 1 and count =3D=3D 3 then total =3D total + 100 * count e= nd if number !=3D 1 and count =3D=3D 6 then return 600 end if number =3D=3D 1 and (count > 3) then total =3D total + ( 100 + ( coun= t - 3)) end if number =3D=3D 1 and (count < 3) then total =3D total + 100 * count = end if number =3D=3D 5 and (count > 3) then total =3D total + ( 50 * (count = -3)) end if number =3D=3D 5 and (count < 3) then total =3D total + 50 * count en= d } end =20 class AboutScoringProject < EdgeCase::Koan def test_score_of_an_empty_list_is_zero assert_equal 0=2C score([]) end def test_score_of_a_single_roll_of_5_is_50 assert_equal 50=2C score([5]) end def test_score_of_a_single_roll_of_1_is_100 assert_equal 100=2C score([1]) end def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores assert_equal 300=2C score([1=2C5=2C5=2C1]) end def test_score_of_single_2s_3s_4s_and_6s_are_zero assert_equal 0=2C score([2=2C3=2C4=2C6]) end def test_score_of_a_triple_1_is_1000 assert_equal 1000=2C score([1=2C1=2C1]) end def test_score_of_other_triples_is_100x assert_equal 200=2C score([2=2C2=2C2]) assert_equal 300=2C score([3=2C3=2C3]) assert_equal 400=2C score([4=2C4=2C4]) assert_equal 500=2C score([5=2C5=2C5]) assert_equal 600=2C score([6=2C6=2C6]) end def test_score_of_mixed_is_sum assert_equal 250=2C score([2=2C5=2C2=2C2=2C3]) assert_equal 550=2C score([5=2C5=2C5=2C5]) 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=2C score([]) end Then I see this error appear :=20 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=2C 26 Sep 2012 03:26:55 +0900 > From: lists@ruby-forum.com > Subject: Re: inject problem > To: ruby-talk@ruby-lang.org >=20 > Roelof Wobben wrote in post #1077503: > > h =3D Hash.new(0) > > dice.each {|el| dice[el] +=3D 1} > > [...] > > But now I'm getting this error : undefined method `+' for nil:NilClass >=20 > You want h[el] +=3D 1=2C not dice[el] +=3D 1. See the original code by Je= s=FAs. >=20 > --=20 > Posted via http://www.ruby-forum.com/. = --_0f1251f3-5eb1-4ebc-ae6c-123942915b2d_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Thanks but I did not solve my problem.

Here is the whole script :
require File.expand_path(File.dirname(__FILE__) + '/edgecase')

= # Greed is a dice game where you roll up to five dice to accumulate
# po= ints. =3B The following "score" function will be used to calculate the<= br># score of a single roll of the dice.
#
# A greed roll is scored a= s follows:
#
# * A set of three ones is 1000 points
#
# * A set= of three numbers (other than ones) is worth 100 times the
# =3B&nbs= p=3B number. (e.g. three fives is 500 points).
#
# * A one (that is n= ot 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 els= e is worth 0 points.
#
#
# Examples:
#
# score([1=2C1=2C1=2C= 5=2C1]) =3D>=3B 1150 points
# score([2=2C3=2C4=2C6=2C2]) =3D>=3B 0 p= oints
# score([3=2C4=2C5=2C3=2C3]) =3D>=3B 350 points
# score([1=2C= 5=2C1=2C2=2C4]) =3D>=3B 250 points
#
# More scoring examples are gi= ven in the tests below:
#
# Your goal is to write the score method.
def score(dice)
 =3B total =3D 0
 =3B h =3D Hash.new(0= )
 =3B dice.each {|el| h[el] +=3D 1}

 =3Bdice.each {|num= ber=2C count|
 =3B =3B if number =3D=3D 1 and count =3D=3D 6 the= n return 2000 end
 =3B =3B if number !=3D 1 and count =3D=3D 3 t= hen total =3D total + 100 * count end
 =3B =3B if number !=3D 1 = and count =3D=3D 6 then return 600 end
 =3B =3B if number =3D=3D= 1 and (count >=3B 3) then total =3D total + ( 100 + ( count - 3)) end =3B =3B if number =3D=3D 1 and (count <=3B 3) then total =3D to= tal =3B =3B + 100 * count end
 =3B =3B if number =3D=3D = 5 and (count >=3B 3) then total =3D total + ( 50 * (count -3)) end
&nb= sp=3B =3B if number =3D=3D 5 and (count <=3B 3) then total =3D total = + =3B 50 * count end
 =3B =3B =3B }

end
 = =3B

class AboutScoringProject <=3B EdgeCase::Koan
 =3B def= test_score_of_an_empty_list_is_zero
 =3B =3B =3B assert_equ= al 0=2C score([])
 =3B end

 =3B def test_score_of_a_singl= e_roll_of_5_is_50
 =3B =3B =3B assert_equal 50=2C score([5])=
 =3B end

 =3B def test_score_of_a_single_roll_of_1_is_10= 0
 =3B =3B =3B assert_equal 100=2C score([1])
 =3B en= d

 =3B def test_score_of_multiple_1s_and_5s_is_the_sum_of_indivi= dual_scores
 =3B =3B =3B assert_equal 300=2C score([1=2C5=2C= 5=2C1])
 =3B end

 =3B def test_score_of_single_2s_3s_4s_a= nd_6s_are_zero
 =3B =3B =3B assert_equal 0=2C score([2=2C3= =2C4=2C6])
 =3B end

 =3B def test_score_of_a_triple_1_is_= 1000
 =3B =3B =3B assert_equal 1000=2C score([1=2C1=2C1]) =3B end

 =3B def test_score_of_other_triples_is_100x
&n= bsp=3B =3B =3B assert_equal 200=2C score([2=2C2=2C2])
 =3B&n= bsp=3B =3B assert_equal 300=2C score([3=2C3=2C3])
 =3B =3B&n= bsp=3B assert_equal 400=2C score([4=2C4=2C4])
 =3B =3B =3B a= ssert_equal 500=2C score([5=2C5=2C5])
 =3B =3B =3B assert_eq= ual 600=2C score([6=2C6=2C6])
 =3B end

 =3B def test_scor= e_of_mixed_is_sum
 =3B =3B =3B assert_equal 250=2C score([2= =2C5=2C2=2C2=2C3])
 =3B =3B =3B assert_equal 550=2C score([5= =2C5=2C5=2C5])
 =3B end

end

And I now get this error: = <=3B0>=3B expected but was =3B <=3B[]>=3B.
When I make that = one a comment :

def test_score_of_an_empty_list_is_zero
 =3B =3B =3B #assert_equal 0=2C score([])
 =3B end

Then I see this error appear :

The answers you = seek...
 =3B undefined method `>=3B' for nil:NilClass

Pleas= e meditate on the following code:
 =3B ./about_scoring_project.rb:43= :in `score'
 =3B ./about_scoring_project.rb:37:in `each'
 =3B= ./about_scoring_project.rb:37:in `score'
 =3B ./about_scoring_proje= ct.rb:56:in `test_score_of_a_single_roll_of_5_is_50'
 =3B /home/roel= of/koans/edgecase.rb:398:in `send'
 =3B /home/roelof/koans/edgecase.= rb:398:in `meditate'
 =3B /home/roelof/koans/edgecase.rb:470:in `wal= k'
 =3B /home/roelof/koans/edgecase.rb:481:in `each_step'
 = =3B /home/roelof/koans/edgecase.rb:479:in `each'
 =3B /home/roelof/k= oans/edgecase.rb:479:in `each_step'
 =3B path_to_enlightenment.rb:38= :in `each_with_index'
 =3B /home/roelof/koans/edgecase.rb:478:in `ea= ch'
 =3B /home/roelof/koans/edgecase.rb:478:in `each_with_index'
=  =3B /home/roelof/koans/edgecase.rb:478:in `each_step'
 =3B /hom= e/roelof/koans/edgecase.rb:476:in `catch'
 =3B /home/roelof/koans/ed= gecase.rb:476:in `each_step'
 =3B /home/roelof/koans/edgecase.rb:469= :in `walk'
 =3B /home/roelof/koans/edgecase.rb:491
 =3B path_= to_enlightenment.rb:38


So something is not well here ?

Ro= elof



>=3B Date:= Wed=2C 26 Sep 2012 03:26:55 +0900
>=3B From: lists@ruby-forum.com
= >=3B Subject: Re: inject problem
>=3B To: ruby-talk@ruby-lang.org>=3B
>=3B Roelof Wobben wrote in post #1077503:
>=3B >=3B = h =3D Hash.new(0)
>=3B >=3B dice.each {|el| dice[el] +=3D 1}
&= gt=3B >=3B [...]
>=3B >=3B But now I'm getting this error : undefi= ned method `+' for nil:NilClass
>=3B
>=3B You want h[el] +=3D 1= =2C not dice[el] +=3D 1. See the original code by Jes=FAs.
>=3B
&g= t=3B --
>=3B Posted via http://www.ruby-forum.com/.
=
= --_0f1251f3-5eb1-4ebc-ae6c-123942915b2d_--