Skip to content

Commit

Permalink
Add bottle-song (#192)
Browse files Browse the repository at this point in the history
* Add `bottle-song`

* EOL

Co-authored-by: Victor Goff <[email protected]>

---------

Co-authored-by: Victor Goff <[email protected]>
  • Loading branch information
BNAndras and kotp authored Jan 6, 2025
1 parent f13a3a9 commit 5360ce2
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "bottle-song",
"name": "Bottle Song",
"uuid": "09ec95a8-1e28-4139-a2de-92436ef91b37",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "darts",
"name": "Darts",
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": [
"BNAndras"
],
"files": {
"solution": [
"bottle-song.red"
],
"test": [
"bottle-song-test.red"
],
"example": [
".meta/example.red"
]
},
"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"
}
71 changes: 71 additions & 0 deletions exercises/practice/bottle-song/.meta/example.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Red [
description: {"Bottle Song" exercise solution for exercism platform}
author: "BNAndras"
]

recite: function [
startBottles
takeDown
] [
verses: []
stop: startBottles - takeDown
i: startBottles
while [i > stop] [
next: i - 1
either i > 1 [
append verses rejoin [
uppercase/part number-to-word i 1 " green bottles hanging on the wall,"
newline
uppercase/part number-to-word i 1 " green bottles hanging on the wall,"
newline
"And if one green bottle should accidentally fall,"
newline
"There'll be " number-to-word next " green " pluralize next " hanging on the wall."
]
] [
append verses rejoin [
"One green bottle hanging on the wall,"
newline
"One green bottle hanging on the wall,"
newline
"And if one green bottle should accidentally fall,"
newline
"There'll be no green bottles hanging on the wall."
]
]
i: next
]

results: ""
repeat index (length? verses) [
append results verses/:index
if index < length? verses [
append results newline
append results newline
]
]
results
]

number-to-word: function [
number
] [
either number = 0 [
"no"
] [
words: [
"one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten"
]
words/(number)
]
]

pluralize: function [
count
] [
either count = 1 [
"bottle"
] [
"bottles"
]
]
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"
175 changes: 175 additions & 0 deletions exercises/practice/bottle-song/bottle-song-test.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
Red [
description: {Tests for "Bottle Song" Exercism exercise}
author: "loziniak"
]

#include %testlib.red

test-init/limit %bottle-song.red 1
; test-init/limit %.meta/example.red 1 ; test example solution

canonical-cases: [#[
description: "first generic verse"
input: #[
startBottles: 10
takeDown: 1
]
expected:
{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.}
function: "recite"
uuid: "d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03"
] #[
description: "last generic verse"
input: #[
startBottles: 3
takeDown: 1
]
expected:
{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.}
function: "recite"
uuid: "0f0aded3-472a-4c64-b842-18d4f1f5f030"
] #[
description: "verse with 2 bottles"
input: #[
startBottles: 2
takeDown: 1
]
expected:
{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.}
function: "recite"
uuid: "f61f3c97-131f-459e-b40a-7428f3ed99d9"
] #[
description: "verse with 1 bottle"
input: #[
startBottles: 1
takeDown: 1
]
expected:
{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.}
function: "recite"
uuid: "05eadba9-5dbd-401e-a7e8-d17cc9baa8e0"
] #[
description: "first two verses"
input: #[
startBottles: 10
takeDown: 2
]
expected:
{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.}
function: "recite"
uuid: "a4a28170-83d6-4dc1-bd8b-319b6abb6a80"
] #[
description: "last three verses"
input: #[
startBottles: 3
takeDown: 3
]
expected:
{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.}
function: "recite"
uuid: "3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db"
] #[
description: "all verses"
input: #[
startBottles: 10
takeDown: 10
]
expected:
{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.}
function: "recite"
uuid: "28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28"
]]


foreach c-case canonical-cases [
case-code: reduce [
'expect c-case/expected compose [
(to word! c-case/function) (values-of c-case/input)
]
]

test c-case/description case-code
]

test-results/print
12 changes: 12 additions & 0 deletions exercises/practice/bottle-song/bottle-song.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Red [
description: {"Bottle Song" exercise solution for exercism platform}
author: "" ; you can write your name here, in quotes
]

recite: function [
startBottles
takeDown
] [
cause-error 'user 'message "You need to implement recite function."
]

Loading

0 comments on commit 5360ce2

Please sign in to comment.