From 4b5ff7afb36a71bdf49098bab77b0f8cd51d5822 Mon Sep 17 00:00:00 2001 From: Claire Thompson <cthompson@pivotal.io> Date: Wed, 14 Oct 2015 12:02:13 -0700 Subject: [PATCH] Add GradeSchool example and test --- GradeSchool/GradeSchoolExample.elm | 31 ++++++++++++++++++++++++++++++ GradeSchool/GradeSchoolTest.elm | 31 ++++++++++++++++++++++++++++++ elm-package.json | 3 ++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 GradeSchool/GradeSchoolExample.elm create mode 100644 GradeSchool/GradeSchoolTest.elm diff --git a/GradeSchool/GradeSchoolExample.elm b/GradeSchool/GradeSchoolExample.elm new file mode 100644 index 0000000..d71a261 --- /dev/null +++ b/GradeSchool/GradeSchoolExample.elm @@ -0,0 +1,31 @@ +module GradeSchoolExample where + +import Dict exposing (..) +import Result exposing (toMaybe) +-- TODO: implement these classes +-- type Grade = Int +-- type Student = String +-- type School = Dict Int List String + +schoolFromList : List (Int, String) -> Dict Int (List String) +schoolFromList schoolList = List.foldl (\tuple -> \dict -> addStudent (fst tuple) (snd tuple) dict) + newSchool schoolList + +schoolToList : Dict Int (List String) -> List (Int, List String) +schoolToList school = Dict.toList school + +newSchool : Dict Int (List String) +newSchool = Dict.empty + +addStudent : Int -> String -> Dict Int (List String) -> Dict Int (List String) +addStudent grade student school = + Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school + +gradeWithStudents : Int -> List String -> Dict Int (List String) +gradeWithStudents grade students = Dict.singleton grade (List.sort students) + +studentsInGrade : Int -> Dict Int (List String) -> List String +studentsInGrade grade school = + case (Dict.get grade school) of + Just list -> list + Nothing -> [] diff --git a/GradeSchool/GradeSchoolTest.elm b/GradeSchool/GradeSchoolTest.elm new file mode 100644 index 0000000..ad253bc --- /dev/null +++ b/GradeSchool/GradeSchoolTest.elm @@ -0,0 +1,31 @@ +module GradeSchoolTest where + +-- TODO - remove example inclusion once Problem sets are ready to go live or CI is set up. + +-- import GradeSchoolExample exposing (addStudent, +-- newSchool, gradeWithStudents, schoolFromList, +-- studentsInGrade, schoolToList) + +import GradeSchoolExample as S exposing (..) + +import ElmTest.Test exposing (test, Test, suite) +import ElmTest.Assertion exposing (assert, assertEqual) +import ElmTest.Runner.Element exposing (runDisplay) + +import Dict + +tests : Test +tests = suite "GradeSchool Test Suite" + [ + test "add student" (assertEqual [(2, ["Aimee"])] + (S.schoolToList (S.addStudent 2 "Aimee" S.newSchool))), + test "add more students in same class" (assertEqual [(2, ["Blair", "James", "Paul"])] + (S.schoolToList (S.gradeWithStudents 2 ["James", "Blair", "Paul"]))), + test "add students to different grades" (assertEqual [(3, ["Chelsea"]), (7, ["Logan"])] + (S.schoolToList (S.schoolFromList [(3, "Chelsea"), (7, "Logan")]))), + test "get students in a grade" (assertEqual ["Bradley", "Franklin"] + (S.studentsInGrade 5 (S.schoolFromList [(5, "Franklin"), (5, "Bradley"), (1, "Jeff")]))), + test "get students in a non-existent grade" (assertEqual [] (S.studentsInGrade 1 S.newSchool)) + ] + +main = runDisplay tests diff --git a/elm-package.json b/elm-package.json index 0d6af7f..f0c0019 100644 --- a/elm-package.json +++ b/elm-package.json @@ -27,7 +27,8 @@ "SpaceAge", "Anagram", "NucleotideCount", - "PhoneNumber" + "PhoneNumber", + "GradeSchool" ], "exposed-modules": [], "dependencies": {