Skip to content

Commit

Permalink
Add bottle-song exercise (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jan 30, 2025
1 parent 97fa7ee commit 0e412d6
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
],
"difficulty": 1
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "26b6fdd5-6e9d-4a5a-87d3-e9de104b6f3d",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "square-root",
"name": "Square Root",
Expand Down
57 changes: 57 additions & 0 deletions exercises/practice/bottle-song/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Instructions

Recite the lyrics to that popular children's repetitive song: Ten Green Bottles.

Note that not all verses are identical.

```text
Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be nine green bottles hanging on the wall.
Nine green bottles hanging on the wall,
Nine green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be eight green bottles hanging on the wall.
Eight green bottles hanging on the wall,
Eight green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be seven green bottles hanging on the wall.
Seven green bottles hanging on the wall,
Seven green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be six green bottles hanging on the wall.
Six green bottles hanging on the wall,
Six green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be five green bottles hanging on the wall.
Five green bottles hanging on the wall,
Five green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be four green bottles hanging on the wall.
Four green bottles hanging on the wall,
Four green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be three green bottles hanging on the wall.
Three green bottles hanging on the wall,
Three green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be two green bottles hanging on the wall.
Two green bottles hanging on the wall,
Two green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There'll be one green bottle hanging on the wall.
One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There'll be no green bottles hanging on the wall.
```
19 changes: 19 additions & 0 deletions exercises/practice/bottle-song/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"bottle_song.zig"
],
"test": [
"test_bottle_song.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles"
}
60 changes: 60 additions & 0 deletions exercises/practice/bottle-song/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const std = @import("std");

const green_bottle = " green bottle";

const hanging_on = " hanging on the wall";

const and_if = "And if one green bottle should accidentally fall,\nThere'll be ";

const comma = ",\n";

const stop = ".";

const numbers = [_][]const u8{ "No", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };

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

fn appendLine(buffer: []u8, offset: *usize, num_bottles: u32) void {
appendString(buffer, offset, numbers[num_bottles]);

appendString(buffer, offset, green_bottle);

if (num_bottles != 1) {
buffer[offset.*] = 's';
offset.* += 1;
}

appendString(buffer, offset, hanging_on);
}

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

var num_bottles = start_bottles;
while (num_bottles > start_bottles - take_down) {
if (num_bottles != start_bottles) {
buffer[offset] = '\n';
buffer[offset + 1] = '\n';
offset += 2;
}

appendLine(buffer, &offset, num_bottles);
appendString(buffer, &offset, comma);

appendLine(buffer, &offset, num_bottles);
appendString(buffer, &offset, comma);

appendString(buffer, &offset, and_if);
num_bottles -= 1;

const savedOffset = offset;
appendLine(buffer, &offset, num_bottles);
buffer[savedOffset] = buffer[savedOffset] | 32;
appendString(buffer, &offset, stop);
}

return buffer[0..offset];
}
31 changes: 31 additions & 0 deletions exercises/practice/bottle-song/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

[d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03]
description = "verse -> single verse -> first generic verse"

[0f0aded3-472a-4c64-b842-18d4f1f5f030]
description = "verse -> single verse -> last generic verse"

[f61f3c97-131f-459e-b40a-7428f3ed99d9]
description = "verse -> single verse -> verse with 2 bottles"

[05eadba9-5dbd-401e-a7e8-d17cc9baa8e0]
description = "verse -> single verse -> verse with 1 bottle"

[a4a28170-83d6-4dc1-bd8b-319b6abb6a80]
description = "lyrics -> multiple verses -> first two verses"

[3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db]
description = "lyrics -> multiple verses -> last three verses"

[28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28]
description = "lyrics -> multiple verses -> all verses"
6 changes: 6 additions & 0 deletions exercises/practice/bottle-song/bottle_song.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn recite(buffer: []u8, start_bottles: u32, take_down: u32) []const u8 {
_ = buffer;
_ = start_bottles;
_ = take_down;
@compileError("please implement the recite function");
}
155 changes: 155 additions & 0 deletions exercises/practice/bottle-song/test_bottle_song.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const std = @import("std");
const testing = std.testing;

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

test "verse-single verse-first generic verse" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\Ten green bottles hanging on the wall,
\\Ten green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be nine green bottles hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 10, 1);
try testing.expectEqualStrings(expected, actual);
}

test "verse-single verse-last generic verse" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\Three green bottles hanging on the wall,
\\Three green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be two green bottles hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 3, 1);
try testing.expectEqualStrings(expected, actual);
}

test "verse-single verse-verse with 2 bottles" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\Two green bottles hanging on the wall,
\\Two green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be one green bottle hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 2, 1);
try testing.expectEqualStrings(expected, actual);
}

test "verse-single verse-verse with 1 bottle" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\One green bottle hanging on the wall,
\\One green bottle hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be no green bottles hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 1, 1);
try testing.expectEqualStrings(expected, actual);
}

test "lyrics-multiple verses-first two verses" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\Ten green bottles hanging on the wall,
\\Ten green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be nine green bottles hanging on the wall.
\\
\\Nine green bottles hanging on the wall,
\\Nine green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be eight green bottles hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 10, 2);
try testing.expectEqualStrings(expected, actual);
}

test "lyrics-multiple verses-last three verses" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\Three green bottles hanging on the wall,
\\Three green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be two green bottles hanging on the wall.
\\
\\Two green bottles hanging on the wall,
\\Two green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be one green bottle hanging on the wall.
\\
\\One green bottle hanging on the wall,
\\One green bottle hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be no green bottles hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 3, 3);
try testing.expectEqualStrings(expected, actual);
}

test "lyrics-multiple verses-all verses" {
const buffer_size = 4000;
var buffer: [buffer_size]u8 = undefined;
const expected: []const u8 =
\\Ten green bottles hanging on the wall,
\\Ten green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be nine green bottles hanging on the wall.
\\
\\Nine green bottles hanging on the wall,
\\Nine green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be eight green bottles hanging on the wall.
\\
\\Eight green bottles hanging on the wall,
\\Eight green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be seven green bottles hanging on the wall.
\\
\\Seven green bottles hanging on the wall,
\\Seven green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be six green bottles hanging on the wall.
\\
\\Six green bottles hanging on the wall,
\\Six green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be five green bottles hanging on the wall.
\\
\\Five green bottles hanging on the wall,
\\Five green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be four green bottles hanging on the wall.
\\
\\Four green bottles hanging on the wall,
\\Four green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be three green bottles hanging on the wall.
\\
\\Three green bottles hanging on the wall,
\\Three green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be two green bottles hanging on the wall.
\\
\\Two green bottles hanging on the wall,
\\Two green bottles hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be one green bottle hanging on the wall.
\\
\\One green bottle hanging on the wall,
\\One green bottle hanging on the wall,
\\And if one green bottle should accidentally fall,
\\There'll be no green bottles hanging on the wall.
;
const actual = bottle_song.recite(&buffer, 10, 10);
try testing.expectEqualStrings(expected, actual);
}

0 comments on commit 0e412d6

Please sign in to comment.