Skip to content

Commit

Permalink
Add bottle-song exercise (resolves #401) (#406)
Browse files Browse the repository at this point in the history
* Add bottle-song exercise (resolves #401)

* Address BottleSong review feedback

* BottleSong take_down is not optional
  • Loading branch information
keiravillekode authored Nov 11, 2023
1 parent 1d79f6b commit be3bf4e
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,20 @@
"text_formatting"
]
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "64a74b80-b0e6-46f7-b6fd-c0b613ef14f4",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": [
"control_flow_if_else_statements",
"control_flow_loops",
"strings",
"text_formatting"
]
},
{
"slug": "etl",
"name": "ETL",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/bottle-song/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
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.lua"
],
"test": [
"bottle-song_spec.lua"
],
"example": [
".meta/example.lua"
]
},
"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"
}
51 changes: 51 additions & 0 deletions exercises/practice/bottle-song/.meta/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local BottleSong = {}

local GENERIC_VERSE = table.concat({
"N green bottles hanging on the wall,\n",
"N green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be M green bottles hanging on the wall.\n"
})

local words = {
[0] = 'no',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten'
}

local function title_case(word)
return word:sub(1,1):upper() .. word:sub(2)
end

local function verse(number)
local a_verse = GENERIC_VERSE:gsub('N', title_case(words[number])):gsub('M', words[number - 1])
if number < 3 then
a_verse = a_verse:gsub('One green bottles', 'One green bottle')
a_verse = a_verse:gsub('one green bottles', 'one green bottle')
end
return a_verse
end

--- Produces the lyrics to the song "Ten Green Bottles"
-- @param start_bottles The initial number of bottles on the wall
-- @param[opt=1] take_down The number of bottles that fall
function BottleSong.recite(start_bottles, take_down)
finish_bottles = start_bottles + 1 - take_down

local verses = ''
for i = start_bottles, finish_bottles, -1 do
verses = verses .. verse(i)
if i ~= finish_bottles then verses = verses .. '\n' end
end
return verses
end

return BottleSong
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"
7 changes: 7 additions & 0 deletions exercises/practice/bottle-song/bottle-song.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local BottleSong = {}

function BottleSong.recite(start_bottles, take_down)

end

return BottleSong
137 changes: 137 additions & 0 deletions exercises/practice/bottle-song/bottle-song_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
local BottleSong = require 'bottle-song'

describe('lyrics', function()
describe('single verse', function()
it('first generic verse', function()
expected = table.concat({
"Ten green bottles hanging on the wall,\n",
"Ten green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be nine green bottles hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(10, 1))
end)

it('last generic verse', function()
expected = table.concat({
"Three green bottles hanging on the wall,\n",
"Three green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be two green bottles hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(3, 1))
end)

it('verse with 2 bottles', function()
expected = table.concat({
"Two green bottles hanging on the wall,\n",
"Two green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be one green bottle hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(2, 1))
end)

it('verse with 1 bottle', function()
expected = table.concat({
"One green bottle hanging on the wall,\n",
"One green bottle hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be no green bottles hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(1, 1))
end)
end)

describe('multiple verses', function()
it('first two verses', function()
expected = table.concat({
"Ten green bottles hanging on the wall,\n",
"Ten green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be nine green bottles hanging on the wall.\n",
"\n",
"Nine green bottles hanging on the wall,\n",
"Nine green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be eight green bottles hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(10, 2))
end)

it('last three verses', function()
expected = table.concat({
"Three green bottles hanging on the wall,\n",
"Three green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be two green bottles hanging on the wall.\n",
"\n",
"Two green bottles hanging on the wall,\n",
"Two green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be one green bottle hanging on the wall.\n",
"\n",
"One green bottle hanging on the wall,\n",
"One green bottle hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be no green bottles hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(3, 3))
end)

it('all verses', function()
expected = table.concat({
"Ten green bottles hanging on the wall,\n",
"Ten green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be nine green bottles hanging on the wall.\n",
"\n",
"Nine green bottles hanging on the wall,\n",
"Nine green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be eight green bottles hanging on the wall.\n",
"\n",
"Eight green bottles hanging on the wall,\n",
"Eight green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be seven green bottles hanging on the wall.\n",
"\n",
"Seven green bottles hanging on the wall,\n",
"Seven green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be six green bottles hanging on the wall.\n",
"\n",
"Six green bottles hanging on the wall,\n",
"Six green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be five green bottles hanging on the wall.\n",
"\n",
"Five green bottles hanging on the wall,\n",
"Five green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be four green bottles hanging on the wall.\n",
"\n",
"Four green bottles hanging on the wall,\n",
"Four green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be three green bottles hanging on the wall.\n",
"\n",
"Three green bottles hanging on the wall,\n",
"Three green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be two green bottles hanging on the wall.\n",
"\n",
"Two green bottles hanging on the wall,\n",
"Two green bottles hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be one green bottle hanging on the wall.\n",
"\n",
"One green bottle hanging on the wall,\n",
"One green bottle hanging on the wall,\n",
"And if one green bottle should accidentally fall,\n",
"There'll be no green bottles hanging on the wall.\n",
})
assert.equal(expected, BottleSong.recite(10, 10))
end)
end)
end)

0 comments on commit be3bf4e

Please sign in to comment.