Avatar of 4d47

4d47's solution

to Grade School in the Clojure Track

Published at Jul 13 2018 · 1 comment
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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?


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.


(ns grade-school-test
  (:require [clojure.test :refer [deftest is]]

(def db {})

(deftest add-student
  (is (= {2 ["Aimee"]} (grade-school/add db "Aimee" 2))))

(deftest add-more-students-in-same-class
  (is (= {2 ["James", "Blair", "Paul"]}
         (-> db
             (grade-school/add "James" 2)
             (grade-school/add "Blair" 2)
             (grade-school/add "Paul" 2)))))

(deftest add-students-to-different-grades
  (is (= {3 ["Chelsea"] 7 ["Logan"]}
         (-> db
             (grade-school/add "Chelsea" 3)
             (grade-school/add "Logan" 7)))))

(deftest get-students-in-a-grade
  (is (= ["Franklin", "Bradley"]
         (-> db
             (grade-school/add "Franklin" 5)
             (grade-school/add "Bradley" 5)
             (grade-school/add "Jeff" 1)
             (grade-school/grade 5)))))

(deftest get-students-in-a-non-existant-grade
  (is (= [] (grade-school/grade db 1))))

(deftest sorted-grade-school
  (is (= (sorted-map 3 ["Kyle"]
                     4 ["Christopher" "Jennifer"]
                     6 ["Kareem"] )
         (-> db
             (grade-school/add "Jennifer" 4)
             (grade-school/add "Kareem" 6)
             (grade-school/add "Christopher" 4)
             (grade-school/add "Kyle" 3)

(deftest sorted-grade_school-keys-sorted
  (is (= [3 4 6]
         (-> db
             (grade-school/add "Jennifer" 4)
             (grade-school/add "Kareem" 6)
             (grade-school/add "Christopher" 4)
             (grade-school/add "Kyle" 3)
(ns grade-school)

(defn grade [db level]
  (get db level []))

(defn add [db name level]
  (assoc db level (conj (grade db level) name)))

(defn sorted [db]
  (into (sorted-map) (for [[k v] db] [k (sort v)])))

Community comments

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

Nice one liner in sorted. I knew there had to be a really simple and elegant way to do that, but I didn't manage to figure it out for myself, sorted-map vs. sorted-by vs. sorted-map-by confused the heck out of me.

I also managed to miss the fact that you can pass a default value to get, so that's good to know!

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?