Skip to content

Commit 3a8b203

Browse files
authored
Add grade-school (#327)
1 parent 043586d commit 3a8b203

File tree

7 files changed

+295
-0
lines changed

7 files changed

+295
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@
643643
"prerequisites": [],
644644
"difficulty": 2
645645
},
646+
{
647+
"slug": "grade-school",
648+
"name": "Grade School",
649+
"uuid": "98f925a2-bc1c-4dc1-b2f4-d06941114250",
650+
"practices": [],
651+
"prerequisites": [],
652+
"difficulty": 5
653+
},
646654
{
647655
"slug": "minesweeper",
648656
"name": "Minesweeper",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Instructions
2+
3+
Given students' names along with the grade that they are in, create a roster for the school.
4+
5+
In the end, you should be able to:
6+
7+
- Add a student's name to the roster for a grade
8+
- "Add Jim to grade 2."
9+
- "OK."
10+
- Get a list of all students enrolled in a grade
11+
- "Which students are in grade 2?"
12+
- "We've only got Jim just now."
13+
- Get a sorted list of all students in all grades.
14+
Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
15+
- "Who all is enrolled in school right now?"
16+
- "Let me think.
17+
We have Anna, Barb, and Charlie in grade 1, Alex, Peter, and Zoe in grade 2 and Jim in grade 5.
18+
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim"
19+
20+
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.
21+
In fact, when a test attempts to add the same student more than once, your implementation should indicate that this is incorrect.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"grade-school.coffee"
8+
],
9+
"test": [
10+
"grade-school.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Given students' names along with the grade that they are in, create a roster for the school.",
17+
"source": "A pairing session with Phil Battos at gSchool"
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class GradeSchool
2+
constructor: () ->
3+
@students = {}
4+
5+
add: (student, level) ->
6+
if @students[student]
7+
false
8+
else
9+
@students[student] = level
10+
true
11+
12+
grade: (level) ->
13+
result = []
14+
for student, grade of @students
15+
if grade is level
16+
result.push student
17+
result.sort()
18+
19+
roster: () ->
20+
result = []
21+
grades = (g for _, g of @students).sort()
22+
for g in grades.unique()
23+
result.push (@grade g)...
24+
result
25+
26+
Array::unique = ->
27+
output = {}
28+
output[@[key]] = @[key] for key in [0...@length]
29+
value for key, value of output
30+
31+
module.exports = GradeSchool
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[a3f0fb58-f240-4723-8ddc-e644666b85cc]
13+
description = "Roster is empty when no student is added"
14+
15+
[9337267f-7793-4b90-9b4a-8e3978408824]
16+
description = "Add a student"
17+
18+
[6d0a30e4-1b4e-472e-8e20-c41702125667]
19+
description = "Student is added to the roster"
20+
21+
[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a]
22+
description = "Adding multiple students in the same grade in the roster"
23+
24+
[233be705-dd58-4968-889d-fb3c7954c9cc]
25+
description = "Multiple students in the same grade are added to the roster"
26+
27+
[87c871c1-6bde-4413-9c44-73d59a259d83]
28+
description = "Cannot add student to same grade in the roster more than once"
29+
30+
[c125dab7-2a53-492f-a99a-56ad511940d8]
31+
description = "A student can't be in two different grades"
32+
include = false
33+
34+
[a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1]
35+
description = "A student can only be added to the same grade in the roster once"
36+
include = false
37+
reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8"
38+
39+
[d7982c4f-1602-49f6-a651-620f2614243a]
40+
description = "Student not added to same grade in the roster more than once"
41+
reimplements = "a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1"
42+
43+
[e70d5d8f-43a9-41fd-94a4-1ea0fa338056]
44+
description = "Adding students in multiple grades"
45+
46+
[75a51579-d1d7-407c-a2f8-2166e984e8ab]
47+
description = "Students in multiple grades are added to the roster"
48+
49+
[7df542f1-57ce-433c-b249-ff77028ec479]
50+
description = "Cannot add same student to multiple grades in the roster"
51+
52+
[6a03b61e-1211-4783-a3cc-fc7f773fba3f]
53+
description = "A student cannot be added to more than one grade in the sorted roster"
54+
include = false
55+
reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8"
56+
57+
[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5]
58+
description = "Student not added to multiple grades in the roster"
59+
reimplements = "6a03b61e-1211-4783-a3cc-fc7f773fba3f"
60+
61+
[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6]
62+
description = "Students are sorted by grades in the roster"
63+
64+
[d9fb5bea-f5aa-4524-9d61-c158d8906807]
65+
description = "Students are sorted by name in the roster"
66+
67+
[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]
68+
description = "Students are sorted by grades and then by name in the roster"
69+
70+
[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]
71+
description = "Grade is empty if no students in the roster"
72+
73+
[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6]
74+
description = "Grade is empty if no students in that grade"
75+
76+
[2bfc697c-adf2-4b65-8d0f-c46e085f796e]
77+
description = "Student not added to same grade more than once"
78+
79+
[66c8e141-68ab-4a04-a15a-c28bc07fe6b9]
80+
description = "Student not added to multiple grades"
81+
82+
[c9c1fc2f-42e0-4d2c-b361-99271f03eda7]
83+
description = "Student not added to other grade for multiple grades"
84+
85+
[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]
86+
description = "Students are sorted by name in a grade"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class GradeSchool
2+
constructor: () ->
3+
4+
add: (student, level) ->
5+
6+
grade: (level) ->
7+
8+
roster: () ->
9+
10+
module.exports = GradeSchool
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
GradeSchool = require './grade-school'
2+
3+
describe 'Grade School', ->
4+
school = {}
5+
beforeEach ->
6+
school = new GradeSchool
7+
8+
it 'Roster is empty when no student is added', ->
9+
expect(school.roster()).toEqual []
10+
11+
xit 'Add a student', ->
12+
expect(school.add 'Aimee', 2).toBe true
13+
14+
xit 'Student is added to the roster', ->
15+
school.add 'Aimee', 2
16+
expect(school.roster()).toEqual ['Aimee']
17+
18+
xit 'Adding multiple students in the same grade in the roster', ->
19+
expect(school.add 'Blair', 2).toBe true
20+
expect(school.add 'James', 2).toBe true
21+
expect(school.add 'Paul', 2).toBe true
22+
23+
xit 'Multiple students in the same grade are added to the roster', ->
24+
school.add 'Blair', 2
25+
school.add 'James', 2
26+
school.add 'Paul', 2
27+
expect(school.roster()).toEqual ['Blair', 'James', 'Paul']
28+
29+
xit 'Cannot add student to same grade in the roster more than once', ->
30+
school.add 'Blair', 2
31+
expect(school.add 'James', 2).toBe true
32+
expect(school.add 'James', 2).toBe false
33+
school.add 'Paul', 2
34+
35+
xit 'Student not added to same grade in the roster more than once', ->
36+
school.add 'Blair', 2
37+
school.add 'James', 2
38+
school.add 'James', 2
39+
school.add 'Paul', 2
40+
expect(school.roster()).toEqual ['Blair', 'James', 'Paul']
41+
42+
xit 'Adding students in multiple grades', ->
43+
expect(school.add 'Chelsea', 3).toBe true
44+
expect(school.add 'Logan', 7).toBe true
45+
46+
xit 'Students in multiple grades are added to the roster', ->
47+
school.add 'Chelsea', 3
48+
school.add 'Logan', 7
49+
expect(school.roster()).toEqual ['Chelsea', 'Logan']
50+
51+
xit 'Cannot add same student to multiple grades in the roster', ->
52+
school.add 'Blair', 2
53+
expect(school.add 'James', 2).toBe true
54+
expect(school.add 'James', 3).toBe false
55+
school.add 'Paul', 3
56+
57+
xit 'Student not added to multiple grades in the roster', ->
58+
school.add 'Blair', 2
59+
school.add 'James', 2
60+
school.add 'James', 3
61+
school.add 'Paul', 3
62+
expect(school.roster()).toEqual ['Blair', 'James', 'Paul']
63+
64+
xit 'Students are sorted by grades in the roster', ->
65+
school.add 'Jim', 3
66+
school.add 'Peter', 2
67+
school.add 'Anna', 1
68+
expect(school.roster()).toEqual ['Anna', 'Peter', 'Jim']
69+
70+
xit 'Students are sorted by name in the roster', ->
71+
school.add 'Peter', 2
72+
school.add 'Zoe', 2
73+
school.add 'Alex', 2
74+
expect(school.roster()).toEqual ['Alex', 'Peter', 'Zoe']
75+
76+
xit 'Students are sorted by grades and then by name in the roster', ->
77+
school.add 'Peter', 2
78+
school.add 'Anna', 1
79+
school.add 'Barb', 1
80+
school.add 'Zoe', 2
81+
school.add 'Alex', 2
82+
school.add 'Jim', 3
83+
school.add 'Charlie', 1
84+
expect(school.roster()).toEqual ['Anna', 'Barb', 'Charlie', 'Alex', 'Peter', 'Zoe', 'Jim']
85+
86+
xit 'Grade is empty if no students in the roster', ->
87+
expect(school.grade 1).toEqual []
88+
89+
xit 'Grade is empty if no students in that grade', ->
90+
school.add 'Peter', 2
91+
school.add 'Zoe', 2
92+
school.add 'Alex', 2
93+
school.add 'Jim', 3
94+
expect(school.grade 1).toEqual []
95+
96+
xit 'Student not added to same grade more than once', ->
97+
school.add 'Blair', 2
98+
school.add 'James', 2
99+
school.add 'James', 2
100+
school.add 'Paul', 2
101+
expect(school.grade 2).toEqual ['Blair', 'James', 'Paul']
102+
103+
xit 'Student not added to multiple grades', ->
104+
school.add 'Blair', 2
105+
school.add 'James', 2
106+
school.add 'James', 3
107+
school.add 'Paul', 3
108+
expect(school.grade 2).toEqual ['Blair', 'James']
109+
110+
xit 'Student not added to other grade for multiple grades', ->
111+
school.add 'Blair', 2
112+
school.add 'James', 2
113+
school.add 'James', 3
114+
school.add 'Paul', 3
115+
expect(school.grade 3).toEqual ['Paul']
116+
117+
xit 'Students are sorted by name in a grade', ->
118+
school.add 'Franklin', 5
119+
school.add 'Bradley', 5
120+
school.add 'Jeff', 1
121+
expect(school.grade 5).toEqual ['Bradley', 'Franklin']

0 commit comments

Comments
 (0)