Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kindergarten-garden (#940) #943

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"language": "C",
"slug": "c",
"active": true,
"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.",
"version": 3,
"status": {
"concept_exercises": false,
"test_runner": true,
"representer": false,
"analyzer": false
},
"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.",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bin/configlet create made those extra changes. Not sure we want to keep them as part of this PR. I can for sure revert them and only keep the addition of the entry for this exercise.

"version": 3,
"online_editor": {
"indent_style": "space",
"indent_size": 4,
Expand All @@ -36,7 +36,6 @@
]
},
"exercises": {
"concept": [],
"practice": [
{
"slug": "hello-world",
Expand Down Expand Up @@ -1015,6 +1014,19 @@
"performance_optimizations",
"strings"
]
},
{
"slug": "kindergarten-garden",
"name": "Kindergarten Garden",
"uuid": "9484317e-d638-4714-afba-2a10baccff16",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"topics": [
"arrays",
"control_flow_loops",
"strings"
]
}
]
},
Expand All @@ -1024,7 +1036,7 @@
"slug": "basics",
"name": "Basics"
},
{
{
"uuid": "20948c69-20f7-498d-b743-fada00e5c79d",
"slug": "bits",
"name": "Bits"
Expand All @@ -1043,7 +1055,7 @@
"uuid": "1721552b-9c55-432d-a776-05e618c25bb4",
"slug": "function-pointers",
"name": "Function Pointers"
},
},
{
"uuid": "e5725455-3ce3-48e9-a3c6-ceb522434b5b",
"slug": "conditionals",
Expand All @@ -1058,7 +1070,7 @@
"uuid": "e39f5003-8ed9-4d82-ab9b-f79bbca78679",
"slug": "linkage",
"name": "Linkage"
},
},
{
"uuid": "3d79388b-e909-4e1b-99e9-d28e30e54fde",
"slug": "loops",
Expand All @@ -1083,7 +1095,7 @@
"uuid": "f4946fe8-afb3-4110-8fca-b1476f19be6f",
"slug": "static-functions",
"name": "Static Functions"
},
},
{
"uuid": "c065ddca-627c-45b6-a92f-7144dd852111",
"slug": "storage-class-specifiers",
Expand All @@ -1092,46 +1104,46 @@
],
"key_features": [
{
"icon": "cross-platform",
"title": "Cross-platform",
"content": "C is portable and can run on almost any platform and chipset."
"content": "C is portable and can run on almost any platform and chipset.",
"icon": "cross-platform"
},
{
"icon": "general-purpose",
"title": "General purpose",
"content": "Can be used for a wide variety of workloads from IoT to game engines."
"content": "Can be used for a wide variety of workloads from IoT to game engines.",
"icon": "general-purpose"
},
{
"icon": "fast",
"title": "Fast",
"content": "Consistently rated as one of the highest performing languages for energy and time."
"content": "Consistently rated as one of the highest performing languages for energy and time.",
"icon": "fast"
},
{
"icon": "stable",
"title": "Stable",
"content": "The C standard values stability and backwards compatibility, C programs can run for decades."
"content": "The C standard values stability and backwards compatibility, C programs can run for decades.",
"icon": "stable"
},
{
"icon": "powerful",
"title": "Powerful",
"content": "Allows both lower and higher level functionality, making it very powerful."
"content": "Allows both lower and higher level functionality, making it very powerful.",
"icon": "powerful"
},
{
"icon": "widely-used",
"title": "Widely used",
"content": "Consistently one of the most popular languages for everything from operating systems to firmware"
"content": "Consistently one of the most popular languages for everything from operating systems to firmware",
"icon": "widely-used"
}
],
"tags": [
"execution_mode/compiled",
"paradigm/procedural",
"typing/static",
"platform/android",
"platform/ios",
"platform/linux",
"platform/mac",
"platform/windows",
"runtime/language_specific",
"typing/static",
"used_for/backends",
"used_for/cross_platform_development",
"used_for/embedded_systems",
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
21 changes: 21 additions & 0 deletions exercises/practice/kindergarten-garden/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"ahans"
],
"files": {
"solution": [
"kindergarten_garden.c",
"kindergarten_garden.h"
],
"test": [
"test_kindergarten_garden.c"
],
"example": [
".meta/example.c",
".meta/example.h"
]
},
"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"
}
48 changes: 48 additions & 0 deletions exercises/practice/kindergarten-garden/.meta/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "kindergarten_garden.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int student_to_index(const char *student)
{
static const char *students[] = { "Alice", "Bob", "Charlie", "David",
"Eve", "Fred", "Ginny", "Harriet",
"Ileana", "Joseph", "Kincaid", "Larry" };
for (size_t i = 0; i < sizeof(students) / sizeof(students[0]); ++i) {
if (strcmp(students[i], student) == 0) {
return i;
}
}
printf("error: student %s not found\n", student);
exit(1);
}

static plant_t letter_to_plant(char letter)
{
switch (letter) {
case 'C':
return CLOVER;
case 'G':
return GRASS;
case 'R':
return RADISHES;
case 'V':
return VIOLETS;
}
printf("error: letter %c does not match any plant\n", letter);
exit(1);
}

plants_t plants(const char *diagram, const char *student)
{
const char *rows[] = { diagram, strchr(diagram, '\n') + 1 };
const size_t student_col = student_to_index(student) * 2;
plants_t res;
for (int i = 0; i < 4; ++i) {
const size_t row_index = i / 2;
const size_t col_index = student_col + i % 2;
res.plants[i] = letter_to_plant(rows[row_index][col_index]);
}
return res;
}
12 changes: 12 additions & 0 deletions exercises/practice/kindergarten-garden/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef KINDERGARTEN_GARDEN_H
#define KINDERGARTEN_GARDEN_H

typedef enum { CLOVER = 0, GRASS = 1, RADISHES = 2, VIOLETS = 3 } plant_t;

typedef struct {
plant_t plants[4];
} plants_t;

plants_t plants(const char *diagram, const char *student);

#endif
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 @@
#include "kindergarten_garden.h"
10 changes: 10 additions & 0 deletions exercises/practice/kindergarten-garden/kindergarten_garden.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef KINDERGARTEN_GARDEN_H
#define KINDERGARTEN_GARDEN_H

typedef enum { CLOVER = 0, GRASS = 1, RADISHES = 2, VIOLETS = 3 } plant_t;

typedef struct {
plant_t plants[4];
} plants_t;

#endif
37 changes: 37 additions & 0 deletions exercises/practice/kindergarten-garden/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### If you wish to use extra libraries (math.h for instance),
### add their flags here (-lm in our case) in the "LIBS" variable.

LIBS = -lm

###
CFLAGS = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror
CFLAGS += -Wmissing-declarations
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR

ASANFLAGS = -fsanitize=address
ASANFLAGS += -fno-common
ASANFLAGS += -fno-omit-frame-pointer

.PHONY: test
test: tests.out
@./tests.out

.PHONY: memcheck
memcheck: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS)
@./memcheck.out
@echo "Memory check passed"

.PHONY: clean
clean:
rm -rf *.o *.out *.out.dSYM

tests.out: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS)
Loading