🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Grade School in the Scala Track

Published at Aug 14 2019 · 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?

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.

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.

GradeSchoolTest.scala

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") {
    pending
    school.add("Aimee", 2)
    school.db should be (Map(2 -> Seq("Aimee")))
  }

  test ("add more students in same class") {
    pending
    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") {
    pending
    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") {
    pending
    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") {
    pending
    school.grade(1) should be (Seq())
  }

  test ("sort school") {
    pending
    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))
  }
}
import scala.collection.immutable.TreeMap

class School {
  type DB = Map[Int, Seq[String]]
  private var dataStore: DB = Map()

  def add(name: String, g: Int) = {
    dataStore += (g -> (grade(g) :+ name))
  }

  def db: DB = dataStore

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

  def sorted: DB = TreeMap(dataStore.toSeq: _*).mapValues(seq => seq.sorted)








































  //  type DB = Map[Int, List[String]]
  //  var storage: DB = Map[Int, List[String]]()
  //
  //  def add(name: String, g: Int) = storage +=(g -> (grade(g):::name::Nil))
  //
  //  def db: DB = storage
  //
  //  def grade(g: Int): List[String] = storage.get(g) match {
  //    case None => List()
  //    case Some(x) => x
  //  }
  //
  //  def sorted: DB = ListMap(storage.toSeq.sortBy(_._1):_*)
}

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?