Skip to content

Commit 280267a

Browse files
authored
Add grade-school (#310)
1 parent d5fc26b commit 280267a

File tree

7 files changed

+334
-0
lines changed

7 files changed

+334
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,14 @@
575575
"prerequisites": [],
576576
"difficulty": 4
577577
},
578+
{
579+
"slug": "grade-school",
580+
"name": "Grade School",
581+
"uuid": "17234e84-78aa-4afc-9bad-aa1ca0483d28",
582+
"practices": [],
583+
"prerequisites": [],
584+
"difficulty": 5
585+
},
578586
{
579587
"slug": "hamming",
580588
"name": "Hamming",
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.
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.vim"
8+
],
9+
"test": [
10+
"grade_school.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function! GradeSchool() abort
2+
return {
3+
\ 'values': {},
4+
\ 'Add': function('s:Add'),
5+
\ 'Grade': function('s:Grade'),
6+
\ 'Roster': function('s:Roster')}
7+
endfunction
8+
9+
function! s:Add(student, grade) abort dict
10+
if has_key(self.values, a:student)
11+
return v:false
12+
endif
13+
14+
let self.values[a:student] = a:grade
15+
return v:true
16+
endfunction
17+
18+
function! s:Grade(grade) abort dict
19+
let l:result = []
20+
21+
for l:item in items(self.values)
22+
if l:item[1] == a:grade
23+
call add(l:result, l:item[0])
24+
endif
25+
endfor
26+
27+
let l:sorted = sort(l:result)
28+
return l:sorted
29+
endfunction
30+
31+
function! s:Roster() abort dict
32+
let l:result = []
33+
34+
let l:grades = sort(s:Unique(values(self.values)))
35+
for l:grade in l:grades
36+
call extend(l:result, self.Grade(l:grade))
37+
endfor
38+
39+
return l:result
40+
endfunction
41+
42+
function s:Unique(list) abort
43+
let l:result = []
44+
for l:item in a:list
45+
if index(l:result, l:item) == -1
46+
call add(l:result, l:item)
47+
endif
48+
endfor
49+
50+
return l:result
51+
endfunction
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
Before:
2+
let g:school = GradeSchool()
3+
4+
Execute (Roster is empty when no student is added):
5+
let g:expected = []
6+
AssertEqual g:expected, g:school.Roster()
7+
8+
Execute (Add a student):
9+
Assert g:school.Add('Aimee', 2)
10+
11+
Execute (Student is added to the roster):
12+
call g:school.Add('Aimee', 2)
13+
AssertEqual ['Aimee'], g:school.Roster()
14+
15+
Execute (Adding multiple students in the same grade in the roster):
16+
Assert g:school.Add('Blair', 2)
17+
Assert g:school.Add('James', 2)
18+
Assert g:school.Add('Paul', 2)
19+
20+
Execute (Multiple students in the same grade are added to the roster):
21+
call g:school.Add('Blair', 2)
22+
call g:school.Add('James', 2)
23+
call g:school.Add('Paul', 2)
24+
25+
let g:expected = ['Blair', 'James', 'Paul']
26+
AssertEqual g:expected, g:school.Roster()
27+
28+
Execute (Cannot add student to same grade in the roster more than once):
29+
call g:school.Add('Blair', 2)
30+
Assert g:school.Add('James', 2)
31+
Assert !g:school.Add('James', 2)
32+
call g:school.Add('Paul', 2)
33+
34+
Execute (Student not added to same grade in the roster more than once):
35+
call g:school.Add('Blair', 2)
36+
call g:school.Add('James', 2)
37+
call g:school.Add('James', 2)
38+
call g:school.Add('Paul', 2)
39+
40+
let g:expected = ['Blair', 'James', 'Paul']
41+
AssertEqual g:expected, g:school.Roster()
42+
43+
Execute (Adding students in multiple grades):
44+
Assert g:school.Add('Chelsea', 3)
45+
Assert g:school.Add('Logan', 7)
46+
47+
Execute (Students in multiple grades are added to the roster):
48+
call g:school.Add('Chelsea', 3)
49+
call g:school.Add('Logan', 7)
50+
51+
let g:expected = ['Chelsea', 'Logan']
52+
AssertEqual g:expected, g:school.Roster()
53+
54+
Execute (Cannot add same student to multiple grades in the roster):
55+
let g:students = [['Blair', 2], ['James', 2], ['James', 3], ['Paul', 3]]
56+
call g:school.Add('Blair', 2)
57+
Assert g:school.Add('James', 2)
58+
Assert !g:school.Add('James', 3)
59+
call g:school.Add('Paul', 3)
60+
61+
Execute (Student not added to multiple grades in the roster):
62+
call g:school.Add('Blair', 2)
63+
call g:school.Add('James', 2)
64+
call g:school.Add('James', 3)
65+
call g:school.Add('Paul', 3)
66+
67+
let g:expected = ['Blair', 'James', 'Paul']
68+
AssertEqual g:expected, g:school.Roster()
69+
70+
Execute (Students are sorted by grades in the roster):
71+
call g:school.Add('Jim', 3)
72+
call g:school.Add('Peter', 2)
73+
call g:school.Add('Anna', 1)
74+
75+
let g:expected = ['Anna', 'Peter', 'Jim']
76+
AssertEqual g:expected, g:school.Roster()
77+
78+
Execute (Students are sorted by name in the roster):
79+
call g:school.Add('Peter', 2)
80+
call g:school.Add('Zoe', 2)
81+
call g:school.Add('Alex', 2)
82+
83+
let g:expected = ['Alex', 'Peter', 'Zoe']
84+
AssertEqual g:expected, g:school.Roster()
85+
86+
Execute (Students are sorted by grades and then by name in the roster):
87+
call g:school.Add('Peter', 2)
88+
call g:school.Add('Anna', 1)
89+
call g:school.Add('Barb', 1)
90+
call g:school.Add('Zoe', 2)
91+
call g:school.Add('Alex', 2)
92+
call g:school.Add('Jim', 3)
93+
call g:school.Add('Charlie', 1)
94+
95+
let g:expected = ['Anna', 'Barb', 'Charlie', 'Alex', 'Peter', 'Zoe', 'Jim']
96+
AssertEqual g:expected, g:school.Roster()
97+
98+
Execute (Grade is empty if no students in the roster):
99+
AssertEqual [], g:school.Grade(1)
100+
101+
Execute (Grade is empty if no students in that grade):
102+
call g:school.Add('Peter', 2)
103+
call g:school.Add('Zoe', 2)
104+
call g:school.Add('Alex', 2)
105+
call g:school.Add('Jim', 3)
106+
107+
let g:expected = []
108+
AssertEqual expected, g:school.Grade(1)
109+
110+
Execute (Student not added to same grade more than once):
111+
call g:school.Add('Blair', 2)
112+
call g:school.Add('James', 2)
113+
call g:school.Add('James', 2)
114+
call g:school.Add('Paul', 2)
115+
116+
let g:expected = ['Blair', 'James', 'Paul']
117+
AssertEqual expected, g:school.Grade(2)
118+
119+
Execute (Student not added to multiple grades):
120+
call g:school.Add('Blair', 2)
121+
call g:school.Add('James', 2)
122+
call g:school.Add('James', 3)
123+
call g:school.Add('Paul', 3)
124+
125+
let g:expected = ['Blair', 'James']
126+
AssertEqual expected, g:school.Grade(2)
127+
128+
Execute (Student not added to other grade for multiple grades):
129+
call g:school.Add('Blair', 2)
130+
call g:school.Add('James', 2)
131+
call g:school.Add('James', 3)
132+
call g:school.Add('Paul', 3)
133+
134+
let g:expected = ['Paul']
135+
AssertEqual expected, g:school.Grade(3)
136+
137+
Execute (Students are sorted by name in a grade):
138+
call g:school.Add('Franklin', 5)
139+
call g:school.Add('Bradley', 5)
140+
call g:school.Add('Jeff', 1)
141+
142+
let g:expected = ['Bradley', 'Franklin']
143+
AssertEqual expected, g:school.Grade(5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"
2+
" Returns an object representing a school roster, which can add a student,
3+
" list students in a grade, or list all students
4+
"
5+
function! GradeSchool() abort
6+
" your implementation goes here
7+
endfunction

0 commit comments

Comments
 (0)