Skip to content

Commit 3574291

Browse files
Add twelve-days exercise (#302)
1 parent cd4f288 commit 3574291

File tree

7 files changed

+275
-0
lines changed

7 files changed

+275
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,14 @@
518518
"string_manipulation"
519519
]
520520
},
521+
{
522+
"slug": "twelve-days",
523+
"name": "Twelve Days",
524+
"uuid": "f9ca770d-1dcd-4ab5-8cbb-80cd007d0587",
525+
"practices": [],
526+
"prerequisites": [],
527+
"difficulty": 3
528+
},
521529
{
522530
"slug": "yacht",
523531
"name": "Yacht",
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+
"source/twelve_days.d"
8+
],
9+
"test": [
10+
"source/twelve_days.d"
11+
],
12+
"example": [
13+
"example/twelve_days.d"
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: 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "twelve-days"
2+
buildRequirements "disallowDeprecations"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module twelve_days;
2+
3+
import std.array;
4+
5+
pure string recite(int startVerse, int endVerse)
6+
{
7+
immutable string[] ordinals = [ "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" ];
8+
9+
immutable string gifts =
10+
"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.";
11+
12+
immutable size_t[] table = [ 0, 235, 213, 194, 174, 157, 137, 113, 90, 69, 48, 26, 0 ];
13+
14+
string[] lyrics;
15+
for (int verse = startVerse; verse <= endVerse; ++verse)
16+
{
17+
lyrics ~= "On the " ~ ordinals[verse] ~ " day of Christmas my true love gave to me: " ~ gifts[table[verse] .. gifts.length];
18+
}
19+
20+
return lyrics.join("\n");
21+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
module twelve_days;
2+
3+
pure string recite(int startVerse, int endVerse)
4+
{
5+
// implement this function
6+
}
7+
8+
unittest
9+
{
10+
immutable int allTestsEnabled = 0;
11+
12+
// Verse-first day a partridge in a pear tree
13+
{
14+
string expected =
15+
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.";
16+
assert(recite(1, 1) == expected);
17+
}
18+
19+
static if (allTestsEnabled)
20+
{
21+
// Verse-second day two turtle doves
22+
{
23+
string expected =
24+
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.";
25+
assert(recite(2, 2) == expected);
26+
}
27+
28+
// Verse-third day three french hens
29+
{
30+
string expected =
31+
"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.";
32+
assert(recite(3, 3) == expected);
33+
}
34+
35+
// Verse-fourth day four calling birds
36+
{
37+
string expected =
38+
"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.";
39+
assert(recite(4, 4) == expected);
40+
}
41+
42+
// Verse-fifth day five gold rings
43+
{
44+
string expected =
45+
"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.";
46+
assert(recite(5, 5) == expected);
47+
}
48+
49+
// Verse-sixth day six geese-a-laying
50+
{
51+
string expected =
52+
"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.";
53+
assert(recite(6, 6) == expected);
54+
}
55+
56+
// Verse-seventh day seven swans-a-swimming
57+
{
58+
string expected =
59+
"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.";
60+
assert(recite(7, 7) == expected);
61+
}
62+
63+
// Verse-eighth day eight maids-a-milking
64+
{
65+
string expected =
66+
"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.";
67+
assert(recite(8, 8) == expected);
68+
}
69+
70+
// Verse-ninth day nine ladies dancing
71+
{
72+
string expected =
73+
"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.";
74+
assert(recite(9, 9) == expected);
75+
}
76+
77+
// Verse-tenth day ten lords-a-leaping
78+
{
79+
string expected =
80+
"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.";
81+
assert(recite(10, 10) == expected);
82+
}
83+
84+
// Verse-eleventh day eleven pipers piping
85+
{
86+
string expected =
87+
"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.";
88+
assert(recite(11, 11) == expected);
89+
}
90+
91+
// Verse-twelfth day twelve drummers drumming
92+
{
93+
string expected =
94+
"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.";
95+
assert(recite(12, 12) == expected);
96+
}
97+
98+
// Lyrics-recites first three verses of the song
99+
{
100+
string expected =
101+
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.\n" ~
102+
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.\n" ~
103+
"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.";
104+
assert(recite(1, 3) == expected);
105+
}
106+
107+
// Lyrics-recites three verses from the middle of the song
108+
{
109+
string expected =
110+
"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.\n" ~
111+
"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.\n" ~
112+
"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.";
113+
assert(recite(4, 6) == expected);
114+
}
115+
116+
// Lyrics-recites the whole song
117+
{
118+
string expected =
119+
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.\n" ~
120+
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.\n" ~
121+
"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.\n" ~
122+
"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.\n" ~
123+
"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.\n" ~
124+
"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.\n" ~
125+
"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.\n" ~
126+
"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.\n" ~
127+
"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.\n" ~
128+
"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.\n" ~
129+
"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.\n" ~
130+
"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.";
131+
assert(recite(1, 12) == expected);
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)