[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Another Easy Beginner Question

danielj

6/18/2007 4:50:00 PM


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
gifts = 0

12.times do
day = day + 1
gifts = gifts + day
end

print gifts

Does anyone know how you would do it with a <em>loop inside a loop</
em>?

15 Answers

Sammy Larbi

6/18/2007 5:10:00 PM

0

I'm not sure why you'd want to, but I suppose you could do something like:

gifts=0
(1..12).each do |i|
(1..i).each { |j| gifts += 1 }
end
puts gifts

danielj wrote, On 6/18/2007 11:50 AM:
> 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
> gifts = 0
>
> 12.times do
> day = day + 1
> gifts = gifts + day
> end
>
> print gifts
>
> Does anyone know how you would do it with a <em>loop inside a loop</
> em>?
>
>
>
>
>


danielj

6/18/2007 5:19:00 PM

0

I just wanted to do the challenge correctly...

Although, maybe I shouldn't be doing a tutorial with that kind of lame
challenge...

Alex LeDonne

6/18/2007 5:29:00 PM

0

On 6/18/07, danielj <danielj@sleepingindian.org> wrote:
>
> 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
> gifts = 0
>
> 12.times do
> day = day + 1
> gifts = gifts + day
> end
>
> print gifts
>
> 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

Stephen Ball

6/18/2007 5:32:00 PM

0

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
gifts_for_the_day = 0
total_gifts = 0

12.times do
day += 1
gifts_for_the_day += day
total_gifts += gifts_for_the_day
puts "Day: #{day}, Gifts for Day: #{gifts_for_the_day}, Total gifts:
#{total_gifts}"
end

-- Stephen

On 6/18/07, danielj <danielj@sleepingindian.org> wrote:
> I just wanted to do the challenge correctly...
>
> Although, maybe I shouldn't be doing a tutorial with that kind of lame
> challenge...
>
>
>

Daniel Kempkens

6/18/2007 5:42:00 PM

0

Alex LeDonne schrieb:
> On 6/18/07, danielj <danielj@sleepingindian.org> wrote:
>>
>> 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
>> gifts = 0
>>
>> 12.times do
>> day = day + 1
>> gifts = gifts + day
>> end
>>
>> print gifts
>>
>> 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
>
Well, I solved this for fun, my code looks like this:

def gift_counter(max = 12)
gifts = 0
1.upto(max) do |day|
gifts += day # I guess 'day' is a bad var-name here :D
puts "Day: {#day}, Gifts: #{gifts}"
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?

Alex LeDonne

6/18/2007 5:50:00 PM

0

On 6/18/07, Daniel Kempkens <Daniel.Kempkens@gmail.com> wrote:
> Alex LeDonne schrieb:
> > On 6/18/07, danielj <danielj@sleepingindian.org> wrote:
> >>
> >> 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
> >> gifts = 0
> >>
> >> 12.times do
> >> day = day + 1
> >> gifts = gifts + day
> >> end
> >>
> >> print gifts
> >>
> >> 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
> >
> Well, I solved this for fun, my code looks like this:
>
> def gift_counter(max = 12)
> gifts = 0
> 1.upto(max) do |day|
> gifts += day # I guess 'day' is a bad var-name here :D
> puts "Day: {#day}, Gifts: #{gifts}"
> 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
is asking for a gift_totaler.

-A

Gavin Kistner

6/18/2007 5:53:00 PM

0

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.

> Well, I solved this for fun, my code looks like this:
>
> def gift_counter(max = 12)
> gifts = 0
> 1.upto(max) do |day|
> gifts += day # I guess 'day' is a bad var-name here :D
> puts "Day: {#day}, Gifts: #{gifts}"
> 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?

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.

Sammy Larbi

6/18/2007 6:11:00 PM

0

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


Matt Filizzi wrote, On 6/18/2007 12:19 PM:
> Wouldn't it be this...
>
> gifts = 0
> (1..12).each do |x|
> (1..x).each do { |y| gifts += y }
> end
>
> puts gifts
>
> Note the difference between mine and yours, where I count each item
> instead
> 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 6/18/07, Sammy Larbi <sam@powersource.com> wrote:
>>
>> I'm not sure why you'd want to, but I suppose you could do something
>> like:
>>
>> gifts=0
>> (1..12).each do |i|
>> (1..i).each { |j| gifts += 1 }
>> end
>> puts gifts
>>
>> danielj wrote, On 6/18/2007 11:50 AM:
>> > 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
>> > gifts = 0
>> >
>> > 12.times do
>> > day = day + 1
>> > gifts = gifts + day
>> > end
>> >
>> > print gifts
>> >
>> > Does anyone know how you would do it with a <em>loop inside a loop</
>> > em>?
>> >
>> >
>> >
>> >
>> >
>>
>>
>>
>


Daniel Kempkens

6/18/2007 6:32:00 PM

0

Phrogz schrieb:
> 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.
>
>> Well, I solved this for fun, my code looks like this:
>>
>> def gift_counter(max = 12)
>> gifts = 0
>> 1.upto(max) do |day|
>> gifts += day # I guess 'day' is a bad var-name here :D
>> puts "Day: {#day}, Gifts: #{gifts}"
>> 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?
>
> 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.

danielj

6/18/2007 7:04:00 PM

0

I see...

Thanks all...

It was really helpful to see the problem thought about many different
ways