Skip to content

Commit 199cc03

Browse files
authored
Add Kindergarten Garden Exercise (#632)
1 parent 10f0cfe commit 199cc03

File tree

7 files changed

+399
-0
lines changed

7 files changed

+399
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,14 @@
11871187
"practices": [],
11881188
"prerequisites": [],
11891189
"difficulty": 6
1190+
},
1191+
{
1192+
"slug": "kindergarten-garden",
1193+
"name": "Kindergarten Garden",
1194+
"uuid": "2fd3d924-477d-4113-96a5-c9687776da49",
1195+
"practices": [],
1196+
"prerequisites": [],
1197+
"difficulty": 3
11901198
}
11911199
]
11921200
},
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Instructions
2+
3+
Given a diagram, determine which plants each child in the kindergarten class is
4+
responsible for.
5+
6+
The kindergarten class is learning about growing plants.
7+
The teacher thought it would be a good idea to give them actual seeds, plant them in actual dirt, and grow actual plants.
8+
9+
They've chosen to grow grass, clover, radishes, and violets.
10+
11+
To this end, the children have put little cups along the window sills, and
12+
planted one type of plant in each cup, choosing randomly from the available
13+
types of seeds.
14+
15+
```text
16+
[window][window][window]
17+
........................ # each dot represents a cup
18+
........................
19+
```
20+
21+
There are 12 children in the class:
22+
23+
- Alice, Bob, Charlie, David,
24+
- Eve, Fred, Ginny, Harriet,
25+
- Ileana, Joseph, Kincaid, and Larry.
26+
27+
Each child gets 4 cups, two on each row.
28+
Their teacher assigns cups to the children alphabetically by their names.
29+
30+
The following diagram represents Alice's plants:
31+
32+
```text
33+
[window][window][window]
34+
VR......................
35+
RG......................
36+
```
37+
38+
In the first row, nearest the windows, she has a violet and a radish.
39+
In the second row she has a radish and some grass.
40+
41+
Your program will be given the plants from left-to-right starting with the row nearest the windows.
42+
From this, it should be able to determine which plants belong to each student.
43+
44+
For example, if it's told that the garden looks like so:
45+
46+
```text
47+
[window][window][window]
48+
VRCGVVRVCGGCCGVRGCVCGCGV
49+
VRCCCGCRRGVCGCRVVCVGCGCV
50+
```
51+
52+
Then if asked for Alice's plants, it should provide:
53+
54+
- Violets, radishes, violets, radishes
55+
56+
While asking for Bob's plants would yield:
57+
58+
- Clover, grass, clover, clover
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"tomasnorre"
4+
],
5+
"files": {
6+
"solution": [
7+
"KindergartenGarden.php"
8+
],
9+
"test": [
10+
"KindergartenGardenTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
14+
]
15+
},
16+
"blurb": "Given a diagram, determine which plants each child in the kindergarten class is responsible for.",
17+
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
18+
"source_url": "https://turing.edu"
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class KindergartenGarden
6+
{
7+
private const STUDENTS = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Fred', 'Ginny', 'Harriet', 'Ileana', 'Joseph', 'Kincaid', 'Larry'];
8+
9+
private $plantsMap = [
10+
'G' => 'grass',
11+
'C' => 'clover',
12+
'R' => 'radishes',
13+
'V' => 'violets',
14+
];
15+
16+
private $studentPlants = [];
17+
18+
public function __construct(string $diagram)
19+
{
20+
$rows = explode("\n", $diagram);
21+
foreach ($rows as $row) {
22+
for ($i = 0, $iMax = strlen($row); $i < $iMax; $i += 2) {
23+
$student = (int)($i / 2);
24+
foreach (self::STUDENTS as $index => $name) {
25+
if ($index === $student) {
26+
$this->studentPlants[$name][] = $this->plantsMap[$row[$i]];
27+
$this->studentPlants[$name][] = $this->plantsMap[$row[$i + 1]];
28+
break;
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
public function plants(string $student): array
36+
{
37+
return $this->studentPlants[$student] ?? [];
38+
}
39+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1fc316ed-17ab-4fba-88ef-3ae78296b692]
13+
description = "partial garden -> garden with single student"
14+
15+
[acd19dc1-2200-4317-bc2a-08f021276b40]
16+
description = "partial garden -> different garden with single student"
17+
18+
[c376fcc8-349c-446c-94b0-903947315757]
19+
description = "partial garden -> garden with two students"
20+
21+
[2d620f45-9617-4924-9d27-751c80d17db9]
22+
description = "partial garden -> multiple students for the same garden with three students -> second student's garden"
23+
24+
[57712331-4896-4364-89f8-576421d69c44]
25+
description = "partial garden -> multiple students for the same garden with three students -> third student's garden"
26+
27+
[149b4290-58e1-40f2-8ae4-8b87c46e765b]
28+
description = "full garden -> for Alice, first student's garden"
29+
30+
[ba25dbbc-10bd-4a37-b18e-f89ecd098a5e]
31+
description = "full garden -> for Bob, second student's garden"
32+
33+
[566b621b-f18e-4c5f-873e-be30544b838c]
34+
description = "full garden -> for Charlie"
35+
36+
[3ad3df57-dd98-46fc-9269-1877abf612aa]
37+
description = "full garden -> for David"
38+
39+
[0f0a55d1-9710-46ed-a0eb-399ba8c72db2]
40+
description = "full garden -> for Eve"
41+
42+
[a7e80c90-b140-4ea1-aee3-f4625365c9a4]
43+
description = "full garden -> for Fred"
44+
45+
[9d94b273-2933-471b-86e8-dba68694c615]
46+
description = "full garden -> for Ginny"
47+
48+
[f55bc6c2-ade8-4844-87c4-87196f1b7258]
49+
description = "full garden -> for Harriet"
50+
51+
[759070a3-1bb1-4dd4-be2c-7cce1d7679ae]
52+
description = "full garden -> for Ileana"
53+
54+
[78578123-2755-4d4a-9c7d-e985b8dda1c6]
55+
description = "full garden -> for Joseph"
56+
57+
[6bb66df7-f433-41ab-aec2-3ead6e99f65b]
58+
description = "full garden -> for Kincaid, second to last student's garden"
59+
60+
[d7edec11-6488-418a-94e6-ed509e0fa7eb]
61+
description = "full garden -> for Larry, last student's garden"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class KindergartenGarden
28+
{
29+
public function __construct(string $diagram)
30+
{
31+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
32+
}
33+
34+
public function plants(string $student): array
35+
{
36+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
37+
}
38+
}

0 commit comments

Comments
 (0)