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:
Note that all our students only have one name. (It's a small town, what do you want?)
For library documentation, follow Useful OCaml resources.
A Makefile
is provided with a default target to compile your solution and run the tests. At the command line, type:
make
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.
If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!
A pairing session with Phil Battos at gSchool http://gschool.it
(* grade-school - 1.0.0 *)
open OUnit2
open Grade_school
let assert_lists_equal exp got _ctx =
let printer = String.concat ";" in
assert_equal exp got ~printer
let tests = [
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
"Adding a student adds them to the sorted roster" >:: (
let school = empty_school |> add "Aimee" 2 in
assert_lists_equal ["Aimee"] (roster school)
);
"Adding more student adds them to the sorted roster" >:: (
let school = empty_school |> add "Blair" 2 |> add "James" 2 |> add "Paul" 2 in
assert_lists_equal ["Blair"; "James"; "Paul"] (roster school)
);
"Adding students to different grades adds them to the same sorted roster" >:: (
let school = empty_school |> add "Chelsea" 3 |> add "Logan" 7 in
assert_lists_equal ["Chelsea"; "Logan"] (roster school)
);
"Roster returns an empty list if there are no students enrolled" >:: (
let school = empty_school in
assert_lists_equal [] (roster school)
);
"Student names with grades are displayed in the same sorted roster" >:: (
let school = empty_school
|> add "Peter" 2
|> add "Anna" 1
|> add "Barb" 1
|> add "Zoe" 2
|> add "Alex" 2
|> add "Jim" 3
|> add "Charlie" 1 in
assert_lists_equal ["Anna"; "Barb"; "Charlie"; "Alex"; "Peter"; "Zoe"; "Jim"] (roster school)
);
"Grade returns the students in that grade in alphabetical order" >:: (
let school = empty_school |> add "Franklin" 5 |> add "Bradley" 5 |> add "Jeff" 1 in
assert_lists_equal ["Bradley"; "Franklin"] (grade 5 school)
);
"Grade returns an empty list if there are no students in that grade" >:: (
let school = empty_school in
assert_lists_equal [] (grade 1 school)
);
(* "an empty school has no children in grade 1" >::
assert_lists_equal [] (grade 1 empty_school);
"an empty school has Emma in grade 1 after she's been added to grade 1" >::(
let s = add "Emma" 1 empty_school in
assert_lists_equal ["Emma"] (grade 1 s)
);
"an empty school does not have Emma in grade 1 after she's been added to grade 2" >::(
let s = add "Emma" 2 empty_school in
assert_lists_equal [] (grade 1 s)
);
"an empty school has Emma, Timmy, Bob and Becky in grade 1 after they've been added to grade 1" >::(
let s = empty_school |> add "Emma" 1 |> add "Timmy" 1 |> add "Bob" 1 |> add "Becky" 1 in
assert_lists_equal ["Becky"; "Bob"; "Emma"; "Timmy"] (grade 1 s |> List.sort compare)
);
"a sorted school has Emma, Timmy, Bob and Becky in grade 1 in sorted order" >::(
let s = empty_school |> add "Emma" 1 |> add "Timmy" 1 |> add "Bob" 1 |> add "Becky" 1 |> sorted in
assert_lists_equal ["Becky"; "Bob"; "Emma"; "Timmy"] (grade 1 s)
); *)
]
let () =
run_test_tt_main ("grade-school tests" >::: tests)
open Base
module Int_map = Map.M(Int)
type school = string list Int_map.t
let empty_school = Map.empty (module Int)
let grade g smap =
match Map.find smap g with
| Some names -> names
| None -> []
let add name g smap =
Map.set smap ~key:g ~data:(name::(grade g smap))
let sorted smap =
Map.map smap ~f:(List.sort ~compare:(String.compare))
let roster smap =
sorted smap |> Map.data |> List.concat
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.
Level up your programming skills with 3,429 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments