Add bottle-song exercise (#225)
This commit is contained in:
@@ -477,6 +477,14 @@
|
||||
"prerequisites": [],
|
||||
"difficulty": 4
|
||||
},
|
||||
{
|
||||
"slug": "bottle-song",
|
||||
"name": "Bottle Song",
|
||||
"uuid": "7bb10ccd-4ccd-4bb8-bf92-c4294623a173",
|
||||
"practices": [],
|
||||
"prerequisites": [],
|
||||
"difficulty": 4
|
||||
},
|
||||
{
|
||||
"slug": "binary-search",
|
||||
"name": "Binary Search",
|
||||
|
||||
57
exercises/practice/bottle-song/.docs/instructions.md
Normal file
57
exercises/practice/bottle-song/.docs/instructions.md
Normal file
@@ -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
exercises/practice/bottle-song/.meta/config.json
Normal file
19
exercises/practice/bottle-song/.meta/config.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"authors": [
|
||||
"erikschierboom"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"bottle-song.8th"
|
||||
],
|
||||
"test": [
|
||||
"test.8th"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.8th"
|
||||
]
|
||||
},
|
||||
"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"
|
||||
}
|
||||
22
exercises/practice/bottle-song/.meta/example.8th
Normal file
22
exercises/practice/bottle-song/.meta/example.8th
Normal file
@@ -0,0 +1,22 @@
|
||||
["no", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] constant counts
|
||||
|
||||
: capitalize \ s -- s
|
||||
0 s:@ s:uc 0 swap s:!
|
||||
;
|
||||
|
||||
: bottles \ n -- s
|
||||
dup 1 n:= if "" else "s" then swap counts swap a:_@ "%s green bottle%s" s:strfmt
|
||||
;
|
||||
|
||||
: verse \ n -- s
|
||||
dup
|
||||
n:1- bottles swap
|
||||
bottles capitalize
|
||||
1 bottles swap dup
|
||||
"%s hanging on the wall,\n%s hanging on the wall,\nAnd if %s should accidentally fall,\nThere'll be %s hanging on the wall."
|
||||
s:strfmt
|
||||
;
|
||||
|
||||
: recite \ n n -- s
|
||||
2dup n:1- n:- -rot >r ' verse -rot loop- r> a:close "\n\n" a:join
|
||||
;
|
||||
31
exercises/practice/bottle-song/.meta/tests.toml
Normal file
31
exercises/practice/bottle-song/.meta/tests.toml
Normal file
@@ -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"
|
||||
3
exercises/practice/bottle-song/bottle-song.8th
Normal file
3
exercises/practice/bottle-song/bottle-song.8th
Normal file
@@ -0,0 +1,3 @@
|
||||
: recite \ n n -- s
|
||||
|
||||
;
|
||||
182
exercises/practice/bottle-song/libs/exercism/test
Normal file
182
exercises/practice/bottle-song/libs/exercism/test
Normal file
@@ -0,0 +1,182 @@
|
||||
needs console/loaded
|
||||
needs utils/latebind
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
ns: test
|
||||
|
||||
-1 var, test-count
|
||||
var tests-passed
|
||||
var tests-failed
|
||||
var tests-skipped
|
||||
true var, run-test
|
||||
|
||||
\ Some utility words
|
||||
|
||||
|
||||
: test-passed \ s x x -- \\ test name, expected value, actual value
|
||||
2drop
|
||||
1 tests-passed n:+!
|
||||
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
|
||||
;
|
||||
|
||||
: test-skipped \ s --
|
||||
1 tests-skipped n:+!
|
||||
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
|
||||
;
|
||||
|
||||
: test-failed \ s x x -- \\ test name, expected value, actual value
|
||||
1 tests-failed n:+!
|
||||
rot
|
||||
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
|
||||
" Actual: «" . . "»" . cr
|
||||
" Expected: «" . . "»" . cr cr
|
||||
;
|
||||
|
||||
: isword? \ x -- x f
|
||||
dup >kind ns:w n:=
|
||||
;
|
||||
|
||||
: run-test? \ -- T
|
||||
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
|
||||
;
|
||||
|
||||
\ Num passed + num skipped + num failed should == num tests
|
||||
: all-tests-run? \ -- T
|
||||
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
|
||||
test-count @ n:=
|
||||
;
|
||||
|
||||
\ returns true if x is a date, false otherwise
|
||||
: date? \ x -- x T
|
||||
dup >kind ns:d n:=
|
||||
;
|
||||
|
||||
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
|
||||
: eq? \ x x -- T
|
||||
\ are the items the same kind?
|
||||
2dup >kind swap >kind n:=
|
||||
!if 2drop false ;then
|
||||
|
||||
\ same kind: try different comparators
|
||||
number? if n:= ;then
|
||||
string? if s:= ;then
|
||||
array? if ' eq? a:= 2nip ;then
|
||||
map? if ' eq? m:= 2nip ;then
|
||||
date? if d:= ;then
|
||||
|
||||
\ otherwise fall back to 'lazy evaluation'
|
||||
G:l: =
|
||||
;
|
||||
|
||||
: eps_eq? \ n x x -- T
|
||||
\ are the items the same kind?
|
||||
2dup >kind swap >kind n:=
|
||||
!if 2drop false ;then
|
||||
number? !if 2drop false ;then
|
||||
rot n:~=
|
||||
;
|
||||
|
||||
: check-depth \ ... n -- ...
|
||||
dup>r
|
||||
n:1+ depth n:=
|
||||
!if
|
||||
con:red con:onBlack
|
||||
"PANIC: expected stack depth to be " . r> . cr
|
||||
"Stack is:" . cr
|
||||
.s cr
|
||||
255 die
|
||||
then
|
||||
rdrop
|
||||
;
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
\ status report at end of run
|
||||
( all-tests-run?
|
||||
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
|
||||
) onexit
|
||||
|
||||
\ Print a summary of the tests run
|
||||
( con:white con:onBlack
|
||||
test-count @ . space "tests planned - " .
|
||||
tests-passed @ . space "passed - " .
|
||||
tests-skipped @ . space "skipped - " .
|
||||
tests-failed @ . space "failed" . cr
|
||||
) onexit
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
\ The public-facing words
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
: equal? \ s x w -- | s w x --
|
||||
run-test? !if 2drop test-skipped ;; then
|
||||
isword? !if swap then
|
||||
w:exec
|
||||
3 check-depth
|
||||
2dup \ so test-failed can show actual and expected
|
||||
eq? if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: approx_equal? \ s x w n -- | s w x n --
|
||||
run-test? !if 3drop test-skipped ;; then
|
||||
-rot isword? !if swap then
|
||||
w:exec
|
||||
4 check-depth
|
||||
3dup \ so test-failed can show actual and expected
|
||||
eps_eq?
|
||||
if rot drop test-passed else rot drop test-failed then
|
||||
;
|
||||
|
||||
: true? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
true swap dup \ so test-failed can show actual and expected
|
||||
if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: false? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
false swap dup \ so test-failed can show actual and expected
|
||||
!if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: null? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
null swap dup \ so test-failed can show actual and expected
|
||||
G:null? nip if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: not-null? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
null swap dup \ so test-failed can show actual and expected
|
||||
G:null? not nip if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: SKIP-REST-OF-TESTS false run-test ! ;
|
||||
|
||||
: tests \ n --
|
||||
test-count !
|
||||
;
|
||||
|
||||
\ Set the exit status:
|
||||
\ 0 = all OK
|
||||
\ 1 = not all tests were run (some error occurred)
|
||||
\ 2 = some tests failed
|
||||
: end-of-tests \ --
|
||||
all-tests-run?
|
||||
if
|
||||
tests-failed @ 0 n:= if 0 else 2 then
|
||||
else
|
||||
1
|
||||
then
|
||||
die
|
||||
;
|
||||
|
||||
125
exercises/practice/bottle-song/test.8th
Normal file
125
exercises/practice/bottle-song/test.8th
Normal file
@@ -0,0 +1,125 @@
|
||||
"bottle-song.8th" f:include
|
||||
needs exercism/test
|
||||
with: test
|
||||
7 tests
|
||||
|
||||
"First generic verse"
|
||||
( 10 1 recite )
|
||||
"Ten green bottles hanging on the wall,\n"
|
||||
"Ten green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be nine green bottles hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
SKIP-REST-OF-TESTS
|
||||
|
||||
"Last generic verse"
|
||||
( 3 1 recite )
|
||||
"Three green bottles hanging on the wall,\n"
|
||||
"Three green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be two green bottles hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
"Verse with 2 bottles"
|
||||
( 2 1 recite )
|
||||
"Two green bottles hanging on the wall,\n"
|
||||
"Two green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be one green bottle hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
"Verse with 1 bottle"
|
||||
( 1 1 recite )
|
||||
"One green bottle hanging on the wall,\n"
|
||||
"One green bottle hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be no green bottles hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
"First two verses"
|
||||
( 10 2 recite )
|
||||
"Ten green bottles hanging on the wall,\n"
|
||||
"Ten green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be nine green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Nine green bottles hanging on the wall,\n" s:+
|
||||
"Nine green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be eight green bottles hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
"Last three verses"
|
||||
( 3 3 recite )
|
||||
"Three green bottles hanging on the wall,\n"
|
||||
"Three green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be two green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Two green bottles hanging on the wall,\n" s:+
|
||||
"Two green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be one green bottle hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"One green bottle hanging on the wall,\n" s:+
|
||||
"One green bottle hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be no green bottles hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
"All verses"
|
||||
( 10 10 recite )
|
||||
"Ten green bottles hanging on the wall,\n"
|
||||
"Ten green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be nine green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Nine green bottles hanging on the wall,\n" s:+
|
||||
"Nine green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be eight green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Eight green bottles hanging on the wall,\n" s:+
|
||||
"Eight green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be seven green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Seven green bottles hanging on the wall,\n" s:+
|
||||
"Seven green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be six green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Six green bottles hanging on the wall,\n" s:+
|
||||
"Six green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be five green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Five green bottles hanging on the wall,\n" s:+
|
||||
"Five green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be four green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Four green bottles hanging on the wall,\n" s:+
|
||||
"Four green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be three green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Three green bottles hanging on the wall,\n" s:+
|
||||
"Three green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be two green bottles hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"Two green bottles hanging on the wall,\n" s:+
|
||||
"Two green bottles hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be one green bottle hanging on the wall.\n" s:+
|
||||
"\n" s:+
|
||||
"One green bottle hanging on the wall,\n" s:+
|
||||
"One green bottle hanging on the wall,\n" s:+
|
||||
"And if one green bottle should accidentally fall,\n" s:+
|
||||
"There'll be no green bottles hanging on the wall." s:+
|
||||
equal?
|
||||
|
||||
end-of-tests
|
||||
;with
|
||||
Reference in New Issue
Block a user