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:
Note that all our students only have one name. (It's a small town, what do you want?)
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?
To run the tests, run the command busted
from within the exercise directory.
For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.
A pairing session with Phil Battos at gSchool http://gschool.it
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
local School = require('grade-school')
describe('grade-school', function()
it('a new school has an empty roster', function()
local school = School:new()
local expected = {}
local result = school:roster()
assert.are.same(expected, result)
end)
it('adding a student adds them to the roster for the given grade', function()
local school = School:new()
school:add('Aimee', 2)
local expected = { [2] = { 'Aimee' } }
local result = school:roster()
assert.are.same(expected, result)
end)
it('adding more students to the same grade adds them to the roster', function()
local school = School:new()
school:add('Blair', 2)
school:add('James', 2)
school:add('Paul', 2)
local expected = { [2] = { 'Blair', 'James', 'Paul' } }
local result = school:roster()
assert.are.same(expected, result)
end)
it('adding students to different grades adds them to the roster', function()
local school = School:new()
school:add('Chelsea',3)
school:add('Logan',7)
local expected = { [3]={ 'Chelsea' }, [7] = { 'Logan'} }
local result = school:roster()
assert.are.same(expected, result)
end)
it('grade returns the students in that grade in alphabetical order', function()
local school = School:new()
school:add('Franklin', 5)
school:add('Bradley', 5)
school:add('Jeff', 1)
local expected = { 'Bradley', 'Franklin' }
local result = school:grade(5)
assert.are.same(expected, result)
end)
it('grade returns an empty array if there are no students in that grade', function()
local school = School:new()
local result = school:grade(1)
local expected = {}
assert.are.same(expected, result)
end)
it('the students names in each grade in the roster are sorted', function()
local school = School:new()
school:add('Jennifer', 4)
school:add('Kareem', 6)
school:add('Christopher', 4)
school:add('Kyle', 3)
local expected = {
[3]={ 'Kyle' },
[4]={ 'Christopher', 'Jennifer' },
[6]={ 'Kareem' }
}
local result = school:roster()
assert.are.same(expected, result)
end)
end)
local School = {}
School.__index = School
function School:new()
return setmetatable({_roster = {}}, School)
end
function School:roster()
return self._roster
end
function School:add(name, grade)
self._roster[grade] = self._roster[grade] or {}
table.insert(self._roster[grade], name)
table.sort(self._roster[grade], function (a, b) return a < b end)
end
function School:grade(_grade)
return self._roster[_grade] or {}
end
return School
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments