Avatar of BilalMakhlouf

BilalMakhlouf's solution

to Grade School in the Kotlin Track

Published at Jun 13 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?


Go through the setup instructions for Kotlin to install the necessary dependencies:


Making the test suite pass

Execute the tests with:

$ gradlew test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore annotation.


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.junit.Before
import org.junit.Test
import org.junit.Ignore
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SchoolTest {

    private lateinit var school: School

    fun beforeTest() {
        school = School()

    fun startsWithNoStudents() {

    fun addsStudents() {
        school.add("Aimee", 2)

        val expected = mapOf(2 to listOf("Aimee"))
        assertEquals(expected, school.db())

    fun addsMoreStudentsInSameGrade() {
        val grade = 2
        school.add("James", grade)
        school.add("Blair", grade)
        school.add("Paul", grade)

        val expected = mapOf(2 to listOf("James", "Blair", "Paul"))
        assertEquals(expected, school.db())

    fun addsStudentsInMultipleGrades() {
        school.add("Chelsea", 3)
        school.add("Logan", 7)

        val expected = mapOf(3 to listOf("Chelsea"), 7 to listOf("Logan"))
        assertEquals(expected, school.db())

    fun getsStudentsInAGrade() {
        school.add("Franklin", 5)
        school.add("Bradley", 5)
        school.add("Jeff", 1)

        val expected = mapOf(5 to listOf("Franklin", "Bradley"), 1 to listOf("Jeff"))
        assertEquals(expected, school.db())

    fun getsStudentsInEmptyGrade() {

    fun sortsSchool() {
        school.add("Jennifer", 4)
        school.add("Kareem", 6)
        school.add("Christopher", 4)
        school.add("Kyle", 3)

        val expected = mapOf(3 to listOf("Kyle"), 4 to listOf("Christopher", "Jennifer"), 6 to listOf("Kareem"))
        val sortedClasses = school.sort()
        assertEquals(listOf(3, 4, 6), sortedClasses.keys.toList(), "Grades not sorted in ascending order")
        assertEquals(expected, sortedClasses)

class School {

    private val students = mutableMapOf<Int, List<String>>()

    fun db(): Map<Int, List<String>> = students

    fun add(s: String, i: Int) {
        students[i] = grade(i).plus(s)

    fun grade(i: Int): List<String> = db()[i] ?: emptyList()

    fun sort(): Map<Int, List<String>> = students.mapValues {

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?