Skip to content

Commit 4da6f15

Browse files
vaengahans
andauthored
feat: add kindergarten-garden exercise (#804)
Co-authored-by: Alexander Hans <[email protected]>
1 parent bd28108 commit 4da6f15

File tree

12 files changed

+18300
-0
lines changed

12 files changed

+18300
-0
lines changed

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,18 @@
11331133
"prerequisites": [],
11341134
"difficulty": 7
11351135
},
1136+
{
1137+
"slug": "kindergarten-garden",
1138+
"name": "Kindergarten Garden",
1139+
"uuid": "5bc93b17-5209-4517-985e-f6fb3252afb3",
1140+
"practices": [
1141+
"enums"
1142+
],
1143+
"prerequisites": [
1144+
"vector-arrays"
1145+
],
1146+
"difficulty": 3
1147+
},
11361148
{
11371149
"slug": "dnd-character",
11381150
"name": "D&D Character",
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"authors": [
3+
"vaeng"
4+
],
5+
"files": {
6+
"solution": [
7+
"kindergarten_garden.cpp",
8+
"kindergarten_garden.h"
9+
],
10+
"test": [
11+
"kindergarten_garden_test.cpp"
12+
],
13+
"example": [
14+
".meta/example.cpp",
15+
".meta/example.h"
16+
]
17+
},
18+
"blurb": "Given a diagram, determine which plants each child in the kindergarten class is responsible for.",
19+
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
20+
"source_url": "https://turing.edu"
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "kindergarten_garden.h"
2+
3+
#include <algorithm>
4+
5+
namespace kindergarten_garden {
6+
7+
std::array<Plants, 4> plants(std::string_view diagram,
8+
std::string_view student) {
9+
const int first_plant_index = 2 * (student.at(0) - 'A');
10+
const size_t second_plant_index{diagram.find('\n') + first_plant_index + 1};
11+
const std::array<char, 4> positions{
12+
diagram[first_plant_index], diagram[first_plant_index + 1],
13+
diagram[second_plant_index], diagram[second_plant_index + 1]};
14+
std::array<Plants, 4> result;
15+
std::transform(positions.cbegin(), positions.cend(), result.begin(),
16+
[](char c) { return static_cast<Plants>(c); });
17+
18+
return result;
19+
}
20+
21+
} // namespace kindergarten_garden
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <string_view>
5+
6+
namespace kindergarten_garden {
7+
8+
enum class Plants : char { grass = 'G', clover = 'C', radishes = 'R', violets = 'V' };
9+
10+
std::array<Plants, 4> plants(std::string_view diagram,
11+
std::string_view student);
12+
} // namespace kindergarten_garden
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Get the exercise name from the current directory
2+
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
3+
4+
# Basic CMake project
5+
cmake_minimum_required(VERSION 3.5.1)
6+
7+
# Name the project after the exercise
8+
project(${exercise} CXX)
9+
10+
# Get a source filename from the exercise name by replacing -'s with _'s
11+
string(REPLACE "-" "_" file ${exercise})
12+
13+
# Implementation could be only a header
14+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
15+
set(exercise_cpp ${file}.cpp)
16+
else()
17+
set(exercise_cpp "")
18+
endif()
19+
20+
# Use the common Catch library?
21+
if(EXERCISM_COMMON_CATCH)
22+
# For Exercism track development only
23+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
24+
elseif(EXERCISM_TEST_SUITE)
25+
# The Exercism test suite is being run, the Docker image already
26+
# includes a pre-built version of Catch.
27+
find_package(Catch2 REQUIRED)
28+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
29+
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
30+
# When Catch is installed system wide we need to include a different
31+
# header, we need this define to use the correct one.
32+
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
33+
else()
34+
# Build executable from sources and headers
35+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
36+
endif()
37+
38+
set_target_properties(${exercise} PROPERTIES
39+
CXX_STANDARD 17
40+
CXX_STANDARD_REQUIRED OFF
41+
CXX_EXTENSIONS OFF
42+
)
43+
44+
set(CMAKE_BUILD_TYPE Debug)
45+
46+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
47+
set_target_properties(${exercise} PROPERTIES
48+
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
49+
)
50+
endif()
51+
52+
# Configure to run all the tests?
53+
if(${EXERCISM_RUN_ALL_TESTS})
54+
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
55+
endif()
56+
57+
# Tell MSVC not to warn us about unchecked iterators in debug builds
58+
if(${MSVC})
59+
set_target_properties(${exercise} PROPERTIES
60+
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
61+
endif()
62+
63+
# Run the tests on every build
64+
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "kindergarten_garden.h"
2+
3+
namespace kindergarten_garden {
4+
5+
} // namespace kindergarten_garden
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
namespace kindergarten_garden {
4+
5+
} // namespace kindergarten_garden
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "kindergarten_garden.h"
2+
#ifdef EXERCISM_TEST_SUITE
3+
#include <catch2/catch.hpp>
4+
#else
5+
#include "test/catch.hpp"
6+
#endif
7+
8+
// improves error messages with triangle flavor enum text instead of integers:
9+
CATCH_REGISTER_ENUM(kindergarten_garden::Plants,
10+
kindergarten_garden::Plants::clover,
11+
kindergarten_garden::Plants::grass,
12+
kindergarten_garden::Plants::violets,
13+
kindergarten_garden::Plants::radishes)
14+
15+
TEST_CASE("garden with single student", "[1fc316ed-17ab-4fba-88ef-3ae78296b692]") {
16+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::grass};
17+
REQUIRE(kindergarten_garden::plants("RC\nGG", "Alice") == expected);
18+
}
19+
20+
#if defined(EXERCISM_RUN_ALL_TESTS)
21+
22+
TEST_CASE("different garden with single student", "[acd19dc1-2200-4317-bc2a-08f021276b40]") {
23+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::clover};
24+
REQUIRE(kindergarten_garden::plants("VC\nRC", "Alice") == expected);
25+
}
26+
27+
TEST_CASE("garden with two students", "[c376fcc8-349c-446c-94b0-903947315757]") {
28+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::clover};
29+
REQUIRE(kindergarten_garden::plants("VVCG\nVVRC", "Bob") == expected);
30+
}
31+
32+
TEST_CASE("second student's garden", "[2d620f45-9617-4924-9d27-751c80d17db9]") {
33+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::clover, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::clover};
34+
REQUIRE(kindergarten_garden::plants("VVCCGG\nVVCCGG", "Bob") == expected);
35+
}
36+
37+
TEST_CASE("third student's garden", "[57712331-4896-4364-89f8-576421d69c44]") {
38+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::grass, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::grass};
39+
REQUIRE(kindergarten_garden::plants("VVCCGG\nVVCCGG", "Charlie") == expected);
40+
}
41+
42+
TEST_CASE("for Alice, first student's garden", "[149b4290-58e1-40f2-8ae4-8b87c46e765b]") {
43+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::violets, kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::radishes};
44+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Alice") == expected);
45+
}
46+
47+
TEST_CASE("for Bob, second student's garden", "[ba25dbbc-10bd-4a37-b18e-f89ecd098a5e]") {
48+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::clover};
49+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Bob") == expected);
50+
}
51+
52+
TEST_CASE("for Charlie", "[566b621b-f18e-4c5f-873e-be30544b838c]") {
53+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::violets, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass};
54+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Charlie") == expected);
55+
}
56+
57+
TEST_CASE("for David", "[3ad3df57-dd98-46fc-9269-1877abf612aa]") {
58+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::radishes};
59+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "David") == expected);
60+
}
61+
62+
TEST_CASE("for Eve", "[0f0a55d1-9710-46ed-a0eb-399ba8c72db2]") {
63+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::grass};
64+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Eve") == expected);
65+
}
66+
67+
TEST_CASE("for Fred", "[a7e80c90-b140-4ea1-aee3-f4625365c9a4]") {
68+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::grass, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover};
69+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Fred") == expected);
70+
}
71+
72+
TEST_CASE("for Ginny", "[9d94b273-2933-471b-86e8-dba68694c615]") {
73+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::grass, kindergarten_garden::Plants::clover};
74+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Ginny") == expected);
75+
}
76+
77+
TEST_CASE("for Harriet", "[f55bc6c2-ade8-4844-87c4-87196f1b7258]") {
78+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::violets, kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::radishes, kindergarten_garden::Plants::violets};
79+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Harriet") == expected);
80+
}
81+
82+
TEST_CASE("for Ileana", "[759070a3-1bb1-4dd4-be2c-7cce1d7679ae]") {
83+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::grass, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover};
84+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Ileana") == expected);
85+
}
86+
87+
TEST_CASE("for Joseph", "[78578123-2755-4d4a-9c7d-e985b8dda1c6]") {
88+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::grass};
89+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Joseph") == expected);
90+
}
91+
92+
TEST_CASE("for Kincaid, second to last student's garden", "[6bb66df7-f433-41ab-aec2-3ead6e99f65b]") {
93+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::grass, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::grass};
94+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Kincaid") == expected);
95+
}
96+
97+
TEST_CASE("for Larry, last student's garden", "[d7edec11-6488-418a-94e6-ed509e0fa7eb]") {
98+
std::array<kindergarten_garden::Plants, 4> expected{kindergarten_garden::Plants::grass, kindergarten_garden::Plants::violets, kindergarten_garden::Plants::clover, kindergarten_garden::Plants::violets};
99+
REQUIRE(kindergarten_garden::plants("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV", "Larry") == expected);
100+
}
101+
102+
#endif

0 commit comments

Comments
 (0)