Inject problem

Thanks but I won’t solve my problem.

When I change the method to this :

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 total = 2000 end
if number != 1 and count == 3 then total = total + 100 * count end
if number != 1 and count == 6 then total = 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
}

return total

end

then I see this error message :

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:ineach’
./about_scoring_project.rb:37:in score' ./about_scoring_project.rb:58:intest_score_of_a_single_roll_of_5_is_50’
/home/roelof/koans/edgecase.rb:398:in send' /home/roelof/koans/edgecase.rb:398:inmeditate’
/home/roelof/koans/edgecase.rb:470:in walk' /home/roelof/koans/edgecase.rb:481:ineach_step’
/home/roelof/koans/edgecase.rb:479:in each' /home/roelof/koans/edgecase.rb:479:ineach_step’
path_to_enlightenment.rb:38:in each_with_index' /home/roelof/koans/edgecase.rb:478:ineach’
/home/roelof/koans/edgecase.rb:478:in each_with_index' /home/roelof/koans/edgecase.rb:478:ineach_step’
/home/roelof/koans/edgecase.rb:476:in catch' /home/roelof/koans/edgecase.rb:476:ineach_step’
/home/roelof/koans/edgecase.rb:469:in `walk’
/home/roelof/koans/edgecase.rb:491
path_to_enlightenment.rb:38

Thanks.

I know I have a lot to learn.
I’m just beginning with ruby and in this moments I miss a IDE where I
can do everything step by step so I can see what went wrong.

Now refractor the if then 's and I hope I can decline it to less then 5
if then’s.
But that will be difficult.

Roelof

On Wed, Sep 26, 2012 at 9:39 AM, Roelof W. [email protected]
wrote:

Now refractor the if then 's and I hope I can decline it to less then 5 if
then’s.
But that will be difficult.

I think you should not structure the code this way. I think you should
loop through the counts, finding and removing sets. Something like
(pseudocode):

while there are sets of 3 ones:
remove the set from the hash (h[1] -= 3)
sum 1000 to the score
end

while there are sets of 3 any other pip count:
remove the set from the hash (h[x] -= 3)
sum 100*x to the score
end

sum 100 * (remaining number of ones)
sum 50 * (remaining number of fives)

This way, your code is more simple and robust. For example this will
also work if you throw 9 dice, while your current algorithm does not.

Jesus.

No problem.

I try to learn ruby by following ruby koans.
But I think now it’s not a good idea.

They work with test-driven development.
So every test on koans fail but most of the times in the error message
is hidden what the right answer is.
In the end you have to use that knowlegde to solve puzzles like this.

So i’m looking for a way that works for me.

Roelof

Roelof W. wrote in post #1077587:

I’m just beginning with ruby and in this moments I miss a IDE where I
can do everything step by step so I can see what went wrong.

Well, I wouldn’t rely too much on the IDE. The danger of this is that it
leads to a kind of “trial and error” programming: You just write
something down and then use the debugger to make actual working code out
of it.

That’s not how it should be, especially when you’ve just started. The
code reflects your thoughts, so it should at least be logical in itself.
Sure, we all make mistakes, so there will always be typos, syntax errors
etc. But when your code is logical incorrect, it shows that you’ve not
really thought this through (or maybe didn’t even understand the parts
you’ve copied and pasted).

This is no critique or something, just a suggestion. In school exams, we
actually have to write down our code on a piece of paper. This way we
focus on clear thoughts, not so much on getting the code running
somehow.

Now refractor the if then 's and I hope I can decline it to less then 5
if then’s.

I don’t understand the logic, so I can’t help you with that. But you
should structure the cases with nested "if"s.

Interresting idea.

I will try if I can make it work this afternoon or this evening.

Roelof