From 82bdb21ab0d01be3faf7d9f7358b96ccd9458907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 24 Jul 2024 19:46:23 -0700 Subject: [PATCH 1/3] Add grade-school --- config.json | 8 + .../grade-school/.docs/instructions.md | 21 ++ .../practice/grade-school/.meta/config.json | 18 ++ .../practice/grade-school/.meta/tests.toml | 86 ++++++++ exercises/practice/grade-school/dub.sdl | 2 + .../grade-school/example/grade_school.d | 42 ++++ .../grade-school/source/grade_school.d | 202 ++++++++++++++++++ 7 files changed, 379 insertions(+) create mode 100644 exercises/practice/grade-school/.docs/instructions.md create mode 100644 exercises/practice/grade-school/.meta/config.json create mode 100644 exercises/practice/grade-school/.meta/tests.toml create mode 100644 exercises/practice/grade-school/dub.sdl create mode 100644 exercises/practice/grade-school/example/grade_school.d create mode 100644 exercises/practice/grade-school/source/grade_school.d diff --git a/config.json b/config.json index 35b0448..a3b232b 100644 --- a/config.json +++ b/config.json @@ -546,6 +546,14 @@ "data_structures" ] }, + { + "slug": "grade-school", + "name": "Grade School", + "uuid": "8972fa0d-5d14-4703-8fe7-3a89ee8c0b7f", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "grains", "name": "Grains", diff --git a/exercises/practice/grade-school/.docs/instructions.md b/exercises/practice/grade-school/.docs/instructions.md new file mode 100644 index 0000000..9a63e39 --- /dev/null +++ b/exercises/practice/grade-school/.docs/instructions.md @@ -0,0 +1,21 @@ +# Instructions + +Given students' names along with the grade that they are in, create a roster for the school. + +In the end, you should be able to: + +- Add a student's name to the roster for a grade + - "Add Jim to grade 2." + - "OK." +- Get a list of all students enrolled in a grade + - "Which students are in grade 2?" + - "We've only got Jim just now." +- Get a sorted list of all students in all grades. + Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name. + - "Who all is enrolled in school right now?" + - "Let me think. + We have Anna, Barb, and Charlie in grade 1, Alex, Peter, and Zoe in grade 2 and Jim in grade 5. + So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim" + +Note that all our students only have one name (It's a small town, what do you want?) and each student cannot be added more than once to a grade or the roster. +In fact, when a test attempts to add the same student more than once, your implementation should indicate that this is incorrect. diff --git a/exercises/practice/grade-school/.meta/config.json b/exercises/practice/grade-school/.meta/config.json new file mode 100644 index 0000000..bd60515 --- /dev/null +++ b/exercises/practice/grade-school/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "source/grade_school.d" + ], + "test": [ + "source/grade_school.d" + ], + "example": [ + "example/grade_school.d" + ] + }, + "blurb": "Given students' names along with the grade that they are in, create a roster for the school.", + "source": "A pairing session with Phil Battos at gSchool" +} diff --git a/exercises/practice/grade-school/.meta/tests.toml b/exercises/practice/grade-school/.meta/tests.toml new file mode 100644 index 0000000..50c9e2e --- /dev/null +++ b/exercises/practice/grade-school/.meta/tests.toml @@ -0,0 +1,86 @@ +# 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. + +[a3f0fb58-f240-4723-8ddc-e644666b85cc] +description = "Roster is empty when no student is added" + +[9337267f-7793-4b90-9b4a-8e3978408824] +description = "Add a student" + +[6d0a30e4-1b4e-472e-8e20-c41702125667] +description = "Student is added to the roster" + +[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a] +description = "Adding multiple students in the same grade in the roster" + +[233be705-dd58-4968-889d-fb3c7954c9cc] +description = "Multiple students in the same grade are added to the roster" + +[87c871c1-6bde-4413-9c44-73d59a259d83] +description = "Cannot add student to same grade in the roster more than once" + +[c125dab7-2a53-492f-a99a-56ad511940d8] +description = "A student can't be in two different grades" +include = false + +[a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1] +description = "A student can only be added to the same grade in the roster once" +include = false +reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8" + +[d7982c4f-1602-49f6-a651-620f2614243a] +description = "Student not added to same grade in the roster more than once" +reimplements = "a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1" + +[e70d5d8f-43a9-41fd-94a4-1ea0fa338056] +description = "Adding students in multiple grades" + +[75a51579-d1d7-407c-a2f8-2166e984e8ab] +description = "Students in multiple grades are added to the roster" + +[7df542f1-57ce-433c-b249-ff77028ec479] +description = "Cannot add same student to multiple grades in the roster" + +[6a03b61e-1211-4783-a3cc-fc7f773fba3f] +description = "A student cannot be added to more than one grade in the sorted roster" +include = false +reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8" + +[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5] +description = "Student not added to multiple grades in the roster" +reimplements = "6a03b61e-1211-4783-a3cc-fc7f773fba3f" + +[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6] +description = "Students are sorted by grades in the roster" + +[d9fb5bea-f5aa-4524-9d61-c158d8906807] +description = "Students are sorted by name in the roster" + +[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6] +description = "Students are sorted by grades and then by name in the roster" + +[5e67aa3c-a3c6-4407-a183-d8fe59cd1630] +description = "Grade is empty if no students in the roster" + +[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6] +description = "Grade is empty if no students in that grade" + +[2bfc697c-adf2-4b65-8d0f-c46e085f796e] +description = "Student not added to same grade more than once" + +[66c8e141-68ab-4a04-a15a-c28bc07fe6b9] +description = "Student not added to multiple grades" + +[c9c1fc2f-42e0-4d2c-b361-99271f03eda7] +description = "Student not added to other grade for multiple grades" + +[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e] +description = "Students are sorted by name in a grade" diff --git a/exercises/practice/grade-school/dub.sdl b/exercises/practice/grade-school/dub.sdl new file mode 100644 index 0000000..baa5515 --- /dev/null +++ b/exercises/practice/grade-school/dub.sdl @@ -0,0 +1,2 @@ +name "grade-school" +buildRequirements "disallowDeprecations" diff --git a/exercises/practice/grade-school/example/grade_school.d b/exercises/practice/grade-school/example/grade_school.d new file mode 100644 index 0000000..3da81f6 --- /dev/null +++ b/exercises/practice/grade-school/example/grade_school.d @@ -0,0 +1,42 @@ +module grade_school; + +import std.algorithm; +import std.array; + +struct GradeSchool +{ + bool add(immutable string student, immutable int grade) + { + if (student in students) + { + return false; + } + + students[student] = grade; + return true; + } + + string[] grade(int grade) + { + return students + .byKeyValue + .filter!(kvp => kvp.value == grade) + .map!(kvp => kvp.key) + .array + .sort() + .array; + } + + string[] roster() + { + return students + .byKeyValue + .array + .sort!((a, b) => a.value < b.value || (a.value == b.value && a.key < b.key)) + .map!(kvp => kvp.key) + .array; + } + + private: + int[string] students; +} diff --git a/exercises/practice/grade-school/source/grade_school.d b/exercises/practice/grade-school/source/grade_school.d new file mode 100644 index 0000000..2a309ab --- /dev/null +++ b/exercises/practice/grade-school/source/grade_school.d @@ -0,0 +1,202 @@ +module grade_school; + +struct GradeSchool +{ + bool add(immutable string student, immutable int grade) + { + // implement this function + } + + string[] grade(int grade) + { + // implement this function + } + + string[] roster() + { + // implement this function + } +} + +unittest +{ + immutable int allTestsEnabled = 0; + + // Roster is empty when no student is added + { + GradeSchool school; + assert(school.roster() == []); + } + + static if (allTestsEnabled) + { + // Add a student + { + GradeSchool school; + assert(school.add("Aimee", 2)); + } + + // Student is added to the roster + { + GradeSchool school; + school.add("Aimee", 2); + assert(school.roster() == ["Aimee"]); + } + + // Adding multiple students in the same grade in the roster + { + GradeSchool school; + assert(school.add("Blair", 2)); + assert(school.add("James", 2)); + assert(school.add("Paul", 2)); + } + + // Multiple students in the same grade are added to the roster + { + GradeSchool school; + school.add("Blair", 2); + school.add("James", 2); + school.add("Paul", 2); + assert(school.roster() == ["Blair", "James", "Paul"]); + } + + // Cannot add student to same grade in the roster more than once + { + GradeSchool school; + school.add("Blair", 2); + assert(school.add("James", 2)); + assert(!school.add("James", 2)); + school.add("Paul", 2); + } + + // Student not added to same grade in the roster more than once + { + GradeSchool school; + school.add("Blair", 2); + school.add("James", 2); + school.add("James", 2); + school.add("Paul", 2); + assert(school.roster() == ["Blair", "James", "Paul"]); + } + + // Adding students in multiple grades + { + GradeSchool school; + assert(school.add("Chelsea", 3)); + assert(school.add("Logan", 7)); + } + + // Students in multiple grades are added to the roster + { + GradeSchool school; + school.add("Chelsea", 3); + school.add("Logan", 7); + assert(school.roster() == ["Chelsea", "Logan"]); + } + + // Cannot add same student to multiple grades in the roster + { + GradeSchool school; + school.add("Blair", 2); + assert(school.add("James", 2)); + assert(!school.add("James", 3)); + school.add("Paul", 3); + } + + // Student not added to multiple grades in the roster + { + GradeSchool school; + school.add("Blair", 2); + school.add("James", 2); + school.add("James", 3); + school.add("Paul", 3); + assert(school.roster() == ["Blair", "James", "Paul"]); + } + + // Students are sorted by grades in the roster + { + GradeSchool school; + school.add("Jim", 3); + school.add("Peter", 2); + school.add("Anna", 1); + assert(school.roster() == ["Anna", "Peter", "Jim"]); + } + + // Students are sorted by name in the roster + { + GradeSchool school; + school.add("Peter", 2); + school.add("Zoe", 2); + school.add("Alex", 2); + assert(school.roster() == ["Alex", "Peter", "Zoe"]); + } + + // Students are sorted by grades and then by name in the roster + { + GradeSchool school; + school.add("Peter", 2); + school.add("Anna", 1); + school.add("Barb", 1); + school.add("Zoe", 2); + school.add("Alex", 2); + school.add("Jim", 3); + school.add("Charlie", 1); + assert(school.roster() == ["Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"]); + } + + // Grade is empty if no students in the roster + { + GradeSchool school; + assert(school.grade(1) == []); + } + + // Grade is empty if no students in that grade + { + GradeSchool school; + school.add("Peter", 2); + school.add("Zoe", 2); + school.add("Alex", 2); + school.add("Jim", 3); + assert(school.grade(1) == []); + } + + // Student not added to same grade more than once + { + GradeSchool school; + school.add("Blair", 2); + school.add("James", 2); + school.add("James", 2); + school.add("Paul", 2); + assert(school.grade(2) == ["Blair", "James", "Paul"]); + } + + // Student not added to multiple grades + { + GradeSchool school; + school.add("Blair", 2); + school.add("James", 2); + school.add("James", 3); + school.add("Paul", 3); + assert(school.grade(2) == ["Blair", "James"]); + } + + // Student not added to other grade for multiple grades + { + GradeSchool school; + school.add("Blair", 2); + school.add("James", 2); + school.add("James", 3); + school.add("Paul", 3); + assert(school.grade(3) == ["Paul"]); + } + + // Students are sorted by name in a grade + { + GradeSchool school; + school.add("Franklin", 5); + school.add("Bradley", 5); + school.add("Jeff", 1); + assert(school.grade(5) == ["Bradley", "Franklin"]); + } + } +} From e384394713c8feb2713d873536e23c00d883e045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 24 Jul 2024 19:48:43 -0700 Subject: [PATCH 2/3] Drop unneeded parentheses --- exercises/practice/grade-school/example/grade_school.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/grade-school/example/grade_school.d b/exercises/practice/grade-school/example/grade_school.d index 3da81f6..6c8d8ae 100644 --- a/exercises/practice/grade-school/example/grade_school.d +++ b/exercises/practice/grade-school/example/grade_school.d @@ -23,7 +23,7 @@ struct GradeSchool .filter!(kvp => kvp.value == grade) .map!(kvp => kvp.key) .array - .sort() + .sort .array; } From 217effcc39b2940e1b7060f96e598f52c70fad6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 24 Jul 2024 19:49:03 -0700 Subject: [PATCH 3/3] Fix formatting --- .../practice/grade-school/example/grade_school.d | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/practice/grade-school/example/grade_school.d b/exercises/practice/grade-school/example/grade_school.d index 6c8d8ae..cc19b9c 100644 --- a/exercises/practice/grade-school/example/grade_school.d +++ b/exercises/practice/grade-school/example/grade_school.d @@ -19,12 +19,12 @@ struct GradeSchool string[] grade(int grade) { return students - .byKeyValue - .filter!(kvp => kvp.value == grade) - .map!(kvp => kvp.key) - .array - .sort - .array; + .byKeyValue + .filter!(kvp => kvp.value == grade) + .map!(kvp => kvp.key) + .array + .sort + .array; } string[] roster()