From: Daniel Kempkens Date: 2007-06-19T02:45:02+09:00 Subject: Re: Another Easy Beginner Question Alex LeDonne schrieb: > On 6/18/07, danielj 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? >> (Hint: You will need a loop inside another). >> >> 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 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?