Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add grade-school exercise (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent 347e96c commit e7f2725
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@
],
"difficulty": 2
},
{
"slug": "grade-school",
"name": "Grade School",
"uuid": "f9b0bc81-ea0b-4007-8228-6bb520333289",
"practices": [],
"prerequisites": [
"numbers",
"strings"
],
"difficulty": 3
},
{
"slug": "grains",
"name": "Grains",
Expand Down
21 changes: 21 additions & 0 deletions exercises/practice/grade-school/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Instructions

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?"
- "Let me think.
We have Anna, Barb, and Charlie in grade 1, Alex, Peter, and Zoe in grade 2 and Jim in grade 5.
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim"

Note that all our students only have one name (It's a small town, what do you want?) and each student cannot be added more than once to a grade or the roster.
In fact, when a test attempts to add the same student more than once, your implementation should indicate that this is incorrect.
18 changes: 18 additions & 0 deletions exercises/practice/grade-school/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/grade_school.cljs"
],
"test": [
"test/grade_school_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Given students' names along with the grade that they are in, create a roster for the school.",
"source": "A pairing session with Phil Battos at gSchool"
}
19 changes: 19 additions & 0 deletions exercises/practice/grade-school/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns grade-school)

(defn grade
"show the roster for a specific grade"
[db grade-level]
(db grade-level []))

(defn add
"add student to roster for grade"
[db student grade-level]
(let [roster (grade db grade-level)]
(assoc db grade-level (conj roster student))))

(defn sorted
"show the sorted roster for each grade"
[db]
(into (sorted-map)
(for [[grade-level roster] db]
[grade-level (sort roster)])))
24 changes: 24 additions & 0 deletions exercises/practice/grade-school/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[6d0a30e4-1b4e-472e-8e20-c41702125667]
description = "Adding a student adds them to the sorted roster"

[233be705-dd58-4968-889d-fb3c7954c9cc]
description = "Adding more student adds them to the sorted roster"

[75a51579-d1d7-407c-a2f8-2166e984e8ab]
description = "Adding students to different grades adds them to the same sorted roster"

[a3f0fb58-f240-4723-8ddc-e644666b85cc]
description = "Roster returns an empty list if there are no students enrolled"

[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]
description = "Student names with grades are displayed in the same sorted roster"

[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]
description = "Grade returns the students in that grade in alphabetical order"

[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]
description = "Grade returns an empty list if there are no students in that grade"
10 changes: 10 additions & 0 deletions exercises/practice/grade-school/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
13 changes: 13 additions & 0 deletions exercises/practice/grade-school/src/grade_school.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns grade-school)

(defn grade [school grade] ;; <- arglist goes here
;; your code goes here
)

(defn add [school name grade] ;; <- arglist goes here
;; your code goes here
)

(defn sorted [school] ;; <- arglist goes here
;; your code goes here
)
53 changes: 53 additions & 0 deletions exercises/practice/grade-school/test/grade_school_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns grade-school-test
(:require [clojure.test :refer [deftest is]]
grade-school))

(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-existent-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)
(grade-school/sorted)))))

(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)
(grade-school/sorted)
(keys)))))

0 comments on commit e7f2725

Please sign in to comment.