Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of billwanjohi

billwanjohi's solution

to Grade School in the Scala Track

Published at Feb 15 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

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?

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.


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.


import org.scalatest._

/** @version created manually **/
class GradeSchoolTest extends FunSuite with Matchers with OneInstancePerTest {
  val school = new School

  test ("empty school") {
    school.db should be (Map())

  test ("add student") {
    school.add("Aimee", 2)
    school.db should be (Map(2 -> Seq("Aimee")))

  test ("add more students in same class") {
    school.add("James", 2)
    school.add("Blair", 2)
    school.add("Paul", 2)
    school.db should be (Map(2 -> Seq("James", "Blair", "Paul")))

  test ("add students to different grades") {
    school.add("Chelsea", 3)
    school.add("Logan", 7)
    school.db should be (Map(3 -> Seq("Chelsea"), 7 -> Seq("Logan")))

  test ("get students in a grade") {
    school.add("Franklin", 5)
    school.add("Bradley", 5)
    school.add("Jeff", 1)
    school.grade(5) should be (Seq("Franklin", "Bradley"))

  test ("get students in a non-existant grade") {
    school.grade(1) should be (Seq())

  test ("sort school") {
    school.add("Jennifer", 4)
    school.add("Kareem", 6)
    school.add("Christopher", 4)
    school.add("Kyle", 3)
    val sorted = Map(
      3 -> Seq("Kyle"),
      4 -> Seq("Christopher", "Jennifer"),
      6 -> Seq("Kareem")
    school.sorted should be (sorted)
    school.sorted.keys.toList should be (Seq(3, 4, 6))
class School {
  type DB = Map[Int, Seq[String]]

  var db: DB = Map()

  def add(name: String, g: Int) =
    db = db + (g -> (grade(g) :+ name))

  def grade(g: Int): Seq[String] = db getOrElse(g, Seq())

  def sorted: DB = {
    db.toSeq.sortBy(_._1).map {
      pair => (pair._1, pair._2.sorted)

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?