Avatar of JaeHyoLee

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:

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "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?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

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.

grade-school_spec.lua

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

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?