Skip to content

Commit 8b0ae57

Browse files
committed
Add kindergarten-garden (exercism#940)
1 parent 4f5b4fc commit 8b0ae57

File tree

13 files changed

+4280
-20
lines changed

13 files changed

+4280
-20
lines changed

config.json

+27-20
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"language": "C",
33
"slug": "c",
44
"active": true,
5-
"blurb": "C is a small, general-purpose, imperative programming language with a static type system, scopes, and structures. It's typically used as an alternative to assembly programming, such as in operating systems.",
6-
"version": 3,
75
"status": {
86
"concept_exercises": false,
97
"test_runner": true,
108
"representer": false,
119
"analyzer": false
1210
},
11+
"blurb": "C is a small, general-purpose, imperative programming language with a static type system, scopes, and structures. It's typically used as an alternative to assembly programming, such as in operating systems.",
12+
"version": 3,
1313
"online_editor": {
1414
"indent_style": "space",
1515
"indent_size": 4,
@@ -36,7 +36,6 @@
3636
]
3737
},
3838
"exercises": {
39-
"concept": [],
4039
"practice": [
4140
{
4241
"slug": "hello-world",
@@ -1015,6 +1014,14 @@
10151014
"performance_optimizations",
10161015
"strings"
10171016
]
1017+
},
1018+
{
1019+
"slug": "kindergarten-garden",
1020+
"name": "Kindergarten Garden",
1021+
"uuid": "9484317e-d638-4714-afba-2a10baccff16",
1022+
"practices": [],
1023+
"prerequisites": [],
1024+
"difficulty": 1
10181025
}
10191026
]
10201027
},
@@ -1024,7 +1031,7 @@
10241031
"slug": "basics",
10251032
"name": "Basics"
10261033
},
1027-
{
1034+
{
10281035
"uuid": "20948c69-20f7-498d-b743-fada00e5c79d",
10291036
"slug": "bits",
10301037
"name": "Bits"
@@ -1043,7 +1050,7 @@
10431050
"uuid": "1721552b-9c55-432d-a776-05e618c25bb4",
10441051
"slug": "function-pointers",
10451052
"name": "Function Pointers"
1046-
},
1053+
},
10471054
{
10481055
"uuid": "e5725455-3ce3-48e9-a3c6-ceb522434b5b",
10491056
"slug": "conditionals",
@@ -1058,7 +1065,7 @@
10581065
"uuid": "e39f5003-8ed9-4d82-ab9b-f79bbca78679",
10591066
"slug": "linkage",
10601067
"name": "Linkage"
1061-
},
1068+
},
10621069
{
10631070
"uuid": "3d79388b-e909-4e1b-99e9-d28e30e54fde",
10641071
"slug": "loops",
@@ -1083,7 +1090,7 @@
10831090
"uuid": "f4946fe8-afb3-4110-8fca-b1476f19be6f",
10841091
"slug": "static-functions",
10851092
"name": "Static Functions"
1086-
},
1093+
},
10871094
{
10881095
"uuid": "c065ddca-627c-45b6-a92f-7144dd852111",
10891096
"slug": "storage-class-specifiers",
@@ -1092,46 +1099,46 @@
10921099
],
10931100
"key_features": [
10941101
{
1095-
"icon": "cross-platform",
10961102
"title": "Cross-platform",
1097-
"content": "C is portable and can run on almost any platform and chipset."
1103+
"content": "C is portable and can run on almost any platform and chipset.",
1104+
"icon": "cross-platform"
10981105
},
10991106
{
1100-
"icon": "general-purpose",
11011107
"title": "General purpose",
1102-
"content": "Can be used for a wide variety of workloads from IoT to game engines."
1108+
"content": "Can be used for a wide variety of workloads from IoT to game engines.",
1109+
"icon": "general-purpose"
11031110
},
11041111
{
1105-
"icon": "fast",
11061112
"title": "Fast",
1107-
"content": "Consistently rated as one of the highest performing languages for energy and time."
1113+
"content": "Consistently rated as one of the highest performing languages for energy and time.",
1114+
"icon": "fast"
11081115
},
11091116
{
1110-
"icon": "stable",
11111117
"title": "Stable",
1112-
"content": "The C standard values stability and backwards compatibility, C programs can run for decades."
1118+
"content": "The C standard values stability and backwards compatibility, C programs can run for decades.",
1119+
"icon": "stable"
11131120
},
11141121
{
1115-
"icon": "powerful",
11161122
"title": "Powerful",
1117-
"content": "Allows both lower and higher level functionality, making it very powerful."
1123+
"content": "Allows both lower and higher level functionality, making it very powerful.",
1124+
"icon": "powerful"
11181125
},
11191126
{
1120-
"icon": "widely-used",
11211127
"title": "Widely used",
1122-
"content": "Consistently one of the most popular languages for everything from operating systems to firmware"
1128+
"content": "Consistently one of the most popular languages for everything from operating systems to firmware",
1129+
"icon": "widely-used"
11231130
}
11241131
],
11251132
"tags": [
11261133
"execution_mode/compiled",
11271134
"paradigm/procedural",
1128-
"typing/static",
11291135
"platform/android",
11301136
"platform/ios",
11311137
"platform/linux",
11321138
"platform/mac",
11331139
"platform/windows",
11341140
"runtime/language_specific",
1141+
"typing/static",
11351142
"used_for/backends",
11361143
"used_for/cross_platform_development",
11371144
"used_for/embedded_systems",
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"authors": [
3+
"ahans"
4+
],
5+
"files": {
6+
"solution": [
7+
"kindergarten_garden.c",
8+
"kindergarten_garden.h"
9+
],
10+
"test": [
11+
"test_kindergarten_garden.c"
12+
],
13+
"example": [
14+
".meta/example.c",
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "kindergarten_garden.h"
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
static int student_to_index(const char *student)
8+
{
9+
static const char *students[] = { "Alice", "Bob", "Charlie", "David",
10+
"Eve", "Fred", "Ginny", "Harriet",
11+
"Ileana", "Joseph", "Kincaid", "Larry" };
12+
for (size_t i = 0; i < sizeof(students); ++i) {
13+
if (strcmp(students[i], student) == 0) {
14+
return i;
15+
}
16+
}
17+
printf("error: student %s not found\n", student);
18+
exit(1);
19+
}
20+
21+
static plant_t letter_to_plant(char letter)
22+
{
23+
switch (letter) {
24+
case 'C':
25+
return CLOVER;
26+
case 'G':
27+
return GRASS;
28+
case 'R':
29+
return RADISHES;
30+
case 'V':
31+
return VIOLETS;
32+
}
33+
printf("error: letter %c does not match any plant\n", letter);
34+
exit(1);
35+
}
36+
37+
void plants(const char *diagram, const char *student, plant_t *plants)
38+
{
39+
const char *rows[] = { diagram, strchr(diagram, '\n') + 1 };
40+
const size_t student_col = student_to_index(student) * 2;
41+
for (int i = 0; i < 4; ++i) {
42+
const size_t row_index = i / 2;
43+
const size_t col_index = student_col + i % 2;
44+
plants[i] = letter_to_plant(rows[row_index][col_index]);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef KINDERGARTEN_GARDEN_H
2+
#define KINDERGARTEN_GARDEN_H
3+
4+
typedef enum { CLOVER = 0, GRASS = 1, RADISHES = 2, VIOLETS = 3 } plant_t;
5+
6+
void plants(const char *diagram, const char *student, plant_t *plants);
7+
8+
#endif
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,46 @@
1+
#include "kindergarten_garden.h"
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
static int student_to_index(const char *student)
8+
{
9+
static const char *students[] = { "Alice", "Bob", "Charlie", "David",
10+
"Eve", "Fred", "Ginny", "Harriet",
11+
"Ileana", "Joseph", "Kincaid", "Larry" };
12+
for (size_t i = 0; i < sizeof(students); ++i) {
13+
if (strcmp(students[i], student) == 0) {
14+
return i;
15+
}
16+
}
17+
printf("error: student %s not found\n", student);
18+
exit(1);
19+
}
20+
21+
static plant_t letter_to_plant(char letter)
22+
{
23+
switch (letter) {
24+
case 'C':
25+
return CLOVER;
26+
case 'G':
27+
return GRASS;
28+
case 'R':
29+
return RADISHES;
30+
case 'V':
31+
return VIOLETS;
32+
}
33+
printf("error: letter %c does not match any plant\n", letter);
34+
exit(1);
35+
}
36+
37+
void plants(const char *diagram, const char *student, plant_t *plants)
38+
{
39+
const char *rows[] = { diagram, strchr(diagram, '\n') + 1 };
40+
const size_t student_col = student_to_index(student) * 2;
41+
for (int i = 0; i < 4; ++i) {
42+
const size_t row_index = i / 2;
43+
const size_t col_index = student_col + i % 2;
44+
plants[i] = letter_to_plant(rows[row_index][col_index]);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef KINDERGARTEN_GARDEN_H
2+
#define KINDERGARTEN_GARDEN_H
3+
4+
typedef enum { CLOVER = 0, GRASS = 1, RADISHES = 2, VIOLETS = 3 } plant_t;
5+
6+
#endif

0 commit comments

Comments
 (0)