Question

FOR RUBY:

-----------------------------------

Create a program named Gifts. This program will describe the gifts for the 12 Days of Christmas.

  1. Each day describes one gift only. You cannot explicitly output the list of gifts, you need to get each day to display. For instance, for the 3rd day of Christmas, you display the gift for the 3rd day, then display the gift for the 2nd day, then the gift for the 1st day.
  2. Think about the logic for this.. in the song, it counts up from one to 12. For each day starting with day 2, it counts backward to repeat the gifts. (Hint: nested loops)
  3. Use number days, not words. For instance, should be 1st, 2nd, 3rd, 4th, etc. You'll have to figure out how to append 1, 2, and 3 with the correct letters, but 4-12 all use 'th'.
  4. This is not a long program. You'll need to get it done in under 50 lines, using standard Ruby format. That means indenting, having loop beginning and end on their own lines, etc.
  5. Include internal documentation of name, and date.

Output should look similar to: x ---------- Capture Output ---------- > C:\Ruby25-x64\bin\rubyw.exe xmas. rb On the 1st day

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:-

Below is the required code.Here, instead of using nested loops to traverse the gifts backwards from second day onwards, I have used inbuilt array functions for slicing ,reversing and joining.

#the gifts array

Gifts=["twelve drummers drumming","eleven pipers piping","ten lords a leaping","nine ladies dancing","eight maids a milking","seven swans a swimming","six geese a laying","five golden rings","four calling birds","three french hens","two turtle doves","a partidge in a pear tree"].reverse

app=""

#traversing for each day

for i in 0..11

#checking for st,nd,rd or th

if (i+1)==1

p="st"

elsif (i+1)==2

p="nd"

elsif (i+1)==3

p="rd"

else

p="th"

end

gift=Gifts[i]

#forming string of gifts backwards, second day onwards

if i>0

gift=Gifts.slice(1,i).reverse.join(", ")+",and "+Gifts[0]

end

#outputting song line

puts "On the #{i+1}#{p} day of Christmas, my true love gave to me : #{gift}\n"

end

1. #the gifts array 2. Gifts=[twelve drummers drummin g, eleven pipers piping,ten lor ds a leaping,nine ladies dancin22. puts On the #{i+1}#{p} day of Christmas, my true love gave to me : #gift}\n end 23. Success #stdin #stdout Os 6472KBOn the 1st day of Christmas, my true love gave to me : a partidge in a pe ar tree On the 2nd day of Christmas, my true love gn rings, four calling birds, three f rench hens, two turtle doves, and a p artidge in a pear tree On the 8th day of Christmases dancing, eight maids a milking, s even swans a swimming, six geese a l aying, five golden rings, four calli ng birds, threNote:- If you have any doubts please comment below,i am happily help to u brother, please thumbs-up bro and don't give any negative reviews please bro????

Add a comment
Know the answer?
Add Answer to:
FOR RUBY: ----------------------------------- Create a program named Gifts. This program will describe the gifts for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TwelveDays {    public static void main(String[] args)    {       final...

    import java.util.Scanner; public class TwelveDays {    public static void main(String[] args)    {       final int MAX = 12;       int lastDay = 0; //last day for the song, user will update       Scanner scan = new Scanner(System.in);       //Get the last day and use input validation //Begin 1st while       {       }                     int day = 1;      //loop control variable for song verses       //Begin 2nd while       {          //Output: "On the" + day...

  • Rachel's Story A mothers' account of raising a transgendered child Rachel's mother is the author of...

    Rachel's Story A mothers' account of raising a transgendered child Rachel's mother is the author of this story. She writes about the first few years of Rachel's life when she was known as Ryan. This is the story of how Ryan became Rachel. Rachel is a beautiful, well-adjusted, happy and healthy nine year old little girl. She likes to do all the things little girls at this age like to do. Sleepovers and ice-cream sundaes are by far her favorite...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT