-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
exercise
#294
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3fde7d6
Init impl
0xNeshi df4d677
Fix line reading
0xNeshi c095f18
Set difficulty to 5
0xNeshi 0c91fff
Merge branch 'main' into kindergarten-garden
0xNeshi e047dc0
Update config.json: Add missing opening bracket
0xNeshi 731d640
Extract plants into an enum
0xNeshi 6eb635e
Extract students into an enum
0xNeshi 680d9b5
Reduce difficulty
0xNeshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
exercises/practice/kindergarten-garden/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Instructions | ||
|
||
Your task is to, given a diagram, determine which plants each child in the kindergarten class is responsible for. | ||
|
||
There are 12 children in the class: | ||
|
||
- Alice, Bob, Charlie, David, Eve, Fred, Ginny, Harriet, Ileana, Joseph, Kincaid, and Larry. | ||
|
||
Four different types of seeds are planted: | ||
|
||
| Plant | Diagram encoding | | ||
| ------ | ---------------- | | ||
| Grass | G | | ||
| Clover | C | | ||
| Radish | R | | ||
| Violet | V | | ||
|
||
Each child gets four cups, two on each row: | ||
|
||
```text | ||
[window][window][window] | ||
........................ # each dot represents a cup | ||
........................ | ||
``` | ||
|
||
Their teacher assigns cups to the children alphabetically by their names, which means that Alice comes first and Larry comes last. | ||
|
||
Here is an example diagram representing 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Introduction | ||
|
||
The kindergarten class is learning about growing plants. | ||
The teacher thought it would be a good idea to give the class seeds to plant and grow in the dirt. | ||
To this end, the children have put little cups along the window sills and planted one type of plant in each cup. | ||
The children got to pick their favorites from four available types of seeds: grass, clover, radishes, and violets. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"authors": [ | ||
"0xNeshi" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/lib.cairo" | ||
], | ||
"test": [ | ||
"tests/kindergarten_garden.cairo" | ||
], | ||
"example": [ | ||
".meta/example.cairo" | ||
], | ||
"invalidator": [ | ||
"Scarb.toml" | ||
] | ||
}, | ||
"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" | ||
} |
79 changes: 79 additions & 0 deletions
79
exercises/practice/kindergarten-garden/.meta/example.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/// Function to determine the index of a student in the list | ||
fn get_student_index(student: ByteArray) -> Option<usize> { | ||
let student_names: Array<ByteArray> = array![ | ||
"Alice", | ||
"Bob", | ||
"Charlie", | ||
"David", | ||
"Eve", | ||
"Fred", | ||
"Ginny", | ||
"Harriet", | ||
"Ileana", | ||
"Joseph", | ||
"Kincaid", | ||
"Larry", | ||
]; | ||
|
||
let mut index: Option<usize> = Option::None; | ||
for i in 0 | ||
..student_names | ||
.len() { | ||
if @student == student_names[i] { | ||
index = Option::Some(i); | ||
break; | ||
} | ||
}; | ||
|
||
index | ||
} | ||
|
||
/// Get string slice | ||
fn lines(diagram: @ByteArray) -> [ByteArray; 2] { | ||
let mut line1 = ""; | ||
let mut line2 = ""; | ||
|
||
let mut i = 0; | ||
// everything before the newline char is line 1 | ||
while diagram[i] != '\n' { | ||
line1.append_byte(diagram[i]); | ||
i += 1; | ||
}; | ||
// `i` is at the newline char index, so everything after it is line 2 | ||
for i in (i + 1)..diagram.len() { | ||
line2.append_byte(diagram[i]); | ||
}; | ||
|
||
[line1, line2] | ||
} | ||
|
||
/// Mapping plant characters to plant names | ||
fn plant_from_char(c: u8) -> ByteArray { | ||
if c == 'R' { | ||
"radishes" | ||
} else if c == 'C' { | ||
"clover" | ||
} else if c == 'G' { | ||
"grass" | ||
} else if c == 'V' { | ||
"violets" | ||
} else { | ||
panic!("No such plant") | ||
} | ||
} | ||
|
||
/// Function to retrieve the plants for a given student based on the diagram | ||
pub fn plants(diagram: ByteArray, student: ByteArray) -> Array<ByteArray> { | ||
let index = get_student_index(student).unwrap(); | ||
|
||
let [line1, line2] = lines(@diagram); | ||
|
||
// Retrieve the plants for the student based on the index | ||
let start = index * 2; | ||
let plant1 = plant_from_char(line1[start]); | ||
let plant2 = plant_from_char(line1[start + 1]); | ||
let plant3 = plant_from_char(line2[start]); | ||
let plant4 = plant_from_char(line2[start + 1]); | ||
|
||
array![plant1, plant2, plant3, plant4,] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "kindergarten_garden" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
[dev-dependencies] | ||
cairo_test = "2.8.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn plants(diagram: ByteArray, student: ByteArray) -> Array<ByteArray> { | ||
panic!("implement `plants`") | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like an enum to represent the plants might be appropriate here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, I extracted students into an enum too, see 6eb635e