Skip to content

Commit a93f9d2

Browse files
kytrinyxkotp
authored andcommitted
Reimplement grade-school based on problem-specifications
The upstream specification has undergone significant changes. This generates a new test suite based on the changed specification, and reimplements the sample solution to pass the new tests.
1 parent 15d65b0 commit a93f9d2

File tree

5 files changed

+238
-99
lines changed

5 files changed

+238
-99
lines changed
Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Instructions
22

3-
Given students' names along with the grade that they are in, create a roster
4-
for the school.
3+
Given students' names along with the grade that they are in, create a roster for the school.
54

65
In the end, you should be able to:
76

@@ -11,28 +10,12 @@ In the end, you should be able to:
1110
- Get a list of all students enrolled in a grade
1211
- "Which students are in grade 2?"
1312
- "We've only got Jim just now."
14-
- Get a sorted list of all students in all grades. Grades should sort
15-
as 1, 2, 3, etc., and students within a grade should be sorted
16-
alphabetically by name.
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.
1715
- "Who all is enrolled in school right now?"
18-
- "Let me think. We have
19-
Anna, Barb, and Charlie in grade 1,
20-
Alex, Peter, and Zoe in grade 2
21-
and Jim in grade 5.
22-
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim"
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"
2319

24-
Note that all our students only have one name. (It's a small town, what
25-
do you want?)
26-
27-
## For bonus points
28-
29-
Did you get the tests passing and the code clean? If you want to, these
30-
are some additional things you could try:
31-
32-
- If you're working in a language with mutable data structures and your
33-
implementation allows outside code to mutate the school's internal DB
34-
directly, see if you can prevent this. Feel free to introduce additional
35-
tests.
36-
37-
Then please share your thoughts in a comment on the submission. Did this
38-
experiment make the code better? Worse? Did you learn anything from it?
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.

exercises/practice/grade-school/.meta/config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"blurb": "Given students' names along with the grade that they are in, create a roster for the school",
2+
"blurb": "Given students' names along with the grade that they are in, create a roster for the school.",
33
"authors": [
44
"kytrinyx"
55
],
@@ -29,6 +29,5 @@
2929
".meta/example.rb"
3030
]
3131
},
32-
"source": "A pairing session with Phil Battos at gSchool",
33-
"source_url": "http://gschool.it"
32+
"source": "A pairing session with Phil Battos at gSchool"
3433
}

exercises/practice/grade-school/.meta/example.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ def initialize
33
@students = Hash.new { |hash, key| hash[key] = [] }
44
end
55

6-
def students_by_grade
7-
@students.keys.sort.map { |level| grade(level) }
6+
def add(student, grade)
7+
if roster.include?(student)
8+
return false
9+
end
10+
students[grade] << student
11+
true
812
end
913

10-
def add(student, level)
11-
@students[level] << student
12-
@students[level].sort!
14+
def roster
15+
students.sort_by {|grade, names| grade}.map {|_, names| names.sort}.flatten
1316
end
1417

15-
def students(level)
16-
@students[level]
18+
def grade(number)
19+
students[number].sort
1720
end
1821

19-
def grade(level)
20-
{ grade: level, students: students(level) }
21-
end
22+
private
23+
attr_reader :students
2224
end

exercises/practice/grade-school/.meta/tests.toml

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,78 @@
99
# As user-added comments (using the # character) will be removed when this file
1010
# is regenerated, comments can be added via a `comment` key.
1111

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+
1218
[6d0a30e4-1b4e-472e-8e20-c41702125667]
13-
description = "Adding a student adds them to the sorted roster"
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"
1423

1524
[233be705-dd58-4968-889d-fb3c7954c9cc]
16-
description = "Adding more student adds them to the sorted roster"
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"
1745

1846
[75a51579-d1d7-407c-a2f8-2166e984e8ab]
19-
description = "Adding students to different grades adds them to the same sorted roster"
47+
description = "Students in multiple grades are added to the roster"
2048

21-
[a3f0fb58-f240-4723-8ddc-e644666b85cc]
22-
description = "Roster returns an empty list if there are no students enrolled"
49+
[7df542f1-57ce-433c-b249-ff77028ec479]
50+
description = "Cannot add same student to multiple grades in the roster"
2351

24-
[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]
25-
description = "Student names with grades are displayed in the same sorted roster"
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"
2656

27-
[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]
28-
description = "Grade returns the students in that grade in alphabetical order"
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"
2969

3070
[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]
31-
description = "Grade returns an empty list if there are no students in that grade"
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"

0 commit comments

Comments
 (0)