the twelve days of xmas


See On Github

Data

Contributor

Generic placeholder thumbnail

by Yonaba

in lua

Tags

Source Code

-- Lyrics for the song "The Twelve Days Of Xmas"
-- See here: http://www.lyricsmode.com/lyrics/c/christmas_carols/the_twelve_days_of_christmas.html

-- The days
local days = {
  'first', 'second', 'third', 'fourth', 'fifth', 'sixth',
  'seventh', 'eighth', 'nineth', 'tenth', 'eleventh', 'twelfth'
}

-- The gifts my true love gave to me
local gifts = {
  "A partridge in a pear tree.",
  "Two turtle doves",
  "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",
}

-- day: (optional) the number of the day at which the song will start. Defaults to 1
-- returns : the lyrics of the song, as a string
local function the_12_days_of_xmas(day)
  day = day or 1
  assert(day >= 1 and day <= 12, 'day number has to be within the [1..12] range')
  local lyrics = {}
  
  for i = day, 12 do
    table.insert(lyrics, ('On the %s day of Christmas '):format(days[i]))
    table.insert(lyrics, 'My true love gave to me:')
    for j = i, 1, -1 do
      table.insert(lyrics, gifts[j])      
    end
    table.insert(lyrics, '')
  end
  return table.concat(lyrics, '\n')
  
end

return the_12_days_of_xmas
-- Tests for the12daysofxmas.lua
local the12daysofxmas = require 'the12daysofxmas'

local total, pass = 1, 1

local function dec(str, len)
  return #str < len
     and str .. (('.'):rep(len-#str))
      or str:sub(1,len)
end

print('Lyrics from the start:\n')
local lyrics = the12daysofxmas()
print(lyrics)
print(('-'):rep(80))

print('Lyrics from the nineth verse:\n ')
lyrics = the12daysofxmas(9)
print(lyrics)

print('\n'..('-'):rep(80))
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
  :format(total, pass, total-pass, (pass*100/total)))