Skip to content

Commit fd17e9e

Browse files
Add twelve-days exercise (#240)
1 parent 95cfaf3 commit fd17e9e

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@
399399
"prerequisites": [],
400400
"difficulty": 3
401401
},
402+
{
403+
"slug": "twelve-days",
404+
"name": "Twelve Days",
405+
"uuid": "6c366848-a77a-478a-8925-7a8947a14260",
406+
"practices": [],
407+
"prerequisites": [],
408+
"difficulty": 3
409+
},
402410
{
403411
"slug": "affine-cipher",
404412
"name": "Affine Cipher",
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.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module main
2+
3+
import strings
4+
5+
const gifts = '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.\n'
6+
7+
const ordinals = ['', 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth',
8+
'ninth', 'tenth', 'eleventh', 'twelfth']
9+
10+
const table = [0, 235, 213, 194, 174, 157, 137, 113, 90, 69, 48, 26, 0]
11+
12+
fn recite(start_verse int, end_verse int) string {
13+
mut builder := strings.new_builder(4000)
14+
for verse in start_verse .. (end_verse + 1) {
15+
builder.write_string('On the ')
16+
builder.write_string(ordinals[verse])
17+
builder.write_string(' day of Christmas my true love gave to me: ')
18+
builder.write_string(gifts[table[verse]..])
19+
}
20+
builder.go_back(1) // Omit final newline
21+
return builder.str()
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[c0b5a5e6-c89d-49b1-a6b2-9f523bff33f7]
6+
description = "verse -> first day a partridge in a pear tree"
7+
8+
[1c64508a-df3d-420a-b8e1-fe408847854a]
9+
description = "verse -> second day two turtle doves"
10+
11+
[a919e09c-75b2-4e64-bb23-de4a692060a8]
12+
description = "verse -> third day three french hens"
13+
14+
[9bed8631-ec60-4894-a3bb-4f0ec9fbe68d]
15+
description = "verse -> fourth day four calling birds"
16+
17+
[cf1024f0-73b6-4545-be57-e9cea565289a]
18+
description = "verse -> fifth day five gold rings"
19+
20+
[50bd3393-868a-4f24-a618-68df3d02ff04]
21+
description = "verse -> sixth day six geese-a-laying"
22+
23+
[8f29638c-9bf1-4680-94be-e8b84e4ade83]
24+
description = "verse -> seventh day seven swans-a-swimming"
25+
26+
[7038d6e1-e377-47ad-8c37-10670a05bc05]
27+
description = "verse -> eighth day eight maids-a-milking"
28+
29+
[37a800a6-7a56-4352-8d72-0f51eb37cfe8]
30+
description = "verse -> ninth day nine ladies dancing"
31+
32+
[10b158aa-49ff-4b2d-afc3-13af9133510d]
33+
description = "verse -> tenth day ten lords-a-leaping"
34+
35+
[08d7d453-f2ba-478d-8df0-d39ea6a4f457]
36+
description = "verse -> eleventh day eleven pipers piping"
37+
38+
[0620fea7-1704-4e48-b557-c05bf43967f0]
39+
description = "verse -> twelfth day twelve drummers drumming"
40+
41+
[da8b9013-b1e8-49df-b6ef-ddec0219e398]
42+
description = "lyrics -> recites first three verses of the song"
43+
44+
[c095af0d-3137-4653-ad32-bfb899eda24c]
45+
description = "lyrics -> recites three verses from the middle of the song"
46+
47+
[20921bc9-cc52-4627-80b3-198cbbfcf9b7]
48+
description = "lyrics -> recites the whole song"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
module main
2+
3+
fn test_verse__first_day_a_partridge_in_a_pear_tree() {
4+
expected := 'On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.'
5+
assert recite(1, 1) == expected
6+
}
7+
8+
fn test_verse__second_day_two_turtle_doves() {
9+
expected := 'On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.'
10+
assert recite(2, 2) == expected
11+
}
12+
13+
fn test_verse__third_day_three_french_hens() {
14+
expected := '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.'
15+
assert recite(3, 3) == expected
16+
}
17+
18+
fn test_verse__fourth_day_four_calling_birds() {
19+
expected := '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+
assert recite(4, 4) == expected
21+
}
22+
23+
fn test_verse__fifth_day_five_gold_rings() {
24+
expected := '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.'
25+
assert recite(5, 5) == expected
26+
}
27+
28+
fn test_verse__sixth_day_six_geese_a_laying() {
29+
expected := '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.'
30+
assert recite(6, 6) == expected
31+
}
32+
33+
fn test_verse__seventh_day_seven_swans_a_swimming() {
34+
expected := '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.'
35+
assert recite(7, 7) == expected
36+
}
37+
38+
fn test_verse__eighth_day_eight_maids_a_milking() {
39+
expected := '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.'
40+
assert recite(8, 8) == expected
41+
}
42+
43+
fn test_verse__ninth_day_nine_ladies_dancing() {
44+
expected := '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.'
45+
assert recite(9, 9) == expected
46+
}
47+
48+
fn test_verse__tenth_day_ten_lords_a_leaping() {
49+
expected := '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.'
50+
assert recite(10, 10) == expected
51+
}
52+
53+
fn test_verse__eleventh_day_eleven_pipers_piping() {
54+
expected := '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.'
55+
assert recite(11, 11) == expected
56+
}
57+
58+
fn test_verse__twelfth_day_twelve_drummers_drumming() {
59+
expected := '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.'
60+
assert recite(12, 12) == expected
61+
}
62+
63+
fn test_lyrics__recites_first_three_verses_of_the_song() {
64+
expected := ('On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.
65+
On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.
66+
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.')
67+
assert recite(1, 3) == expected
68+
}
69+
70+
fn test_lyrics__recites_three_verses_from_the_middle_of_the_song() {
71+
expected := ('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.
72+
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.
73+
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.')
74+
assert recite(4, 6) == expected
75+
}
76+
77+
fn test_lyrics__recites_the_whole_song() {
78+
expected := ('On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.
79+
On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.
80+
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.
81+
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.
82+
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.
83+
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.
84+
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.
85+
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.
86+
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.
87+
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.
88+
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.
89+
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.')
90+
assert recite(1, 12) == expected
91+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn recite(start_verse int, end_verse int) string {
4+
}

0 commit comments

Comments
 (0)