# Forum: Ruby Another Easy Beginner Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2007-06-18 18:51
```There is a song that goes like this:

On the first day of Christmas, my true love sent to me a partridge
in a pear tree.
On the second day of Christmas, my true love sent to me two turtle
doves and a partridge in a pear tree.
...

If this goes on for the 12 days of Christmas. How many presents will
your true love send you over Christmas?
<strong>(Hint: You will need a loop inside another). </strong>

I don't think you need a loop inside a loop.... Here is what I did:

day = 0

12.times do
day = day + 1
end

Does anyone know how you would do it with a <em>loop inside a loop</
em>?```
on 2007-06-18 19:10
```I'm not sure why you'd want to, but I suppose you could do something
like:

(1..12).each do |i|
(1..i).each { |j| gifts += 1 }
end
on 2007-06-18 19:20
```Wouldn't it be this...

(1..12).each do |x|
(1..x).each do { |y| gifts += y }
end

Note the difference between mine and yours,  where I count each item
of each set.  So 5 gold rings counts as 5 instead of 1.  I think it all
depends on how the original question was intended.```
on 2007-06-18 19:21
```I just wanted to do the challenge correctly...

Although, maybe I shouldn't be doing a tutorial with that kind of lame
challenge...```
on 2007-06-18 19:29
```On 6/18/07, danielj <danielj@sleepingindian.org> wrote:
> your true love send you over Christmas?
> end
>
>
> Does anyone know how you would do it with a <em>loop inside a loop</
> em>?

Note that your program does not solve the problem (which is easy to
misinterpret). To clarify the problem:

On day 1, you get 1 gift.
On day 2, you get 3 gifts, not two (two doves AND one bird/tree combo).
On day 3, you get 6 gifts - 3 hens, 2 doves, one more pear tree
w/attendant partridge.
and so on... until
On day 11 you get 66 gifts, and
On day 12 you get 78 gifts.

Do you see how a loop within a loop may be helpful?

Good luck!

-A```
on 2007-06-18 19:32
```Remember to count each gift for each day (on the second day you get
another bird in a tree, on the third day you get two more doves and
another bird in a tree, etc.)

day = 0

12.times do
day += 1
end

-- Stephen```
on 2007-06-18 19:45
```Alex LeDonne schrieb:
>> If this goes on for the 12 days of Christmas. How many presents will
>
>
> Good luck!
>
> -A
>
Well, I solved this for fun, my code looks like this:

1.upto(max) do |day|
gifts += day # I guess 'day' is a bad var-name here :D
end
end

It works quite good, so why does this Quiz-Site suggest to use two
loops? Am I getting something of this quiz wrong?```
on 2007-06-18 19:51
```On 6/18/07, Daniel Kempkens <Daniel.Kempkens@gmail.com> wrote:
> >>
> >>  day = day + 1
> > misinterpret). To clarify the problem:
> > Do you see how a loop within a loop may be helpful?
>    gifts += day # I guess 'day' is a bad var-name here :D
>   end
> end
>
> It works quite good, so why does this Quiz-Site suggest to use two
> loops? Am I getting something of this quiz wrong?
>

Well, the original question is:
What is the total number of gifts given over the 12 days?

The gift_counter is good for the number by-the-day, but the question

-A```
on 2007-06-18 19:55
```On Jun 18, 11:42 am, Daniel Kempkens <Daniel.Kempk...@gmail.com>
wrote:
> Alex LeDonne schrieb:
> > On day 1, you get 1 gift.
> > On day 2, you get 3 gifts, not two (two doves AND one bird/tree combo).
> > On day 3, you get 6 gifts - 3 hens, 2 doves, one more pear tree
> > w/attendant partridge.

> It works quite good, so why does this Quiz-Site suggest to use two
> loops? Am I getting something of this quiz wrong?

Does it work? It tells you how many gifts you GET on each day, but
does not tell you how many gifts you HAVE when the day is done.

On day 1 you get 1 gift, resulting in 1 gift owned by you.
On day 2 you get 3 gifts, resulting in 4 gifts owned by you.
On day 3 you get 6 gifts, resulting in 10 gifts owned by you.```
on 2007-06-18 20:11
```I originally thought it boiled down to

1+2+...+11+12=78

But now that I think about it again, I'm pretty sure I was wrong.  The
correct sequence is:

(1) + (1+2) + (1+2+3) + ... + (1+2+...+11+12)

Which I think makes you right.

Sam```
on 2007-06-18 20:35
```Phrogz schrieb:
> Does it work? It tells you how many gifts you GET on each day, but
> does not tell you how many gifts you HAVE when the day is done.
>
> On day 1 you get 1 gift, resulting in 1 gift owned by you.
> On day 2 you get 3 gifts, resulting in 4 gifts owned by you.
> On day 3 you get 6 gifts, resulting in 10 gifts owned by you.
>
I think I missed the total-part of the quiz ;) But implementing a
total-counter wouldn't be much difficult.```
on 2007-06-18 21:06
```I see...

Thanks all...

It was really helpful to see the problem thought about many different
ways```
on 2007-06-18 21:42
```On 6/18/07, Daniel Kempkens <Daniel.Kempkens@gmail.com> wrote:

> I think I missed the total-part of the quiz ;) But implementing a
> total-counter wouldn't be much difficult.

Wouldn't it be:

def fact(n); n > 1 ? n + fact(n-1) : 1; end
puts (1..12).inject(0) { |s,r| s + fact(r) }```
on 2007-06-18 21:56
```On Jun 18, 3:40 pm, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
> Wouldn't it be:
>
> def fact(n); n > 1 ? n + fact(n-1) : 1; end
> puts (1..12).inject(0) { |s,r| s + fact(r) }

That certainly works, although I'm personally uncomfortable with the
name of your recursive method; "fact" seems to imply factorial, which
this is not.

If you like inject, how about a pair of 'em:

puts (1..12).inject(0) { |sum, day|
}

Eric
----
Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises? http://LearnRuby.com```
on 2007-06-18 23:04
```On Jun 18, 2007, at 1:19 PM, Matt Filizzi wrote:

> Wouldn't it be this...
>
> (1..12).each do |x|
>   (1..x).each do { |y| gifts += y }
^^
> end

Not quite. It should be:

<code>
(1..12).each do |x|
(1..x).each { |y| gifts += y }
end
</code>

But this problem has a nice closed-form solution:

<code>
days * (days +1) * (days + 2) / 6
end
</code>

Regards, Morton```
on 2007-06-18 23:10
```On 6/18/07, Eric I. <rubytraining@gmail.com> wrote:
> On Jun 18, 3:40 pm, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
> > Wouldn't it be:
> >
> > def fact(n); n > 1 ? n + fact(n-1) : 1; end
> > puts (1..12).inject(0) { |s,r| s + fact(r) }
>
> That certainly works, although I'm personally uncomfortable with the
> name of your recursive method; "fact" seems to imply factorial, which
> this is not.

Dur. You're right.  12! = 12*11*10*..

/me is a math major, too.

> If you like inject, how about a pair of 'em:
>
> puts (1..12).inject(0) { |sum, day|
> }

yup, that looks pretty good.```
on 2007-06-18 23:17
```Eric I. wrote:
> puts (1..12).inject(0) { |sum, day|
> }

I'd do it as shown below, but it's probably not covered by the point in
the book you're currently at:

irb(main):001:0> class Integer
irb(main):002:1>   def int_sum
irb(main):003:2>     (1..self).inject(0){|sum,n|sum+n}
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> puts (1..12).inject(0){|sum,n|sum+n.int_sum}
364
=> nil```
This topic is locked and can not be replied to.