Learning R: Christmas Coding Challenge


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!

17 thoughts on “Learning R: Christmas Coding Challenge”

  1. I didn’t look at yours but they seem to be variants of the same idea

    s <- "On the "
    l <- " day of Chirstmas\nMy true love gave to me:\n"
    cat(sapply(1:length(days), function(x)paste0(s,days[x],l,paste0(gifts[x:1],collapse = "\n"),"\n\n")))
    
  2. Or, as a oneliner

    cat(sapply(1:length(days), function(x)paste0("On the ",days[x]," day of Chirstmas\nMy true love gave to me:\n",paste0(gifts[x:1],collapse = "\n"),"\n\n")))

      1. No it wasn’t intentional. Sorry, I didn’t notice it. It is due the the default separator being ” ” for cat. This fixes it.

        cat(sapply(1:length(days), function(x)paste0(s,days[x],l,paste0(gifts[x:1],collapse = "\n"),"\n\n")),sep = '')

        Thanks for the fun puzzle!

        1. And for the one liner

          cat(sapply(1:length(days), function(x)paste0("On the ",days[x]," day of Chirstmas\nMy true love gave to me:\n",paste0(gifts[x:1],collapse = "\n"),"\n\n")),sep = '')

  3. Here is my solution, I also went for the loop and I’m very glad I got it right! It was fun and useful:

    for(i in 1:12) {
      cat(paste("On the",days[i],"day of Christmas","\n","my true love sent to me","\n"),paste(gifts[seq(i,1)],"\n"))
    }
    

    The most evident (to me) problem with my code, after reading yours, is that I used 12 instead of the general solution you provide (length(days)).

      1. I closed down R Studio and re-run the code in a new session. The verses are correctly separated in lines, exactly as in the excerpt that you provided in the blog post.
        As for the indentation, I noticed it as well, but I can’t figure out what it is caused by.

        1. Strange, I get:

          ## 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. 
          ## On the third day of Christmas 
          ##  my true love sent to me 
          ##  Three french hens 
          ##  Two turtle doves and 
          ##  A partridge in a pear tree. 
          ## On the fourth day of Christmas 
          ##  my true love sent to me 
          ##  Four calling birds 
          ##  Three french hens 
          ##  Two turtle doves and 
          ##  A partridge in a pear tree.
          ...
          
          1. Now I got what you mean: each verse is separated from the other by an empty line.
            It seems that the way I structured my code does not allow me to obtain that.
            I must change it somehow.

  4. Here is my solution, i tried solution using generic function with dependent package english

    library(english)
    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")
    
    ccc = function(days=12){
      for(j in 1:days){
        cat(paste0("On the ", ordinal(j), " day of Christmas\n"))
        cat("My true love gave to me:", "\n")
        cat(paste(gifts[j:1], collapse = "\n"), "\n\n")  
      }
    }
    
    ccc(12)
    
  5. library (english)
    
    
    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")
    
    
    ccc = function(days=12){
      for(j in 1:days){
        cat(paste0("On the ", ordinal(j), " day of Christmas\n"))
        cat("My true love gave to me:", "\n")
        cat(paste(gifts[j:1], collapse = "\n"), "\n\n")  
      }
    }
    
    ccc(12)
    

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.