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!
I didn’t look at yours but they seem to be variants of the same idea
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")))
Impressive!
The only thing is that the first lines with “On the…” are indented. Is this intentional?
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!
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 = '')
Perfect!
Really impressive, thank you for this great solution to the puzzle!
Here is my solution, I also went for the loop and I’m very glad I got it right! It was fun and useful:
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)).
I am glad that you liked it, Maurizio!
When I run the code I get no separating lines between the verses and all lines except the first (starting with “On the…) are indented.
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.
Strange, I get:
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.
Here is my solution, i tried solution using generic function with dependent package
english
Very good, Daya!
I was thinking about using the
english
package myself but dismissed the idea again. Perhaps it can also be used for the numbers at the beginning of each line?Yes. Only the line ‘A partridge in a pear tree’ doesn’t have the number starting with.
I am really impressed with the efficiency of your loading page. Delivery of content was speedy and excellent responsive design. Where do you host your site?
Thank you, IONOS is my hosting provider and I am quite satisfied with their service.