Skip to content

Commit

Permalink
Add twelve-days exercise (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jan 30, 2025
1 parent 0e412d6 commit 72c5ed9
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 1 deletion.
10 changes: 9 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@
],
"difficulty": 1
},
{
"slug": "twelve-days",
"name": "Twelve Days",
"uuid": "1ae61ecf-2ce4-49bb-9e3f-8b8b7125b176",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "eliuds-eggs",
"name": "Eliuds Eggs",
"name": "Eliud's Eggs",
"uuid": "493be46b-6cb3-4591-87db-b0a17e9238ee",
"practices": [],
"prerequisites": [],
Expand Down
36 changes: 36 additions & 0 deletions exercises/practice/twelve-days/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Your task in this exercise is to write code that returns the lyrics of the song: "The Twelve Days of Christmas."

"The Twelve Days of Christmas" is a common English Christmas carol.
Each subsequent verse of the song builds on the previous verse.

The lyrics your code returns should _exactly_ match the full song text shown below.

## Lyrics

```text
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 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.
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.
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.
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.
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.
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.
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.
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.
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.
```
19 changes: 19 additions & 0 deletions exercises/practice/twelve-days/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"twelve_days.zig"
],
"test": [
"test_twelve_days.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Output the lyrics to 'The Twelve Days of Christmas'.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)"
}
37 changes: 37 additions & 0 deletions exercises/practice/twelve-days/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");

const on_the = "On the ";

const day_of = " day of Christmas my true love gave to me: ";

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.";

const ordinals = [_][]const u8{ "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" };

const table = [_]usize{ 0, 235, 213, 194, 174, 157, 137, 113, 90, 69, 48, 26, 0 };

fn appendString(buffer: []u8, offset: *usize, str: []const u8) void {
@memcpy(buffer[offset.*..(offset.* + str.len)], str);
offset.* += str.len;
}

pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) []const u8 {
var offset: usize = 0;

for (start_verse..end_verse+1) |verse| {
if (verse != start_verse) {
buffer[offset] = '\n';
offset += 1;
}

appendString(buffer, &offset, on_the);

appendString(buffer, &offset, ordinals[verse]);

appendString(buffer, &offset, day_of);

appendString(buffer, &offset, gifts[table[verse]..]);
}

return buffer[0..offset];
}
55 changes: 55 additions & 0 deletions exercises/practice/twelve-days/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c0b5a5e6-c89d-49b1-a6b2-9f523bff33f7]
description = "verse -> first day a partridge in a pear tree"

[1c64508a-df3d-420a-b8e1-fe408847854a]
description = "verse -> second day two turtle doves"

[a919e09c-75b2-4e64-bb23-de4a692060a8]
description = "verse -> third day three french hens"

[9bed8631-ec60-4894-a3bb-4f0ec9fbe68d]
description = "verse -> fourth day four calling birds"

[cf1024f0-73b6-4545-be57-e9cea565289a]
description = "verse -> fifth day five gold rings"

[50bd3393-868a-4f24-a618-68df3d02ff04]
description = "verse -> sixth day six geese-a-laying"

[8f29638c-9bf1-4680-94be-e8b84e4ade83]
description = "verse -> seventh day seven swans-a-swimming"

[7038d6e1-e377-47ad-8c37-10670a05bc05]
description = "verse -> eighth day eight maids-a-milking"

[37a800a6-7a56-4352-8d72-0f51eb37cfe8]
description = "verse -> ninth day nine ladies dancing"

[10b158aa-49ff-4b2d-afc3-13af9133510d]
description = "verse -> tenth day ten lords-a-leaping"

[08d7d453-f2ba-478d-8df0-d39ea6a4f457]
description = "verse -> eleventh day eleven pipers piping"

[0620fea7-1704-4e48-b557-c05bf43967f0]
description = "verse -> twelfth day twelve drummers drumming"

[da8b9013-b1e8-49df-b6ef-ddec0219e398]
description = "lyrics -> recites first three verses of the song"

[c095af0d-3137-4653-ad32-bfb899eda24c]
description = "lyrics -> recites three verses from the middle of the song"

[20921bc9-cc52-4627-80b3-198cbbfcf9b7]
description = "lyrics -> recites the whole song"
169 changes: 169 additions & 0 deletions exercises/practice/twelve-days/test_twelve_days.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
const std = @import("std");
const testing = std.testing;

const twelve_days = @import("twelve_days.zig");

test "verse-first day a partridge in a pear tree" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.
;
const actual = twelve_days.recite(&buffer, 1, 1);
try testing.expectEqualStrings(expected, actual);
}

test "verse-second day two turtle doves" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.
;
const actual = twelve_days.recite(&buffer, 2, 2);
try testing.expectEqualStrings(expected, actual);
}

test "verse-third day three french hens" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 3, 3);
try testing.expectEqualStrings(expected, actual);
}

test "verse-fourth day four calling birds" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 4, 4);
try testing.expectEqualStrings(expected, actual);
}

test "verse-fifth day five gold rings" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 5, 5);
try testing.expectEqualStrings(expected, actual);
}

test "verse-sixth day six geese-a-laying" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 6, 6);
try testing.expectEqualStrings(expected, actual);
}

test "verse-seventh day seven swans-a-swimming" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 7, 7);
try testing.expectEqualStrings(expected, actual);
}

test "verse-eighth day eight maids-a-milking" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 8, 8);
try testing.expectEqualStrings(expected, actual);
}

test "verse-ninth day nine ladies dancing" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 9, 9);
try testing.expectEqualStrings(expected, actual);
}

test "verse-tenth day ten lords-a-leaping" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 10, 10);
try testing.expectEqualStrings(expected, actual);
}

test "verse-eleventh day eleven pipers piping" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 11, 11);
try testing.expectEqualStrings(expected, actual);
}

test "verse-twelfth day twelve drummers drumming" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 12, 12);
try testing.expectEqualStrings(expected, actual);
}

test "lyrics-recites first three verses of the song" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
;
const actual = twelve_days.recite(&buffer, 1, 3);
try testing.expectEqualStrings(expected, actual);
}

test "lyrics-recites three verses from the middle of the song" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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.
\\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.
\\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.
;
const actual = twelve_days.recite(&buffer, 4, 6);
try testing.expectEqualStrings(expected, actual);
}

test "lyrics-recites the whole song" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\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 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.
\\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.
\\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.
\\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.
\\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.
\\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.
\\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.
\\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.
\\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.
;
const actual = twelve_days.recite(&buffer, 1, 12);
try testing.expectEqualStrings(expected, actual);
}
6 changes: 6 additions & 0 deletions exercises/practice/twelve-days/twelve_days.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn recite(buffer: []u8, start_verse: u32, end_verse: u32) []const u8 {
_ = buffer;
_ = start_verse;
_ = end_verse;
@compileError("please implement the recite function");
}

0 comments on commit 72c5ed9

Please sign in to comment.