--_c65024c6-659c-42f6-bf9a-278d24b70900_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


Hello, 

I thought I solved this problem with this code :

def score(dice)
  total = 0 
  h = Hash.new(0)
  dice.each {|el| dice[el] += 1}

  dice.each {|number, count|
   if number == 1 and count == 3 then total = total + 1000 end
   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
    }
  return total  
end

But now I'm getting this error : undefined method `+' for nil:NilClass

Roelof


> Date: Tue, 25 Sep 2012 23:31:12 +0900
> From: lists / ruby-forum.com
> Subject: Re: inject problem
> To: ruby-talk / ruby-lang.org
> 
> The first parameter of the "inject" block is always the intermediate 
> result used for the aggregation. And the second is the current element.
> 
> For example:
> 
> # calculate sum of 1, 2, ..., 10 (in an inefficient way)
> sum = (1..10).inject do |intermediate_sum, integer|
>   intermediate_sum + integer
> end
> 
> Or in your case:
> 
> count = [1, 2, 1, 1]
> hash = count.inject({}) do |intermediate_hash, number|
>   intermediate_hash[number] += 1 rescue intermediate_hash[number] = 1
>   intermediate_hash
> end
> puts hash
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
 		 	   		  -_c65024c6-659c-42f6-bf9a-278d24b70900_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
Hello, <br><br>I thought I solved this problem with this code :<br><br>def score(dice)<br>&nbsp; total = 0 <br>&nbsp; h = Hash.new(0)<br>&nbsp; dice.each {|el| dice[el] += 1}<br><br>&nbsp; dice.each {|number, count|<br>&nbsp;&nbsp; if number == 1 and count == 3 then total = total + 1000 end<br>&nbsp;&nbsp; if number == 1 and count == 6 then return 2000 end<br>&nbsp;&nbsp; if number != 1 and count == 3 then total = total + 100 * count end<br>&nbsp;&nbsp; if number != 1 and count == 6 then return 600 end<br>&nbsp;&nbsp; if number == 1 and (count &gt; 3) then total = total + ( 100 + ( count - 3)) end<br>&nbsp;&nbsp; if number == 1 and (count &lt; 3) then total = total&nbsp;&nbsp; + 100 * count end<br>&nbsp;&nbsp; if number == 5 and (count &gt; 3) then total = total + ( 50 * (count -3)) end<br>&nbsp;&nbsp; if number == 5 and (count &lt; 3) then total = total +&nbsp; 50 * count end<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp; return total&nbsp; <br>end<br><br>But now I'm getting this error : undefined method `+' for nil:NilClass<br><br>Roelof<br><br><br><div><div id="SkyDrivePlaceholder"></div>&gt; Date: Tue, 25 Sep 2012 23:31:12 +0900<br>&gt; From: lists / ruby-forum.com<br>&gt; Subject: Re: inject problem<br>&gt; To: ruby-talk / ruby-lang.org<br>&gt; <br>&gt; The first parameter of the "inject" block is always the intermediate <br>&gt; result used for the aggregation. And the second is the current element.<br>&gt; <br>&gt; For example:<br>&gt; <br>&gt; # calculate sum of 1, 2, ..., 10 (in an inefficient way)<br>&gt; sum = (1..10).inject do |intermediate_sum, integer|<br>&gt;   intermediate_sum + integer<br>&gt; end<br>&gt; <br>&gt; Or in your case:<br>&gt; <br>&gt; count = [1,, 1, 1]<br>&gt; hash = count.inject({}) do |intermediate_hash, number|<br>&gt;   intermediate_hash[number] += 1 rescue intermediate_hash[number] = 1<br>&gt;   intermediate_hash<br>&gt; end<br>&gt; putsash<br>&gt; <br>&gt; -- <br>&gt; Posted via http://www.ruby-forum.com/.<br>&gt; <br></div> 		 	   		  </div></body>
</html>-_c65024c6-659c-42f6-bf9a-278d24b70900_--