From 3fde7d6750400a73372c946a65bd5f69815bff8d Mon Sep 17 00:00:00 2001 From: Nenad Date: Fri, 4 Oct 2024 11:20:45 +0200 Subject: [PATCH 1/7] Init impl --- config.json | 8 + .../kindergarten-garden/.docs/instructions.md | 56 ++++++ .../kindergarten-garden/.docs/introduction.md | 6 + .../kindergarten-garden/.meta/config.json | 22 +++ .../kindergarten-garden/.meta/example.cairo | 76 ++++++++ .../kindergarten-garden/.meta/tests.toml | 61 +++++++ .../practice/kindergarten-garden/Scarb.toml | 7 + .../kindergarten-garden/src/lib.cairo | 3 + .../tests/kindergarten_garden.cairo | 170 ++++++++++++++++++ 9 files changed, 409 insertions(+) create mode 100644 exercises/practice/kindergarten-garden/.docs/instructions.md create mode 100644 exercises/practice/kindergarten-garden/.docs/introduction.md create mode 100644 exercises/practice/kindergarten-garden/.meta/config.json create mode 100644 exercises/practice/kindergarten-garden/.meta/example.cairo create mode 100644 exercises/practice/kindergarten-garden/.meta/tests.toml create mode 100644 exercises/practice/kindergarten-garden/Scarb.toml create mode 100644 exercises/practice/kindergarten-garden/src/lib.cairo create mode 100644 exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo diff --git a/config.json b/config.json index a0096a09..b166a130 100644 --- a/config.json +++ b/config.json @@ -779,6 +779,14 @@ "practices": [], "prerequisites": [], "difficulty": 6 + }, + { + "slug": "kindergarten-garden", + "name": "Kindergarten Garden", + "uuid": "6e241be1-28ba-4658-a6de-a29f3471725d", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ], "foregone": [ diff --git a/exercises/practice/kindergarten-garden/.docs/instructions.md b/exercises/practice/kindergarten-garden/.docs/instructions.md new file mode 100644 index 00000000..6fe11a58 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.docs/instructions.md @@ -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 diff --git a/exercises/practice/kindergarten-garden/.docs/introduction.md b/exercises/practice/kindergarten-garden/.docs/introduction.md new file mode 100644 index 00000000..5ad97d23 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.docs/introduction.md @@ -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. diff --git a/exercises/practice/kindergarten-garden/.meta/config.json b/exercises/practice/kindergarten-garden/.meta/config.json new file mode 100644 index 00000000..cfbeaff5 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/config.json @@ -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" +} diff --git a/exercises/practice/kindergarten-garden/.meta/example.cairo b/exercises/practice/kindergarten-garden/.meta/example.cairo new file mode 100644 index 00000000..09e3e48f --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/example.cairo @@ -0,0 +1,76 @@ +/// Function to determine the index of a student in the list +fn get_student_index(student: ByteArray) -> Option { + let student_names: Array = array![ + "Alice", + "Bob", + "Charlie", + "David", + "Eve", + "Fred", + "Ginny", + "Harriet", + "Ileana", + "Joseph", + "Kincaid", + "Larry", + ]; + + let mut index: Option = Option::None; + for i in 0 + ..student_names + .len() { + if @student == student_names[i] { + index = Option::Some(i); + break; + } + }; + + index +} + +/// Get string slice +fn slice(diagram: @ByteArray, from: usize, length: usize) -> ByteArray { + let mut result = ""; + + for i in from..(from + length) { + result.append_byte(diagram[i]); + }; + + result +} + +/// 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 { + let index = get_student_index(student).expect('Unexpected student name'); + + let start = index * 2; + + // Split the diagram into rows + let row1 = slice(@diagram, 0, diagram.len() / 2); + let row2 = slice(@diagram, diagram.len() / 2, diagram.len() / 2); + + // Retrieve the plants for the student based on the index + let plant1 = plant_from_char(row1[start]); + let plant2 = plant_from_char(row1[start + 1]); + let plant3 = plant_from_char(row2[start]); + let plant4 = plant_from_char(row2[start + 1]); + + // Return an array with the student's plants + let mut result = array![plant1, plant2, plant3, plant4,]; + result +} diff --git a/exercises/practice/kindergarten-garden/.meta/tests.toml b/exercises/practice/kindergarten-garden/.meta/tests.toml new file mode 100644 index 00000000..0cdd9ad6 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/tests.toml @@ -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" diff --git a/exercises/practice/kindergarten-garden/Scarb.toml b/exercises/practice/kindergarten-garden/Scarb.toml new file mode 100644 index 00000000..01b6a1ff --- /dev/null +++ b/exercises/practice/kindergarten-garden/Scarb.toml @@ -0,0 +1,7 @@ +[package] +name = "kindergarten_garden" +version = "0.1.0" +edition = "2024_07" + +[dev-dependencies] +cairo_test = "2.8.2" diff --git a/exercises/practice/kindergarten-garden/src/lib.cairo b/exercises/practice/kindergarten-garden/src/lib.cairo new file mode 100644 index 00000000..3a2c71f6 --- /dev/null +++ b/exercises/practice/kindergarten-garden/src/lib.cairo @@ -0,0 +1,3 @@ +pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { + panic!("implement `plants`") +} diff --git a/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo b/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo new file mode 100644 index 00000000..bac45958 --- /dev/null +++ b/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo @@ -0,0 +1,170 @@ +use kindergarten_garden::plants; + +#[test] +fn garden_with_single_student() { + let diagram = "RC +GG"; + let student = "Alice"; + let expected = array!["radishes", "clover", "grass", "grass"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn different_garden_with_single_student() { + let diagram = "VC +RC"; + let student = "Alice"; + let expected = array!["violets", "clover", "radishes", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn garden_with_two_students() { + let diagram = "VVCG +VVRC"; + let student = "Bob"; + let expected = array!["clover", "grass", "radishes", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn second_students_garden() { + let diagram = "VVCCGG +VVCCGG"; + let student = "Bob"; + let expected = array!["clover", "clover", "clover", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn third_students_garden() { + let diagram = "VVCCGG +VVCCGG"; + let student = "Charlie"; + let expected = array!["grass", "grass", "grass", "grass"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_alice_first_students_garden() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Alice"; + let expected = array!["violets", "radishes", "violets", "radishes"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_bob_second_students_garden() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Bob"; + let expected = array!["clover", "grass", "clover", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_charlie() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Charlie"; + let expected = array!["violets", "violets", "clover", "grass"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_david() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "David"; + let expected = array!["radishes", "violets", "clover", "radishes"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_eve() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Eve"; + let expected = array!["clover", "grass", "radishes", "grass"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_fred() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Fred"; + let expected = array!["grass", "clover", "violets", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_ginny() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Ginny"; + let expected = array!["clover", "grass", "grass", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_harriet() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Harriet"; + let expected = array!["violets", "radishes", "radishes", "violets"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_ileana() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Ileana"; + let expected = array!["grass", "clover", "violets", "clover"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_joseph() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Joseph"; + let expected = array!["violets", "clover", "violets", "grass"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_kincaid_second_to_last_students_garden() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Kincaid"; + let expected = array!["grass", "clover", "clover", "grass"]; + assert_eq!(plants(diagram, student), expected); +} + +#[test] +#[ignore] +fn for_larry_last_students_garden() { + let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV"; + let student = "Larry"; + let expected = array!["grass", "violets", "clover", "violets"]; + assert_eq!(plants(diagram, student), expected); +} From df4d67750da76ea61047841f612f9cf82a47f264 Mon Sep 17 00:00:00 2001 From: Nenad Date: Fri, 4 Oct 2024 12:03:03 +0200 Subject: [PATCH 2/7] Fix line reading --- .../kindergarten-garden/.meta/example.cairo | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/exercises/practice/kindergarten-garden/.meta/example.cairo b/exercises/practice/kindergarten-garden/.meta/example.cairo index 09e3e48f..8e508e1e 100644 --- a/exercises/practice/kindergarten-garden/.meta/example.cairo +++ b/exercises/practice/kindergarten-garden/.meta/example.cairo @@ -29,14 +29,22 @@ fn get_student_index(student: ByteArray) -> Option { } /// Get string slice -fn slice(diagram: @ByteArray, from: usize, length: usize) -> ByteArray { - let mut result = ""; +fn lines(diagram: @ByteArray) -> [ByteArray; 2] { + let mut line1 = ""; + let mut line2 = ""; - for i in from..(from + length) { - result.append_byte(diagram[i]); + 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]); }; - result + [line1, line2] } /// Mapping plant characters to plant names @@ -56,21 +64,16 @@ fn plant_from_char(c: u8) -> ByteArray { /// Function to retrieve the plants for a given student based on the diagram pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { - let index = get_student_index(student).expect('Unexpected student name'); - - let start = index * 2; + let index = get_student_index(student).unwrap(); - // Split the diagram into rows - let row1 = slice(@diagram, 0, diagram.len() / 2); - let row2 = slice(@diagram, diagram.len() / 2, diagram.len() / 2); + let [line1, line2] = lines(@diagram); // Retrieve the plants for the student based on the index - let plant1 = plant_from_char(row1[start]); - let plant2 = plant_from_char(row1[start + 1]); - let plant3 = plant_from_char(row2[start]); - let plant4 = plant_from_char(row2[start + 1]); + 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]); - // Return an array with the student's plants - let mut result = array![plant1, plant2, plant3, plant4,]; - result + array![plant1, plant2, plant3, plant4,] } From c095f18f6044d2e851f2d5faf0306abde92a99cf Mon Sep 17 00:00:00 2001 From: Nenad Date: Fri, 4 Oct 2024 12:03:31 +0200 Subject: [PATCH 3/7] Set difficulty to 5 --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index b166a130..c9d95ffb 100644 --- a/config.json +++ b/config.json @@ -786,7 +786,7 @@ "uuid": "6e241be1-28ba-4658-a6de-a29f3471725d", "practices": [], "prerequisites": [], - "difficulty": 1 + "difficulty": 5 } ], "foregone": [ From e047dc062c12b0e73b481ae50669b54c3416a46c Mon Sep 17 00:00:00 2001 From: Nenad Date: Sat, 5 Oct 2024 18:18:13 +0200 Subject: [PATCH 4/7] Update config.json: Add missing opening bracket --- config.json | 1 + 1 file changed, 1 insertion(+) diff --git a/config.json b/config.json index 48987bd7..69f8497e 100644 --- a/config.json +++ b/config.json @@ -796,6 +796,7 @@ "prerequisites": [], "difficulty": 5 }, + { "slug": "luhn", "name": "Luhn", "uuid": "8b85fe9d-0964-453f-85d7-92acd2c81723", From 731d640282d1179787df9f535f03e9b5e54a5120 Mon Sep 17 00:00:00 2001 From: Nenad Date: Tue, 8 Oct 2024 08:42:04 +0200 Subject: [PATCH 5/7] Extract plants into an enum --- .../kindergarten-garden/.meta/example.cairo | 20 +++++++---- .../kindergarten-garden/src/lib.cairo | 10 +++++- .../tests/kindergarten_garden.cairo | 36 +++++++++---------- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/exercises/practice/kindergarten-garden/.meta/example.cairo b/exercises/practice/kindergarten-garden/.meta/example.cairo index 8e508e1e..bf1a5255 100644 --- a/exercises/practice/kindergarten-garden/.meta/example.cairo +++ b/exercises/practice/kindergarten-garden/.meta/example.cairo @@ -1,3 +1,11 @@ +#[derive(Drop, Debug, PartialEq)] +pub enum Plant { + Radishes, + Clover, + Grass, + Violets +} + /// Function to determine the index of a student in the list fn get_student_index(student: ByteArray) -> Option { let student_names: Array = array![ @@ -48,22 +56,22 @@ fn lines(diagram: @ByteArray) -> [ByteArray; 2] { } /// Mapping plant characters to plant names -fn plant_from_char(c: u8) -> ByteArray { +fn plant_from_char(c: u8) -> Plant { if c == 'R' { - "radishes" + Plant::Radishes } else if c == 'C' { - "clover" + Plant::Clover } else if c == 'G' { - "grass" + Plant::Grass } else if c == 'V' { - "violets" + Plant::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 { +pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { let index = get_student_index(student).unwrap(); let [line1, line2] = lines(@diagram); diff --git a/exercises/practice/kindergarten-garden/src/lib.cairo b/exercises/practice/kindergarten-garden/src/lib.cairo index 3a2c71f6..46e54be6 100644 --- a/exercises/practice/kindergarten-garden/src/lib.cairo +++ b/exercises/practice/kindergarten-garden/src/lib.cairo @@ -1,3 +1,11 @@ -pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { +#[derive(Drop, Debug, PartialEq)] +pub enum Plant { + Radishes, + Clover, + Grass, + Violets +} + +pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { panic!("implement `plants`") } diff --git a/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo b/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo index bac45958..e4376a1e 100644 --- a/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo +++ b/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo @@ -1,11 +1,11 @@ -use kindergarten_garden::plants; +use kindergarten_garden::{plants, Plant}; #[test] fn garden_with_single_student() { let diagram = "RC GG"; let student = "Alice"; - let expected = array!["radishes", "clover", "grass", "grass"]; + let expected = array![Plant::Radishes, Plant::Clover, Plant::Grass, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -15,7 +15,7 @@ fn different_garden_with_single_student() { let diagram = "VC RC"; let student = "Alice"; - let expected = array!["violets", "clover", "radishes", "clover"]; + let expected = array![Plant::Violets, Plant::Clover, Plant::Radishes, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -25,7 +25,7 @@ fn garden_with_two_students() { let diagram = "VVCG VVRC"; let student = "Bob"; - let expected = array!["clover", "grass", "radishes", "clover"]; + let expected = array![Plant::Clover, Plant::Grass, Plant::Radishes, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -35,7 +35,7 @@ fn second_students_garden() { let diagram = "VVCCGG VVCCGG"; let student = "Bob"; - let expected = array!["clover", "clover", "clover", "clover"]; + let expected = array![Plant::Clover, Plant::Clover, Plant::Clover, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -45,7 +45,7 @@ fn third_students_garden() { let diagram = "VVCCGG VVCCGG"; let student = "Charlie"; - let expected = array!["grass", "grass", "grass", "grass"]; + let expected = array![Plant::Grass, Plant::Grass, Plant::Grass, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -55,7 +55,7 @@ fn for_alice_first_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Alice"; - let expected = array!["violets", "radishes", "violets", "radishes"]; + let expected = array![Plant::Violets, Plant::Radishes, Plant::Violets, Plant::Radishes]; assert_eq!(plants(diagram, student), expected); } @@ -65,7 +65,7 @@ fn for_bob_second_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Bob"; - let expected = array!["clover", "grass", "clover", "clover"]; + let expected = array![Plant::Clover, Plant::Grass, Plant::Clover, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -75,7 +75,7 @@ fn for_charlie() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Charlie"; - let expected = array!["violets", "violets", "clover", "grass"]; + let expected = array![Plant::Violets, Plant::Violets, Plant::Clover, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -85,7 +85,7 @@ fn for_david() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "David"; - let expected = array!["radishes", "violets", "clover", "radishes"]; + let expected = array![Plant::Radishes, Plant::Violets, Plant::Clover, Plant::Radishes]; assert_eq!(plants(diagram, student), expected); } @@ -95,7 +95,7 @@ fn for_eve() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Eve"; - let expected = array!["clover", "grass", "radishes", "grass"]; + let expected = array![Plant::Clover, Plant::Grass, Plant::Radishes, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -105,7 +105,7 @@ fn for_fred() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Fred"; - let expected = array!["grass", "clover", "violets", "clover"]; + let expected = array![Plant::Grass, Plant::Clover, Plant::Violets, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -115,7 +115,7 @@ fn for_ginny() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Ginny"; - let expected = array!["clover", "grass", "grass", "clover"]; + let expected = array![Plant::Clover, Plant::Grass, Plant::Grass, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -125,7 +125,7 @@ fn for_harriet() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Harriet"; - let expected = array!["violets", "radishes", "radishes", "violets"]; + let expected = array![Plant::Violets, Plant::Radishes, Plant::Radishes, Plant::Violets]; assert_eq!(plants(diagram, student), expected); } @@ -135,7 +135,7 @@ fn for_ileana() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Ileana"; - let expected = array!["grass", "clover", "violets", "clover"]; + let expected = array![Plant::Grass, Plant::Clover, Plant::Violets, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -145,7 +145,7 @@ fn for_joseph() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Joseph"; - let expected = array!["violets", "clover", "violets", "grass"]; + let expected = array![Plant::Violets, Plant::Clover, Plant::Violets, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -155,7 +155,7 @@ fn for_kincaid_second_to_last_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Kincaid"; - let expected = array!["grass", "clover", "clover", "grass"]; + let expected = array![Plant::Grass, Plant::Clover, Plant::Clover, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -165,6 +165,6 @@ fn for_larry_last_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; let student = "Larry"; - let expected = array!["grass", "violets", "clover", "violets"]; + let expected = array![Plant::Grass, Plant::Violets, Plant::Clover, Plant::Violets]; assert_eq!(plants(diagram, student), expected); } From 6eb635e85b0a0f277ba6c6c074a17350ae7c7231 Mon Sep 17 00:00:00 2001 From: Nenad Date: Tue, 8 Oct 2024 08:47:36 +0200 Subject: [PATCH 6/7] Extract students into an enum --- .../kindergarten-garden/.meta/example.cairo | 62 ++++++++++--------- .../kindergarten-garden/src/lib.cairo | 18 +++++- .../tests/kindergarten_garden.cairo | 36 +++++------ 3 files changed, 68 insertions(+), 48 deletions(-) diff --git a/exercises/practice/kindergarten-garden/.meta/example.cairo b/exercises/practice/kindergarten-garden/.meta/example.cairo index bf1a5255..0784baef 100644 --- a/exercises/practice/kindergarten-garden/.meta/example.cairo +++ b/exercises/practice/kindergarten-garden/.meta/example.cairo @@ -6,34 +6,38 @@ pub enum Plant { Violets } -/// Function to determine the index of a student in the list -fn get_student_index(student: ByteArray) -> Option { - let student_names: Array = array![ - "Alice", - "Bob", - "Charlie", - "David", - "Eve", - "Fred", - "Ginny", - "Harriet", - "Ileana", - "Joseph", - "Kincaid", - "Larry", - ]; - - let mut index: Option = Option::None; - for i in 0 - ..student_names - .len() { - if @student == student_names[i] { - index = Option::Some(i); - break; - } - }; +#[derive(Drop)] +pub enum Student { + Alice, + Bob, + Charlie, + David, + Eve, + Fred, + Ginny, + Harriet, + Ileana, + Joseph, + Kincaid, + Larry, +} - index +/// Function to determine the index of a student in the list +fn get_student_index(student: Student) -> usize { + match student { + Student::Alice => 0, + Student::Bob => 1, + Student::Charlie => 2, + Student::David => 3, + Student::Eve => 4, + Student::Fred => 5, + Student::Ginny => 6, + Student::Harriet => 7, + Student::Ileana => 8, + Student::Joseph => 9, + Student::Kincaid => 10, + Student::Larry => 11, + } } /// Get string slice @@ -71,8 +75,8 @@ fn plant_from_char(c: u8) -> Plant { } /// Function to retrieve the plants for a given student based on the diagram -pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { - let index = get_student_index(student).unwrap(); +pub fn plants(diagram: ByteArray, student: Student) -> Array { + let index = get_student_index(student); let [line1, line2] = lines(@diagram); diff --git a/exercises/practice/kindergarten-garden/src/lib.cairo b/exercises/practice/kindergarten-garden/src/lib.cairo index 46e54be6..5aa9d38e 100644 --- a/exercises/practice/kindergarten-garden/src/lib.cairo +++ b/exercises/practice/kindergarten-garden/src/lib.cairo @@ -6,6 +6,22 @@ pub enum Plant { Violets } -pub fn plants(diagram: ByteArray, student: ByteArray) -> Array { +#[derive(Drop)] +pub enum Student { + Alice, + Bob, + Charlie, + David, + Eve, + Fred, + Ginny, + Harriet, + Ileana, + Joseph, + Kincaid, + Larry, +} + +pub fn plants(diagram: ByteArray, student: Student) -> Array { panic!("implement `plants`") } diff --git a/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo b/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo index e4376a1e..52f15781 100644 --- a/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo +++ b/exercises/practice/kindergarten-garden/tests/kindergarten_garden.cairo @@ -1,10 +1,10 @@ -use kindergarten_garden::{plants, Plant}; +use kindergarten_garden::{plants, Plant, Student}; #[test] fn garden_with_single_student() { let diagram = "RC GG"; - let student = "Alice"; + let student = Student::Alice; let expected = array![Plant::Radishes, Plant::Clover, Plant::Grass, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -14,7 +14,7 @@ GG"; fn different_garden_with_single_student() { let diagram = "VC RC"; - let student = "Alice"; + let student = Student::Alice; let expected = array![Plant::Violets, Plant::Clover, Plant::Radishes, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -24,7 +24,7 @@ RC"; fn garden_with_two_students() { let diagram = "VVCG VVRC"; - let student = "Bob"; + let student = Student::Bob; let expected = array![Plant::Clover, Plant::Grass, Plant::Radishes, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -34,7 +34,7 @@ VVRC"; fn second_students_garden() { let diagram = "VVCCGG VVCCGG"; - let student = "Bob"; + let student = Student::Bob; let expected = array![Plant::Clover, Plant::Clover, Plant::Clover, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -44,7 +44,7 @@ VVCCGG"; fn third_students_garden() { let diagram = "VVCCGG VVCCGG"; - let student = "Charlie"; + let student = Student::Charlie; let expected = array![Plant::Grass, Plant::Grass, Plant::Grass, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -54,7 +54,7 @@ VVCCGG"; fn for_alice_first_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Alice"; + let student = Student::Alice; let expected = array![Plant::Violets, Plant::Radishes, Plant::Violets, Plant::Radishes]; assert_eq!(plants(diagram, student), expected); } @@ -64,7 +64,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_bob_second_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Bob"; + let student = Student::Bob; let expected = array![Plant::Clover, Plant::Grass, Plant::Clover, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -74,7 +74,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_charlie() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Charlie"; + let student = Student::Charlie; let expected = array![Plant::Violets, Plant::Violets, Plant::Clover, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -84,7 +84,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_david() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "David"; + let student = Student::David; let expected = array![Plant::Radishes, Plant::Violets, Plant::Clover, Plant::Radishes]; assert_eq!(plants(diagram, student), expected); } @@ -94,7 +94,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_eve() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Eve"; + let student = Student::Eve; let expected = array![Plant::Clover, Plant::Grass, Plant::Radishes, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -104,7 +104,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_fred() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Fred"; + let student = Student::Fred; let expected = array![Plant::Grass, Plant::Clover, Plant::Violets, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -114,7 +114,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_ginny() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Ginny"; + let student = Student::Ginny; let expected = array![Plant::Clover, Plant::Grass, Plant::Grass, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -124,7 +124,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_harriet() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Harriet"; + let student = Student::Harriet; let expected = array![Plant::Violets, Plant::Radishes, Plant::Radishes, Plant::Violets]; assert_eq!(plants(diagram, student), expected); } @@ -134,7 +134,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_ileana() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Ileana"; + let student = Student::Ileana; let expected = array![Plant::Grass, Plant::Clover, Plant::Violets, Plant::Clover]; assert_eq!(plants(diagram, student), expected); } @@ -144,7 +144,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_joseph() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Joseph"; + let student = Student::Joseph; let expected = array![Plant::Violets, Plant::Clover, Plant::Violets, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -154,7 +154,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_kincaid_second_to_last_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Kincaid"; + let student = Student::Kincaid; let expected = array![Plant::Grass, Plant::Clover, Plant::Clover, Plant::Grass]; assert_eq!(plants(diagram, student), expected); } @@ -164,7 +164,7 @@ VRCCCGCRRGVCGCRVVCVGCGCV"; fn for_larry_last_students_garden() { let diagram = "VRCGVVRVCGGCCGVRGCVCGCGV VRCCCGCRRGVCGCRVVCVGCGCV"; - let student = "Larry"; + let student = Student::Larry; let expected = array![Plant::Grass, Plant::Violets, Plant::Clover, Plant::Violets]; assert_eq!(plants(diagram, student), expected); } From 680d9b5dd2fb7a9d0a6e8e830c6564b3b7722e95 Mon Sep 17 00:00:00 2001 From: Nenad Date: Tue, 8 Oct 2024 08:52:00 +0200 Subject: [PATCH 7/7] Reduce difficulty --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index 69f8497e..3f4dffba 100644 --- a/config.json +++ b/config.json @@ -794,7 +794,7 @@ "uuid": "6e241be1-28ba-4658-a6de-a29f3471725d", "practices": [], "prerequisites": [], - "difficulty": 5 + "difficulty": 4 }, { "slug": "luhn",