Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 51e867f

Browse files
committedJan 27, 2025·
Add kindergarten-garden exercise
1 parent ff72175 commit 51e867f

File tree

8 files changed

+368
-0
lines changed

8 files changed

+368
-0
lines changed
 

‎config.json

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@
114114
"prerequisites": [],
115115
"difficulty": 3
116116
},
117+
{
118+
"slug": "kindergarten-garden",
119+
"name": "Kindergarten Garden",
120+
"uuid": "8ffd8bb1-f57d-4c9e-adcc-76188a7e1603",
121+
"practices": [],
122+
"prerequisites": [],
123+
"difficulty": 3
124+
},
117125
{
118126
"slug": "scrabble-score",
119127
"name": "Scrabble Score",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Instructions
2+
3+
Your task is to, given a diagram, determine which plants each child in the kindergarten class is responsible for.
4+
5+
There are 12 children in the class:
6+
7+
- Alice, Bob, Charlie, David, Eve, Fred, Ginny, Harriet, Ileana, Joseph, Kincaid, and Larry.
8+
9+
Four different types of seeds are planted:
10+
11+
| Plant | Diagram encoding |
12+
| ------ | ---------------- |
13+
| Grass | G |
14+
| Clover | C |
15+
| Radish | R |
16+
| Violet | V |
17+
18+
Each child gets four cups, two on each row:
19+
20+
```text
21+
[window][window][window]
22+
........................ # each dot represents a cup
23+
........................
24+
```
25+
26+
Their teacher assigns cups to the children alphabetically by their names, which means that Alice comes first and Larry comes last.
27+
28+
Here is an example diagram representing Alice's plants:
29+
30+
```text
31+
[window][window][window]
32+
VR......................
33+
RG......................
34+
```
35+
36+
In the first row, nearest the windows, she has a violet and a radish.
37+
In the second row she has a radish and some grass.
38+
39+
Your program will be given the plants from left-to-right starting with the row nearest the windows.
40+
From this, it should be able to determine which plants belong to each student.
41+
42+
For example, if it's told that the garden looks like so:
43+
44+
```text
45+
[window][window][window]
46+
VRCGVVRVCGGCCGVRGCVCGCGV
47+
VRCCCGCRRGVCGCRVVCVGCGCV
48+
```
49+
50+
Then if asked for Alice's plants, it should provide:
51+
52+
- Violets, radishes, violets, radishes
53+
54+
While asking for Bob's plants would yield:
55+
56+
- Clover, grass, clover, clover
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
The kindergarten class is learning about growing plants.
4+
The teacher thought it would be a good idea to give the class seeds to plant and grow in the dirt.
5+
To this end, the children have put little cups along the window sills and planted one type of plant in each cup.
6+
The children got to pick their favorites from four available types of seeds: grass, clover, radishes, and violets.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"kindergarten_garden.zig"
8+
],
9+
"test": [
10+
"test_kindergarten_garden.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub const Plant = enum {
2+
clover,
3+
grass,
4+
radishes,
5+
violets,
6+
};
7+
8+
fn decode(letter: u8) Plant {
9+
if (letter == 'C') {
10+
return .clover;
11+
}
12+
if (letter == 'G') {
13+
return .grass;
14+
}
15+
if (letter == 'R') {
16+
return .radishes;
17+
}
18+
return .violets;
19+
}
20+
21+
pub fn plants(diagram: []const u8, student: []const u8) [4]Plant {
22+
const first = 2 * (student[0] - 'A');
23+
const second = first + 1;
24+
const third = (diagram.len + 1) / 2 + first;
25+
const fourth = third + 1;
26+
return .{
27+
decode(diagram[first]),
28+
decode(diagram[second]),
29+
decode(diagram[third]),
30+
decode(diagram[fourth]),
31+
};
32+
}
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub const Plant = enum {
2+
clover,
3+
grass,
4+
radishes,
5+
violets,
6+
};
7+
8+
pub fn plants(diagram: []const u8, student: []const u8) [4]Plant {
9+
_ = diagram;
10+
_ = student;
11+
@compileError("please implement the plants function");
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
const kindergarten_garden = @import("kindergarten_garden.zig");
5+
6+
test "partial garden with single student" {
7+
const diagram: []const u8 =
8+
\\RC
9+
\\GG
10+
;
11+
const expected = .{ .radishes, .clover, .grass, .grass };
12+
const actual = kindergarten_garden.plants(diagram, "Alice");
13+
try testing.expectEqual(expected, actual);
14+
}
15+
16+
test "partial garden-different garden with single student" {
17+
const diagram: []const u8 =
18+
\\VC
19+
\\RC
20+
;
21+
const expected = .{ .violets, .clover, .radishes, .clover };
22+
const actual = kindergarten_garden.plants(diagram, "Alice");
23+
try testing.expectEqual(expected, actual);
24+
}
25+
26+
test "partial garden with two students" {
27+
const diagram: []const u8 =
28+
\\VVCG
29+
\\VVRC
30+
;
31+
const expected = .{ .clover, .grass, .radishes, .clover };
32+
const actual = kindergarten_garden.plants(diagram, "Bob");
33+
try testing.expectEqual(expected, actual);
34+
}
35+
36+
test "partial garden-multiple students for the same garden with three students-second student's garden" {
37+
const diagram: []const u8 =
38+
\\VVCCGG
39+
\\VVCCGG
40+
;
41+
const expected = .{ .clover, .clover, .clover, .clover };
42+
const actual = kindergarten_garden.plants(diagram, "Bob");
43+
try testing.expectEqual(expected, actual);
44+
}
45+
46+
test "partial garden-multiple students for the same garden with three students-third student's garden" {
47+
const diagram: []const u8 =
48+
\\VVCCGG
49+
\\VVCCGG
50+
;
51+
const expected = .{ .grass, .grass, .grass, .grass };
52+
const actual = kindergarten_garden.plants(diagram, "Charlie");
53+
try testing.expectEqual(expected, actual);
54+
}
55+
56+
test "full garden-for Alice, first student's garden" {
57+
const diagram: []const u8 =
58+
\\VRCGVVRVCGGCCGVRGCVCGCGV
59+
\\VRCCCGCRRGVCGCRVVCVGCGCV
60+
;
61+
const expected = .{ .violets, .radishes, .violets, .radishes };
62+
const actual = kindergarten_garden.plants(diagram, "Alice");
63+
try testing.expectEqual(expected, actual);
64+
}
65+
66+
test "full garden-for Bob, second student's garden" {
67+
const diagram: []const u8 =
68+
\\VRCGVVRVCGGCCGVRGCVCGCGV
69+
\\VRCCCGCRRGVCGCRVVCVGCGCV
70+
;
71+
const expected = .{ .clover, .grass, .clover, .clover };
72+
const actual = kindergarten_garden.plants(diagram, "Bob");
73+
try testing.expectEqual(expected, actual);
74+
}
75+
76+
test "full garden-for Charlie" {
77+
const diagram: []const u8 =
78+
\\VRCGVVRVCGGCCGVRGCVCGCGV
79+
\\VRCCCGCRRGVCGCRVVCVGCGCV
80+
;
81+
const expected = .{ .violets, .violets, .clover, .grass };
82+
const actual = kindergarten_garden.plants(diagram, "Charlie");
83+
try testing.expectEqual(expected, actual);
84+
}
85+
86+
test "full garden-for David" {
87+
const diagram: []const u8 =
88+
\\VRCGVVRVCGGCCGVRGCVCGCGV
89+
\\VRCCCGCRRGVCGCRVVCVGCGCV
90+
;
91+
const expected = .{ .radishes, .violets, .clover, .radishes };
92+
const actual = kindergarten_garden.plants(diagram, "David");
93+
try testing.expectEqual(expected, actual);
94+
}
95+
96+
test "full garden-for Eve" {
97+
const diagram: []const u8 =
98+
\\VRCGVVRVCGGCCGVRGCVCGCGV
99+
\\VRCCCGCRRGVCGCRVVCVGCGCV
100+
;
101+
const expected = .{ .clover, .grass, .radishes, .grass };
102+
const actual = kindergarten_garden.plants(diagram, "Eve");
103+
try testing.expectEqual(expected, actual);
104+
}
105+
106+
test "full garden-for Fred" {
107+
const diagram: []const u8 =
108+
\\VRCGVVRVCGGCCGVRGCVCGCGV
109+
\\VRCCCGCRRGVCGCRVVCVGCGCV
110+
;
111+
const expected = .{ .grass, .clover, .violets, .clover };
112+
const actual = kindergarten_garden.plants(diagram, "Fred");
113+
try testing.expectEqual(expected, actual);
114+
}
115+
116+
test "full garden-for Ginny" {
117+
const diagram: []const u8 =
118+
\\VRCGVVRVCGGCCGVRGCVCGCGV
119+
\\VRCCCGCRRGVCGCRVVCVGCGCV
120+
;
121+
const expected = .{ .clover, .grass, .grass, .clover };
122+
const actual = kindergarten_garden.plants(diagram, "Ginny");
123+
try testing.expectEqual(expected, actual);
124+
}
125+
126+
test "full garden-for Harriet" {
127+
const diagram: []const u8 =
128+
\\VRCGVVRVCGGCCGVRGCVCGCGV
129+
\\VRCCCGCRRGVCGCRVVCVGCGCV
130+
;
131+
const expected = .{ .violets, .radishes, .radishes, .violets };
132+
const actual = kindergarten_garden.plants(diagram, "Harriet");
133+
try testing.expectEqual(expected, actual);
134+
}
135+
136+
test "full garden-for Ileana" {
137+
const diagram: []const u8 =
138+
\\VRCGVVRVCGGCCGVRGCVCGCGV
139+
\\VRCCCGCRRGVCGCRVVCVGCGCV
140+
;
141+
const expected = .{ .grass, .clover, .violets, .clover };
142+
const actual = kindergarten_garden.plants(diagram, "Ileana");
143+
try testing.expectEqual(expected, actual);
144+
}
145+
146+
test "full garden-for Joseph" {
147+
const diagram: []const u8 =
148+
\\VRCGVVRVCGGCCGVRGCVCGCGV
149+
\\VRCCCGCRRGVCGCRVVCVGCGCV
150+
;
151+
const expected = .{ .violets, .clover, .violets, .grass };
152+
const actual = kindergarten_garden.plants(diagram, "Joseph");
153+
try testing.expectEqual(expected, actual);
154+
}
155+
156+
test "full garden-for Kincaid, second to last student's garden" {
157+
const diagram: []const u8 =
158+
\\VRCGVVRVCGGCCGVRGCVCGCGV
159+
\\VRCCCGCRRGVCGCRVVCVGCGCV
160+
;
161+
const expected = .{ .grass, .clover, .clover, .grass };
162+
const actual = kindergarten_garden.plants(diagram, "Kincaid");
163+
try testing.expectEqual(expected, actual);
164+
}
165+
166+
test "full garden-for Larry, last student's garden" {
167+
const diagram: []const u8 =
168+
\\VRCGVVRVCGGCCGVRGCVCGCGV
169+
\\VRCCCGCRRGVCGCRVVCVGCGCV
170+
;
171+
const expected = .{ .grass, .violets, .clover, .violets };
172+
const actual = kindergarten_garden.plants(diagram, "Larry");
173+
try testing.expectEqual(expected, actual);
174+
}

0 commit comments

Comments
 (0)
Please sign in to comment.