diff --git a/exercises/practice/grade-school/.meta/config.json b/exercises/practice/grade-school/.meta/config.json index ed78d3d756..9041af604d 100644 --- a/exercises/practice/grade-school/.meta/config.json +++ b/exercises/practice/grade-school/.meta/config.json @@ -6,6 +6,7 @@ "ankorGH", "draalger", "ee7", + "jagdish-15", "kytrinyx", "matthewmorgan", "mgmatola", diff --git a/exercises/practice/grade-school/.meta/proof.ci.js b/exercises/practice/grade-school/.meta/proof.ci.js index 3c9855054c..3798691b18 100644 --- a/exercises/practice/grade-school/.meta/proof.ci.js +++ b/exercises/practice/grade-school/.meta/proof.ci.js @@ -4,25 +4,32 @@ export class GradeSchool { } add(student, level) { - this.students.set(student, level); + for (const names of this.students.values()) { + if (names.has(student)) { + return false; + } + } + + if (!this.students.has(level)) { + this.students.set(level, new Set()); + } + + this.students.get(level).add(student); + return true; } grade(level) { - return Array.from(this.students.entries()) - .filter(([, studentGrade]) => studentGrade === level) - .map(([student]) => student) - .sort(); + if (!this.students.has(level)) { + return []; + } + return [...this.students.get(level)].sort(); } roster() { - const result = {}; - - Array.from(this.students.entries()).forEach(([, studentGrade]) => { - if (!result[studentGrade]) { - result[studentGrade] = this.grade(studentGrade); - } - }); - + const result = []; + for (const level of [...this.students.keys()].sort((a, b) => a - b)) { + result.push(...[...this.students.get(level)].sort()); + } return result; } } diff --git a/exercises/practice/grade-school/.meta/tests.toml b/exercises/practice/grade-school/.meta/tests.toml index 4eaa5f0d85..50c9e2e599 100644 --- a/exercises/practice/grade-school/.meta/tests.toml +++ b/exercises/practice/grade-school/.meta/tests.toml @@ -1,27 +1,86 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# 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 = "Adding a student adds them to the sorted roster" +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 -[233be705-dd58-4968-889d-fb3c7954c9cc] -description = "Adding more students adds them to the sorted roster" +[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 = "Adding students to different grades adds them to the same sorted roster" +description = "Students in multiple grades are added to the roster" -[a3f0fb58-f240-4723-8ddc-e644666b85cc] -description = "Roster returns an empty list if there are no students enrolled" +[7df542f1-57ce-433c-b249-ff77028ec479] +description = "Cannot add same student to multiple grades in the roster" -[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6] -description = "Student names with grades are displayed in the same sorted 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" -[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e] -description = "Grade returns the students in that grade in alphabetical order" +[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 returns an empty list if there are no students in that grade" +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/grade-school.spec.js b/exercises/practice/grade-school/grade-school.spec.js index 0334e1f835..7dca986a41 100644 --- a/exercises/practice/grade-school/grade-school.spec.js +++ b/exercises/practice/grade-school/grade-school.spec.js @@ -1,87 +1,178 @@ import { beforeEach, describe, expect, test, xtest } from '@jest/globals'; import { GradeSchool } from './grade-school'; -describe('School', () => { +describe('Grade School', () => { let school; beforeEach(() => { school = new GradeSchool(); }); - test('a new school has an empty roster', () => { - expect(school.roster()).toEqual({}); + test('Roster is empty when no student is added', () => { + expect(school.roster()).toEqual([]); }); - xtest('adding a student adds them to the roster for the given grade', () => { + xtest('Add a student', () => { + expect(school.add('Aimee', 2)).toEqual(true); + }); + + xtest('Student is added to the roster', () => { school.add('Aimee', 2); - const expectedDb = { 2: ['Aimee'] }; + const expectedDb = ['Aimee']; expect(school.roster()).toEqual(expectedDb); }); - xtest('adding more students to the same grade adds them to the roster', () => { + xtest('Adding multiple students in the same grade in the roster', () => { + expect(school.add('Blair', 2)).toEqual(true); + expect(school.add('James', 2)).toEqual(true); + expect(school.add('Paul', 2)).toEqual(true); + }); + + xtest('Multiple students in the same grade are added to the roster', () => { school.add('Blair', 2); school.add('James', 2); school.add('Paul', 2); - const expectedDb = { 2: ['Blair', 'James', 'Paul'] }; + const expectedDb = ['Blair', 'James', 'Paul']; expect(school.roster()).toEqual(expectedDb); }); - xtest('adding students to different grades adds them to the roster', () => { + xtest('Cannot add student to same grade in the roster more than once', () => { + expect(school.add('Blair', 2)).toEqual(true); + expect(school.add('James', 2)).toEqual(true); + expect(school.add('James', 2)).toEqual(false); + expect(school.add('Paul', 2)).toEqual(true); + }); + + xtest('Student not added to same grade in the roster more than once', () => { + school.add('Blair', 2); + school.add('James', 2); + school.add('James', 2); + school.add('Paul', 2); + + const expectedDb = ['Blair', 'James', 'Paul']; + expect(school.roster()).toEqual(expectedDb); + }); + + xtest('Adding students in multiple grades', () => { + expect(school.add('Chelsea', 3)).toEqual(true); + expect(school.add('Logan', 7)).toEqual(true); + }); + + xtest('Students in multiple grades are added to the roster', () => { school.add('Chelsea', 3); school.add('Logan', 7); - const expectedDb = { 3: ['Chelsea'], 7: ['Logan'] }; + const expectedDb = ['Chelsea', 'Logan']; expect(school.roster()).toEqual(expectedDb); }); - xtest('grade returns the students in that grade in alphabetical order', () => { - school.add('Franklin', 5); - school.add('Bradley', 5); - school.add('Jeff', 1); - - const expectedStudents = ['Bradley', 'Franklin']; - expect(school.grade(5)).toEqual(expectedStudents); + xtest('Cannot add same student to multiple grades in the roster', () => { + expect(school.add('Blair', 2)).toEqual(true); + expect(school.add('James', 2)).toEqual(true); + expect(school.add('James', 3)).toEqual(false); + expect(school.add('Paul', 3)).toEqual(true); }); - xtest('grade returns an empty array if there are no students in that grade', () => { - expect(school.grade(1)).toEqual([]); + xtest('Student not added to multiple grades in the roster', () => { + school.add('Blair', 2); + school.add('James', 2); + school.add('James', 3); + school.add('Paul', 3); + + const expectedDb = ['Blair', 'James', 'Paul']; + expect(school.roster()).toEqual(expectedDb); }); - xtest('the students names in each grade in the roster are sorted', () => { - school.add('Jennifer', 4); - school.add('Kareem', 6); - school.add('Christopher', 4); - school.add('Kyle', 3); + xtest('Students are sorted by grades in the roster', () => { + school.add('Jim', 3); + school.add('Peter', 2); + school.add('Anna', 1); - const expectedSortedStudents = { - 3: ['Kyle'], - 4: ['Christopher', 'Jennifer'], - 6: ['Kareem'], - }; - expect(school.roster()).toEqual(expectedSortedStudents); + const expectedDb = ['Anna', 'Peter', 'Jim']; + expect(school.roster()).toEqual(expectedDb); }); - xtest('roster cannot be modified outside of module', () => { - school.add('Aimee', 2); - const roster = school.roster(); - roster[2].push('Oops.'); - const expectedDb = { 2: ['Aimee'] }; + xtest('Students are sorted by name in the roster', () => { + school.add('Peter', 2); + school.add('Zoe', 2); + school.add('Alex', 2); + + const expectedDb = ['Alex', 'Peter', 'Zoe']; expect(school.roster()).toEqual(expectedDb); }); - xtest('roster cannot be modified outside of module using grade()', () => { - school.add('Aimee', 2); - school.grade(2).push('Oops.'); - const expectedDb = { 2: ['Aimee'] }; + xtest('Students are sorted by grades and then by name in the roster', () => { + 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); + + const expectedDb = [ + 'Anna', + 'Barb', + 'Charlie', + 'Alex', + 'Peter', + 'Zoe', + 'Jim', + ]; expect(school.roster()).toEqual(expectedDb); }); - xtest("a student can't be in two different grades", () => { - school.add('Aimee', 2); - school.add('Aimee', 1); + xtest('Grade is empty if no students in the roster', () => { + expect(school.grade(1)).toEqual([]); + }); + + xtest('Grade is empty if no students in that grade', () => { + school.add('Peter', 2); + school.add('Zoe', 2); + school.add('Alex', 2); + school.add('Jim', 3); + + expect(school.grade(1)).toEqual([]); + }); + + xtest('Student not added to same grade more than once', () => { + school.add('Blair', 2); + school.add('James', 2); + school.add('James', 2); + school.add('Paul', 2); + + const expectedDb = ['Blair', 'James', 'Paul']; + expect(school.grade(2)).toEqual(expectedDb); + }); + + xtest('Student not added to multiple grades', () => { + school.add('Blair', 2); + school.add('James', 2); + school.add('James', 3); + school.add('Paul', 3); + + const expectedDb = ['Blair', 'James']; + expect(school.grade(2)).toEqual(expectedDb); + }); + + xtest('Student not added to other grade for multiple grades', () => { + school.add('Blair', 2); + school.add('James', 2); + school.add('James', 3); + school.add('Paul', 3); + + const expectedDb = ['Paul']; + expect(school.grade(3)).toEqual(expectedDb); + }); + + xtest('Students are sorted by name in a grade', () => { + school.add('Franklin', 5); + school.add('Bradley', 5); + school.add('Jeff', 1); - expect(school.grade(2)).toEqual([]); + const expectedDb = ['Bradley', 'Franklin']; + expect(school.grade(5)).toEqual(expectedDb); }); });