Skip to content

Commit 075ed54

Browse files
Add twelve-days exercise (#518)
* Add twelve-days exercise * Generate each verse
1 parent e72019b commit 075ed54

File tree

9 files changed

+327
-0
lines changed

9 files changed

+327
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,14 @@
650650
"exception_handling"
651651
]
652652
},
653+
{
654+
"slug": "twelve-days",
655+
"name": "Twelve Days",
656+
"uuid": "51ea559b-818b-47a9-91af-53772c46e171",
657+
"practices": [],
658+
"prerequisites": [],
659+
"difficulty": 3
660+
},
653661
{
654662
"slug": "word-count",
655663
"name": "Word Count",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Instructions
2+
3+
Your task in this exercise is to write code that returns the lyrics of the song: "The Twelve Days of Christmas."
4+
5+
"The Twelve Days of Christmas" is a common English Christmas carol.
6+
Each subsequent verse of the song builds on the previous verse.
7+
8+
The lyrics your code returns should _exactly_ match the full song text shown below.
9+
10+
## Lyrics
11+
12+
```text
13+
On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.
14+
15+
On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.
16+
17+
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.
18+
19+
On the fourth 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.
20+
21+
On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
22+
23+
On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
24+
25+
On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
26+
27+
On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
28+
29+
On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
30+
31+
On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
32+
33+
On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
34+
35+
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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
36+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"twelve-days.lua"
8+
],
9+
"test": [
10+
"twelve-days_spec.lua"
11+
],
12+
"example": [
13+
".meta/example.lua"
14+
]
15+
},
16+
"blurb": "Output the lyrics to 'The Twelve Days of Christmas'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)"
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
local function recite_verse(verse)
2+
local ordinals = {
3+
'first',
4+
'second',
5+
'third',
6+
'fourth',
7+
'fifth',
8+
'sixth',
9+
'seventh',
10+
'eighth',
11+
'ninth',
12+
'tenth',
13+
'eleventh',
14+
'twelfth'
15+
}
16+
17+
local gifts = {
18+
'a Partridge in a Pear Tree.',
19+
'two Turtle Doves, and',
20+
'three French Hens,',
21+
'four Calling Birds,',
22+
'five Gold Rings,',
23+
'six Geese-a-Laying,',
24+
'seven Swans-a-Swimming,',
25+
'eight Maids-a-Milking,',
26+
'nine Ladies Dancing,',
27+
'ten Lords-a-Leaping,',
28+
'eleven Pipers Piping,',
29+
'twelve Drummers Drumming,'
30+
}
31+
32+
local result = { 'On the', ordinals[verse], 'day of Christmas my true love gave to me:' }
33+
for index = verse, 1, -1 do
34+
table.insert(result, gifts[index])
35+
end
36+
37+
return table.concat(result, ' ')
38+
end
39+
40+
local function recite(start_verse, end_verse)
41+
local result = {}
42+
for verse = start_verse, end_verse do
43+
table.insert(result, recite_verse(verse))
44+
end
45+
return result
46+
end
47+
48+
return { recite = recite }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local map = function(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function render_song(lines)
10+
return table.concat(map(lines, function(line)
11+
return '\'' .. line .. '\''
12+
end), ',\n')
13+
end
14+
15+
return {
16+
module_name = 'twelve_days',
17+
18+
generate_test = function(case)
19+
local template = [[
20+
local expected = {
21+
%s
22+
}
23+
assert.are.same(expected, twelve_days.recite(%d, %d))]]
24+
return template:format(render_song(case.expected), case.input.startVerse, case.input.endVerse)
25+
end
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[c0b5a5e6-c89d-49b1-a6b2-9f523bff33f7]
13+
description = "verse -> first day a partridge in a pear tree"
14+
15+
[1c64508a-df3d-420a-b8e1-fe408847854a]
16+
description = "verse -> second day two turtle doves"
17+
18+
[a919e09c-75b2-4e64-bb23-de4a692060a8]
19+
description = "verse -> third day three french hens"
20+
21+
[9bed8631-ec60-4894-a3bb-4f0ec9fbe68d]
22+
description = "verse -> fourth day four calling birds"
23+
24+
[cf1024f0-73b6-4545-be57-e9cea565289a]
25+
description = "verse -> fifth day five gold rings"
26+
27+
[50bd3393-868a-4f24-a618-68df3d02ff04]
28+
description = "verse -> sixth day six geese-a-laying"
29+
30+
[8f29638c-9bf1-4680-94be-e8b84e4ade83]
31+
description = "verse -> seventh day seven swans-a-swimming"
32+
33+
[7038d6e1-e377-47ad-8c37-10670a05bc05]
34+
description = "verse -> eighth day eight maids-a-milking"
35+
36+
[37a800a6-7a56-4352-8d72-0f51eb37cfe8]
37+
description = "verse -> ninth day nine ladies dancing"
38+
39+
[10b158aa-49ff-4b2d-afc3-13af9133510d]
40+
description = "verse -> tenth day ten lords-a-leaping"
41+
42+
[08d7d453-f2ba-478d-8df0-d39ea6a4f457]
43+
description = "verse -> eleventh day eleven pipers piping"
44+
45+
[0620fea7-1704-4e48-b557-c05bf43967f0]
46+
description = "verse -> twelfth day twelve drummers drumming"
47+
48+
[da8b9013-b1e8-49df-b6ef-ddec0219e398]
49+
description = "lyrics -> recites first three verses of the song"
50+
51+
[c095af0d-3137-4653-ad32-bfb899eda24c]
52+
description = "lyrics -> recites three verses from the middle of the song"
53+
54+
[20921bc9-cc52-4627-80b3-198cbbfcf9b7]
55+
description = "lyrics -> recites the whole song"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local function recite(start_verse, end_verse)
2+
3+
end
4+
5+
return { recite = recite }
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
local twelve_days = require('twelve-days')
2+
3+
describe('twelve-days', function()
4+
describe('verse', function()
5+
it('first day a partridge in a pear tree', function()
6+
local expected = { 'On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.' }
7+
assert.are.same(expected, twelve_days.recite(1, 1))
8+
end)
9+
10+
it('second day two turtle doves', function()
11+
local expected = {
12+
'On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.'
13+
}
14+
assert.are.same(expected, twelve_days.recite(2, 2))
15+
end)
16+
17+
it('third day three french hens', function()
18+
local expected = {
19+
'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.'
20+
}
21+
assert.are.same(expected, twelve_days.recite(3, 3))
22+
end)
23+
24+
it('fourth day four calling birds', function()
25+
local expected = {
26+
'On the fourth 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.'
27+
}
28+
assert.are.same(expected, twelve_days.recite(4, 4))
29+
end)
30+
31+
it('fifth day five gold rings', function()
32+
local expected = {
33+
'On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
34+
}
35+
assert.are.same(expected, twelve_days.recite(5, 5))
36+
end)
37+
38+
it('sixth day six geese-a-laying', function()
39+
local expected = {
40+
'On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
41+
}
42+
assert.are.same(expected, twelve_days.recite(6, 6))
43+
end)
44+
45+
it('seventh day seven swans-a-swimming', function()
46+
local expected = {
47+
'On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
48+
}
49+
assert.are.same(expected, twelve_days.recite(7, 7))
50+
end)
51+
52+
it('eighth day eight maids-a-milking', function()
53+
local expected = {
54+
'On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
55+
}
56+
assert.are.same(expected, twelve_days.recite(8, 8))
57+
end)
58+
59+
it('ninth day nine ladies dancing', function()
60+
local expected = {
61+
'On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
62+
}
63+
assert.are.same(expected, twelve_days.recite(9, 9))
64+
end)
65+
66+
it('tenth day ten lords-a-leaping', function()
67+
local expected = {
68+
'On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
69+
}
70+
assert.are.same(expected, twelve_days.recite(10, 10))
71+
end)
72+
73+
it('eleventh day eleven pipers piping', function()
74+
local expected = {
75+
'On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
76+
}
77+
assert.are.same(expected, twelve_days.recite(11, 11))
78+
end)
79+
80+
it('twelfth day twelve drummers drumming', function()
81+
local expected = {
82+
'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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
83+
}
84+
assert.are.same(expected, twelve_days.recite(12, 12))
85+
end)
86+
end)
87+
88+
describe('lyrics', function()
89+
it('recites first three verses of the song', function()
90+
local expected = {
91+
'On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.',
92+
'On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.',
93+
'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.'
94+
}
95+
assert.are.same(expected, twelve_days.recite(1, 3))
96+
end)
97+
98+
it('recites three verses from the middle of the song', function()
99+
local expected = {
100+
'On the fourth 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.',
101+
'On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
102+
'On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
103+
}
104+
assert.are.same(expected, twelve_days.recite(4, 6))
105+
end)
106+
107+
it('recites the whole song', function()
108+
local expected = {
109+
'On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.',
110+
'On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.',
111+
'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.',
112+
'On the fourth 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.',
113+
'On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
114+
'On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
115+
'On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
116+
'On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
117+
'On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
118+
'On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
119+
'On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.',
120+
'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 Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.'
121+
}
122+
assert.are.same(expected, twelve_days.recite(1, 12))
123+
end)
124+
end)
125+
end)

0 commit comments

Comments
 (0)