Skip to content

Commit

Permalink
Add kindergarten-garden exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed May 11, 2024
1 parent 3ae76f2 commit 84ca205
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "kindergarten-garden",
"name": "Kindergarten Garden",
"uuid": "ff10d863-aec1-43cc-9e01-65231c2ac8dc",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
Expand Down
58 changes: 58 additions & 0 deletions exercises/practice/kindergarten-garden/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Instructions

Given a diagram, determine which plants each child in the kindergarten class is
responsible for.

The kindergarten class is learning about growing plants.
The teacher thought it would be a good idea to give them actual seeds, plant them in actual dirt, and grow actual plants.

They've chosen to grow grass, clover, radishes, and violets.

To this end, the children have put little cups along the window sills, and
planted one type of plant in each cup, choosing randomly from the available
types of seeds.

```text
[window][window][window]
........................ # each dot represents a cup
........................
```

There are 12 children in the class:

- Alice, Bob, Charlie, David,
- Eve, Fred, Ginny, Harriet,
- Ileana, Joseph, Kincaid, and Larry.

Each child gets 4 cups, two on each row.
Their teacher assigns cups to the children alphabetically by their names.

The following diagram represents Alice's plants:

```text
[window][window][window]
VR......................
RG......................
```

In the first row, nearest the windows, she has a violet and a radish.
In the second row she has a radish and some grass.

Your program will be given the plants from left-to-right starting with the row nearest the windows.
From this, it should be able to determine which plants belong to each student.

For example, if it's told that the garden looks like so:

```text
[window][window][window]
VRCGVVRVCGGCCGVRGCVCGCGV
VRCCCGCRRGVCGCRVVCVGCGCV
```

Then if asked for Alice's plants, it should provide:

- Violets, radishes, violets, radishes

While asking for Bob's plants would yield:

- Clover, grass, clover, clover
19 changes: 19 additions & 0 deletions exercises/practice/kindergarten-garden/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"kindergarten-garden.8th"
],
"test": [
"test.8th"
],
"example": [
".meta/example.8th"
]
},
"blurb": "Given a diagram, determine which plants each child in the kindergarten class is responsible for.",
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
"source_url": "https://turing.edu"
}
19 changes: 19 additions & 0 deletions exercises/practice/kindergarten-garden/.meta/example.8th
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
["Alice", "Bob", "Charlie", "David", "Eve", "Fred", "Ginny", "Harriet", "Ileana", "Joseph", "Kincaid", "Larry"] constant children

{ "G": "grass", "C": "clover", "R": "radishes", "V": "violets" } constant plants

: sill-idx \ s -- n
children swap ' s:= a:indexof 2 n:* nip
;

: sills \ n s -- a
"\n" s:/ ( null s:/ over 2 a:slice ) a:map a:squash nip
;

: plant \ s -- s
plants swap caseof
;

: plants \ s s - a
sill-idx swap sills ' plant a:map
;
61 changes: 61 additions & 0 deletions exercises/practice/kindergarten-garden/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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.

[1fc316ed-17ab-4fba-88ef-3ae78296b692]
description = "partial garden -> garden with single student"

[acd19dc1-2200-4317-bc2a-08f021276b40]
description = "partial garden -> different garden with single student"

[c376fcc8-349c-446c-94b0-903947315757]
description = "partial garden -> garden with two students"

[2d620f45-9617-4924-9d27-751c80d17db9]
description = "partial garden -> multiple students for the same garden with three students -> second student's garden"

[57712331-4896-4364-89f8-576421d69c44]
description = "partial garden -> multiple students for the same garden with three students -> third student's garden"

[149b4290-58e1-40f2-8ae4-8b87c46e765b]
description = "full garden -> for Alice, first student's garden"

[ba25dbbc-10bd-4a37-b18e-f89ecd098a5e]
description = "full garden -> for Bob, second student's garden"

[566b621b-f18e-4c5f-873e-be30544b838c]
description = "full garden -> for Charlie"

[3ad3df57-dd98-46fc-9269-1877abf612aa]
description = "full garden -> for David"

[0f0a55d1-9710-46ed-a0eb-399ba8c72db2]
description = "full garden -> for Eve"

[a7e80c90-b140-4ea1-aee3-f4625365c9a4]
description = "full garden -> for Fred"

[9d94b273-2933-471b-86e8-dba68694c615]
description = "full garden -> for Ginny"

[f55bc6c2-ade8-4844-87c4-87196f1b7258]
description = "full garden -> for Harriet"

[759070a3-1bb1-4dd4-be2c-7cce1d7679ae]
description = "full garden -> for Ileana"

[78578123-2755-4d4a-9c7d-e985b8dda1c6]
description = "full garden -> for Joseph"

[6bb66df7-f433-41ab-aec2-3ead6e99f65b]
description = "full garden -> for Kincaid, second to last student's garden"

[d7edec11-6488-418a-94e6-ed509e0fa7eb]
description = "full garden -> for Larry, last student's garden"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
: plants \ s s - a

;
173 changes: 173 additions & 0 deletions exercises/practice/kindergarten-garden/libs/exercism/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
needs console/loaded

\ -----------------------------------------------------------------

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'
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
;

: 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
;

Loading

0 comments on commit 84ca205

Please sign in to comment.