Learning R: Christmas Coding Challenge

[This article was first published on R-Bloggers – Learning Machines, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


In this year’s end post I will give you a little programming challenge!

Everybody knows the Christmas song “The Twelve Days of Christmas”! Your task is to write an R script that creates the lyrics!

The lyrics are the following:

On the first day of Christmas
My true love gave to me:
A partridge in a pear tree.

On the second day of Christmas
My true love gave to me:
Two turtle doves and
A partridge in a pear tree.

On the third day of Christmas
My true love gave to me:
Three french hens
Two turtle doves and
A partridge in a pear tree.

On the forth day of Christmas
My true love gave to me:
Four calling birds
Three french hens
Two turtle doves and
A partridge in a pear tree.

On the Twelfth day of Christmas,
My true love gave to me:
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 and
A partridge in a pear tree.

Your challenge is to write an R script to create the above lyrics. I provide the building blocks here as a starting point:

gifts <- c("A partridge in a pear tree.", "Two turtle doves and", "Three french hens", "Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", "Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", "Eleven pipers piping", "Twelve drummers drumming")
days <- c("first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth")

Hint: for the output you can use the cat function, to concatenate (combine) strings you can use paste and for a new line use "\n".

I will provide my solution below but you should give it a try… Have fun!

Here is my solution, which I also posted on Rosetta Code: The Twelve Days of Christmas:

for (i in 1:length(days)) {
  cat("On the", days[i], "day of Christmas\n")
  cat("My true love gave to me:\n")
  cat(paste(gifts[i:1], collapse = "\n"), "\n\n")
}

I am always amazed at how elegantly one can code with R! If you have other solutions please don’t hesitate to share them with us in the comment section below.


I wish you all a Merry Christmas, Happy Holidays and A Happy New Year! (Hopefully, 2021 will be a better one than 2020!)
And above all: Please stay safe!

We will be taking our Christmas break and will be back on January 12, 2021!

To leave a comment for the author, please follow the link and comment on their blog: R-Bloggers – Learning Machines.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)