From: Alex LeDonne Date: 2007-06-19T02:50:11+09:00 Subject: Re: Another Easy Beginner Question On 6/18/07, Daniel Kempkens wrote: > 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? > 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