ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# JaeHyoLee's solution

## to Grade School in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

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:

• "OK."
• Get a list of all students enrolled in a grade
• "Which students are in grade 2?"
• "We've only got Jim just now."
• Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
• "Who all is enrolled in school right now?"

Note that all our students only have one name. (It's a small town, what do you want?)

## For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

• If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

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?

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

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.

## Source

A pairing session with Phil Battos at gSchool http://gschool.it

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

``````local School = require('grade-school')

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)

local school = School:new()
local expected = { [2] = { 'Aimee' } }
local result = school:roster()
assert.are.same(expected, result)
end)

local school = School:new()
local expected = { [2] = { 'Blair', 'James', 'Paul' } }
local result = school:roster()
assert.are.same(expected, result)
end)

local school = School:new()
local expected = { [3]={ 'Chelsea' }, [7] = { 'Logan'} }
local result = school:roster()
assert.are.same(expected, result)
end)

local school = School:new()
local expected = { 'Bradley', 'Franklin' }
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 expected = {}
assert.are.same(expected, result)
end)

it('the students names in each grade in the roster are sorted', function()
local school = School:new()
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

table.sort(self._roster[grade], function (a, b) return a < b end)
end

end

return School``````