mirror of
https://github.com/correl/elm.git
synced 2024-12-22 11:09:08 +00:00
Run elm-format over cherry picked exercises
This commit is contained in:
parent
5b8970a2db
commit
6e036afc7d
15 changed files with 383 additions and 172 deletions
|
@ -4,20 +4,33 @@ import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
import Accumulate exposing (accumulate)
|
import Accumulate exposing (accumulate)
|
||||||
|
|
||||||
import String
|
import String
|
||||||
|
|
||||||
|
|
||||||
square : Int -> Int
|
square : Int -> Int
|
||||||
square x = x * x
|
square x =
|
||||||
|
x * x
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "Accumulate"
|
tests =
|
||||||
[ test "[]] Accumulate" (assertEqual [] (accumulate square [])),
|
suite
|
||||||
test "square Accumulate" (assertEqual [1,4,9] (accumulate square [1,2,3])),
|
"Accumulate"
|
||||||
test "toUpper Accumulate" (assertEqual ["HELLO","WORLD"] (accumulate String.toUpper ["hello" , "world"])),
|
[ test
|
||||||
test "reverse Accumulate" (assertEqual ["olleh","dlrow"] (accumulate String.reverse ["hello" , "world"]))
|
"[]] Accumulate"
|
||||||
|
(assertEqual [] (accumulate square []))
|
||||||
|
, test
|
||||||
|
"square Accumulate"
|
||||||
|
(assertEqual [ 1, 4, 9 ] (accumulate square [ 1, 2, 3 ]))
|
||||||
|
, test
|
||||||
|
"toUpper Accumulate"
|
||||||
|
(assertEqual [ "HELLO", "WORLD" ] (accumulate String.toUpper [ "hello", "world" ]))
|
||||||
|
, test
|
||||||
|
"reverse Accumulate"
|
||||||
|
(assertEqual [ "olleh", "dlrow" ] (accumulate String.reverse [ "hello", "world" ]))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
|
@ -2,30 +2,47 @@ module GradeSchool (..) where
|
||||||
|
|
||||||
import Dict exposing (..)
|
import Dict exposing (..)
|
||||||
import Result exposing (toMaybe)
|
import Result exposing (toMaybe)
|
||||||
|
|
||||||
|
|
||||||
-- TODO: implement these classes
|
-- TODO: implement these classes
|
||||||
-- type Grade = Int
|
-- type Grade = Int
|
||||||
-- type Student = String
|
-- type Student = String
|
||||||
-- type School = Dict Int List String
|
-- type School = Dict Int List String
|
||||||
|
|
||||||
|
|
||||||
schoolFromList : List ( Int, String ) -> Dict Int (List String)
|
schoolFromList : List ( Int, String ) -> Dict Int (List String)
|
||||||
schoolFromList schoolList = List.foldl (\tuple -> \dict -> addStudent (fst tuple) (snd tuple) dict)
|
schoolFromList schoolList =
|
||||||
newSchool schoolList
|
List.foldl
|
||||||
|
(\tuple -> \dict -> addStudent (fst tuple) (snd tuple) dict)
|
||||||
|
newSchool
|
||||||
|
schoolList
|
||||||
|
|
||||||
|
|
||||||
schoolToList : Dict Int (List String) -> List ( Int, List String )
|
schoolToList : Dict Int (List String) -> List ( Int, List String )
|
||||||
schoolToList school = Dict.toList school
|
schoolToList school =
|
||||||
|
Dict.toList school
|
||||||
|
|
||||||
|
|
||||||
newSchool : Dict Int (List String)
|
newSchool : Dict Int (List String)
|
||||||
newSchool = Dict.empty
|
newSchool =
|
||||||
|
Dict.empty
|
||||||
|
|
||||||
|
|
||||||
addStudent : Int -> String -> Dict Int (List String) -> Dict Int (List String)
|
addStudent : Int -> String -> Dict Int (List String) -> Dict Int (List String)
|
||||||
addStudent grade student school =
|
addStudent grade student school =
|
||||||
Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school
|
Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school
|
||||||
|
|
||||||
|
|
||||||
gradeWithStudents : Int -> List String -> Dict Int (List String)
|
gradeWithStudents : Int -> List String -> Dict Int (List String)
|
||||||
gradeWithStudents grade students = Dict.singleton grade (List.sort students)
|
gradeWithStudents grade students =
|
||||||
|
Dict.singleton grade (List.sort students)
|
||||||
|
|
||||||
|
|
||||||
studentsInGrade : Int -> Dict Int (List String) -> List String
|
studentsInGrade : Int -> Dict Int (List String) -> List String
|
||||||
studentsInGrade grade school =
|
studentsInGrade grade school =
|
||||||
case (Dict.get grade school) of
|
case (Dict.get grade school) of
|
||||||
Just list -> list
|
Just list ->
|
||||||
Nothing -> []
|
list
|
||||||
|
|
||||||
|
Nothing ->
|
||||||
|
[]
|
||||||
|
|
|
@ -3,23 +3,41 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import GradeSchool as S exposing (..)
|
import GradeSchool as S exposing (..)
|
||||||
|
|
||||||
import Dict
|
import Dict
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "GradeSchool Test Suite"
|
tests =
|
||||||
[
|
suite
|
||||||
test "add student" (assertEqual [(2, ["Aimee"])]
|
"GradeSchool"
|
||||||
(S.schoolToList (S.addStudent 2 "Aimee" S.newSchool))),
|
[ test
|
||||||
test "add more students in same class" (assertEqual [(2, ["Blair", "James", "Paul"])]
|
"add student"
|
||||||
(S.schoolToList (S.gradeWithStudents 2 ["James", "Blair", "Paul"]))),
|
(assertEqual
|
||||||
test "add students to different grades" (assertEqual [(3, ["Chelsea"]), (7, ["Logan"])]
|
[ ( 2, [ "Aimee" ] ) ]
|
||||||
(S.schoolToList (S.schoolFromList [(3, "Chelsea"), (7, "Logan")]))),
|
(S.schoolToList (S.addStudent 2 "Aimee" S.newSchool))
|
||||||
test "get students in a grade" (assertEqual ["Bradley", "Franklin"]
|
)
|
||||||
(S.studentsInGrade 5 (S.schoolFromList [(5, "Franklin"), (5, "Bradley"), (1, "Jeff")]))),
|
, test
|
||||||
test "get students in a non-existent grade" (assertEqual [] (S.studentsInGrade 1 S.newSchool))
|
"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))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,5 +6,6 @@ helloWorld name =
|
||||||
case name of
|
case name of
|
||||||
Just name ->
|
Just name ->
|
||||||
"Hello, " ++ name ++ "!"
|
"Hello, " ++ name ++ "!"
|
||||||
|
|
||||||
Nothing ->
|
Nothing ->
|
||||||
"Hello, World!"
|
"Hello, World!"
|
||||||
|
|
|
@ -3,13 +3,16 @@ module NucleotideCount (..) where
|
||||||
import String
|
import String
|
||||||
import List
|
import List
|
||||||
|
|
||||||
|
|
||||||
nucleotideCounts : String -> List ( Char, Int )
|
nucleotideCounts : String -> List ( Char, Int )
|
||||||
nucleotideCounts sequence = [
|
nucleotideCounts sequence =
|
||||||
(getCount 'A' sequence),
|
[ (getCount 'A' sequence)
|
||||||
(getCount 'T' sequence),
|
, (getCount 'T' sequence)
|
||||||
(getCount 'C' sequence),
|
, (getCount 'C' sequence)
|
||||||
(getCount 'G' sequence)
|
, (getCount 'G' sequence)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
getCount : Char -> String -> ( Char, Int )
|
getCount : Char -> String -> ( Char, Int )
|
||||||
getCount base sequence = (base, (List.length (String.split (String.fromChar base) sequence)) - 1)
|
getCount base sequence =
|
||||||
|
( base, (List.length (String.split (String.fromChar base) sequence)) - 1 )
|
||||||
|
|
|
@ -3,20 +3,34 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import NucleotideCount exposing (nucleotideCounts)
|
import NucleotideCount exposing (nucleotideCounts)
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "NucleotideCount test suite"
|
tests =
|
||||||
[
|
suite
|
||||||
test "empty dna strand has no nucleotides" (assertEqual [('A', 0), ('T', 0), ('C', 0), ('G', 0)]
|
"NucleotideCount"
|
||||||
(nucleotideCounts "")),
|
[ test
|
||||||
test "repetitive-sequence-has-only-guanosine" (assertEqual [('A', 0), ('T', 0), ('C', 0), ('G', 8)]
|
"empty dna strand has no nucleotides"
|
||||||
(nucleotideCounts "GGGGGGGG")),
|
(assertEqual
|
||||||
test "counts all nucleotides" (assertEqual [('A', 20), ('T', 21), ('C', 12), ('G', 17)]
|
[ ( 'A', 0 ), ( 'T', 0 ), ( 'C', 0 ), ( 'G', 0 ) ]
|
||||||
(nucleotideCounts "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"))
|
(nucleotideCounts "")
|
||||||
|
)
|
||||||
|
, test
|
||||||
|
"repetitive-sequence-has-only-guanosine"
|
||||||
|
(assertEqual
|
||||||
|
[ ( 'A', 0 ), ( 'T', 0 ), ( 'C', 0 ), ( 'G', 8 ) ]
|
||||||
|
(nucleotideCounts "GGGGGGGG")
|
||||||
|
)
|
||||||
|
, test
|
||||||
|
"counts all nucleotides"
|
||||||
|
(assertEqual
|
||||||
|
[ ( 'A', 20 ), ( 'T', 21 ), ( 'C', 12 ), ( 'G', 17 ) ]
|
||||||
|
(nucleotideCounts "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC")
|
||||||
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
|
@ -3,27 +3,52 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import PhoneNumber exposing (getNumber, printPretty)
|
import PhoneNumber exposing (getNumber, printPretty)
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "PhoneNumber test suite"
|
tests =
|
||||||
[
|
suite
|
||||||
test "cleans number" (assertEqual "1234567890" (getNumber "(123) 456-7890")),
|
"PhoneNumber"
|
||||||
test "cleans number with dots" (assertEqual "1234567890" (getNumber "123.456.7890")),
|
[ test
|
||||||
test "valid when 11 digits and first is 1" (assertEqual "1234567890" (getNumber "11234567890")),
|
"cleans number"
|
||||||
test "invalid when 11 digits" (assertEqual "0000000000" (getNumber "21234567890")),
|
(assertEqual "1234567890" (getNumber "(123) 456-7890"))
|
||||||
test "invalid when 9 digits" (assertEqual "0000000000" (getNumber "123456789")),
|
, test
|
||||||
test "invalid when 12 digits" (assertEqual "0000000000" (getNumber "123456789012")),
|
"cleans number with dots"
|
||||||
test "invalid when empty" (assertEqual "0000000000" (getNumber "")),
|
(assertEqual "1234567890" (getNumber "123.456.7890"))
|
||||||
test "invalid when no digits present" (assertEqual "0000000000" (getNumber " (-) ")),
|
, test
|
||||||
test "valid with leading characters" (assertEqual "1234567890" (getNumber "my number is 123 456 7890")),
|
"valid when 11 digits and first is 1"
|
||||||
test "valid with trailing characters" (assertEqual "1234567890" (getNumber "123 456 7890 - bob")),
|
(assertEqual "1234567890" (getNumber "11234567890"))
|
||||||
test "pretty print" (assertEqual "(123) 456-7890" (printPretty "1234567890")),
|
, test
|
||||||
test "pretty print with full us phone number" (assertEqual "(123) 456-7890" (printPretty "11234567890"))
|
"invalid when 11 digits"
|
||||||
|
(assertEqual "0000000000" (getNumber "21234567890"))
|
||||||
|
, test
|
||||||
|
"invalid when 9 digits"
|
||||||
|
(assertEqual "0000000000" (getNumber "123456789"))
|
||||||
|
, test
|
||||||
|
"invalid when 12 digits"
|
||||||
|
(assertEqual "0000000000" (getNumber "123456789012"))
|
||||||
|
, test
|
||||||
|
"invalid when empty"
|
||||||
|
(assertEqual "0000000000" (getNumber ""))
|
||||||
|
, test
|
||||||
|
"invalid when no digits present"
|
||||||
|
(assertEqual "0000000000" (getNumber " (-) "))
|
||||||
|
, test
|
||||||
|
"valid with leading characters"
|
||||||
|
(assertEqual "1234567890" (getNumber "my number is 123 456 7890"))
|
||||||
|
, test
|
||||||
|
"valid with trailing characters"
|
||||||
|
(assertEqual "1234567890" (getNumber "123 456 7890 - bob"))
|
||||||
|
, test
|
||||||
|
"pretty print"
|
||||||
|
(assertEqual "(123) 456-7890" (printPretty "1234567890"))
|
||||||
|
, test
|
||||||
|
"pretty print with full us phone number"
|
||||||
|
(assertEqual "(123) 456-7890" (printPretty "11234567890"))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
module SpaceAge (..) where
|
module SpaceAge (..) where
|
||||||
|
|
||||||
type Planet = Mercury
|
|
||||||
|
type Planet
|
||||||
|
= Mercury
|
||||||
| Venus
|
| Venus
|
||||||
| Earth
|
| Earth
|
||||||
| Mars
|
| Mars
|
||||||
|
@ -10,19 +12,39 @@ type Planet = Mercury
|
||||||
| Neptune
|
| Neptune
|
||||||
|
|
||||||
|
|
||||||
earthYearInSeconds = 365.25 * 24 * 60 * 60
|
earthYearInSeconds =
|
||||||
|
365.25 * 24 * 60 * 60
|
||||||
|
|
||||||
|
|
||||||
ageOn : Planet -> Float -> Float
|
ageOn : Planet -> Float -> Float
|
||||||
ageOn planet seconds = seconds / (secondsPerYear planet)
|
ageOn planet seconds =
|
||||||
|
seconds / (secondsPerYear planet)
|
||||||
|
|
||||||
|
|
||||||
secondsPerYear : Planet -> Float
|
secondsPerYear : Planet -> Float
|
||||||
secondsPerYear planet =
|
secondsPerYear planet =
|
||||||
earthYearInSeconds * case planet of
|
earthYearInSeconds
|
||||||
Mercury -> 0.2408467
|
* case planet of
|
||||||
Venus -> 0.61519726
|
Mercury ->
|
||||||
Earth -> 1
|
0.2408467
|
||||||
Mars -> 1.8808158
|
|
||||||
Jupiter -> 11.862615
|
Venus ->
|
||||||
Saturn -> 29.447498
|
0.61519726
|
||||||
Uranus -> 84.016846
|
|
||||||
Neptune -> 164.79132
|
Earth ->
|
||||||
|
1
|
||||||
|
|
||||||
|
Mars ->
|
||||||
|
1.8808158
|
||||||
|
|
||||||
|
Jupiter ->
|
||||||
|
11.862615
|
||||||
|
|
||||||
|
Saturn ->
|
||||||
|
29.447498
|
||||||
|
|
||||||
|
Uranus ->
|
||||||
|
84.016846
|
||||||
|
|
||||||
|
Neptune ->
|
||||||
|
164.79132
|
||||||
|
|
|
@ -3,24 +3,24 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import SpaceAge exposing (Planet(..), ageOn)
|
import SpaceAge exposing (Planet(..), ageOn)
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "SpaceAge Test Suite"
|
tests =
|
||||||
[
|
suite
|
||||||
test "age in earth years" (assertEqual 32 (round (ageOn Earth 1000000000))),
|
"SpaceAge Test Suite"
|
||||||
test "age in mercury years" (assertEqual 281 (round (ageOn Mercury 2134835688))),
|
[ test "age in earth years" (assertEqual 32 (round (ageOn Earth 1000000000)))
|
||||||
test "age in venus years" (assertEqual 10 (round (ageOn Venus 189839836))),
|
, test "age in mercury years" (assertEqual 281 (round (ageOn Mercury 2134835688)))
|
||||||
test "age on mars" (assertEqual 39 (round (ageOn Mars 2329871239))),
|
, test "age in venus years" (assertEqual 10 (round (ageOn Venus 189839836)))
|
||||||
test "age on jupiter" (assertEqual 2 (round (ageOn Jupiter 901876382))),
|
, test "age on mars" (assertEqual 39 (round (ageOn Mars 2329871239)))
|
||||||
test "age on saturn" (assertEqual 3 (round (ageOn Saturn 3000000000))),
|
, test "age on jupiter" (assertEqual 2 (round (ageOn Jupiter 901876382)))
|
||||||
test "age on uranus" (assertEqual 1 (round (ageOn Uranus 3210123456))),
|
, test "age on saturn" (assertEqual 3 (round (ageOn Saturn 3000000000)))
|
||||||
test "age on neptune" (assertEqual 2 (round (ageOn Neptune 8210123456)))
|
, test "age on uranus" (assertEqual 1 (round (ageOn Uranus 3210123456)))
|
||||||
|
, test "age on neptune" (assertEqual 2 (round (ageOn Neptune 8210123456)))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,12 @@ module Strain (..) where
|
||||||
|
|
||||||
import List
|
import List
|
||||||
|
|
||||||
|
|
||||||
keep : (a -> Bool) -> List a -> List a
|
keep : (a -> Bool) -> List a -> List a
|
||||||
keep predicate list = List.filter predicate list
|
keep predicate list =
|
||||||
|
List.filter predicate list
|
||||||
|
|
||||||
|
|
||||||
discard : (a -> Bool) -> List a -> List a
|
discard : (a -> Bool) -> List a -> List a
|
||||||
discard predicate list = (keep (\val -> not (predicate val)) list)
|
discard predicate list =
|
||||||
|
(keep (\val -> not (predicate val)) list)
|
||||||
|
|
|
@ -3,33 +3,72 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import Strain exposing (keep, discard)
|
import Strain exposing (keep, discard)
|
||||||
|
|
||||||
import String
|
import String
|
||||||
|
|
||||||
even = \number -> number % 2 == 0
|
|
||||||
odd = \number -> number % 2 == 1
|
even =
|
||||||
isFirstLetter = \letter-> \word -> (String.left 1 word) == letter
|
\number -> number % 2 == 0
|
||||||
lessThanTen = \num -> num < 10
|
|
||||||
|
|
||||||
|
odd =
|
||||||
|
\number -> number % 2 == 1
|
||||||
|
|
||||||
|
|
||||||
|
isFirstLetter =
|
||||||
|
\letter -> \word -> (String.left 1 word) == letter
|
||||||
|
|
||||||
|
|
||||||
|
lessThanTen =
|
||||||
|
\num -> num < 10
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "Strain Test Suite"
|
tests =
|
||||||
[ test "empty keep" (assertEqual [] (keep lessThanTen [])),
|
suite
|
||||||
test "keep everything" (assertEqual [1, 2, 3] (keep lessThanTen [1, 2, 3])),
|
"Strain"
|
||||||
test "keep first and last" (assertEqual [1, 3] (keep odd [1, 2, 3])),
|
[ test
|
||||||
test "keep nothing" (assertEqual [] (keep even [1,3,5,7])),
|
"empty keep"
|
||||||
test "keep neither first nor last" (assertEqual [2] (keep even [1, 2, 3])),
|
(assertEqual [] (keep lessThanTen []))
|
||||||
test "keep strings" (assertEqual ["zebra", "zombies", "zealot"] (keep (isFirstLetter "z") ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"])),
|
, test
|
||||||
test "empty discard" (assertEqual [] (discard lessThanTen [])),
|
"keep everything"
|
||||||
test "discard everything" (assertEqual [] (discard lessThanTen [1, 2, 3])),
|
(assertEqual [ 1, 2, 3 ] (keep lessThanTen [ 1, 2, 3 ]))
|
||||||
test "discard first and last" (assertEqual [2] (discard odd [1, 2, 3])),
|
, test
|
||||||
test "discard nothing" (assertEqual [1,3,5,7] (discard even [1,3,5,7])),
|
"keep first and last"
|
||||||
test "discard neither first nor last" (assertEqual [1, 3] (discard even [1, 2, 3])),
|
(assertEqual [ 1, 3 ] (keep odd [ 1, 2, 3 ]))
|
||||||
test "discard strings" (assertEqual ["apple", "banana", "cherimoya"] (discard (isFirstLetter "z") ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]))
|
, test
|
||||||
|
"keep nothing"
|
||||||
|
(assertEqual [] (keep even [ 1, 3, 5, 7 ]))
|
||||||
|
, test
|
||||||
|
"keep neither first nor last"
|
||||||
|
(assertEqual [ 2 ] (keep even [ 1, 2, 3 ]))
|
||||||
|
, test
|
||||||
|
"keep strings"
|
||||||
|
(assertEqual
|
||||||
|
[ "zebra", "zombies", "zealot" ]
|
||||||
|
(keep (isFirstLetter "z") [ "apple", "zebra", "banana", "zombies", "cherimoya", "zealot" ])
|
||||||
|
)
|
||||||
|
, test
|
||||||
|
"empty discard"
|
||||||
|
(assertEqual [] (discard lessThanTen []))
|
||||||
|
, test
|
||||||
|
"discard everything"
|
||||||
|
(assertEqual [] (discard lessThanTen [ 1, 2, 3 ]))
|
||||||
|
, test
|
||||||
|
"discard first and last"
|
||||||
|
(assertEqual [ 2 ] (discard odd [ 1, 2, 3 ]))
|
||||||
|
, test
|
||||||
|
"discard nothing"
|
||||||
|
(assertEqual [ 1, 3, 5, 7 ] (discard even [ 1, 3, 5, 7 ]))
|
||||||
|
, test
|
||||||
|
"discard neither first nor last"
|
||||||
|
(assertEqual [ 1, 3 ] (discard even [ 1, 2, 3 ]))
|
||||||
|
, test
|
||||||
|
"discard strings"
|
||||||
|
(assertEqual [ "apple", "banana", "cherimoya" ] (discard (isFirstLetter "z") [ "apple", "zebra", "banana", "zombies", "cherimoya", "zealot" ]))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,35 @@
|
||||||
module Sublist (..) where
|
module Sublist (..) where
|
||||||
|
|
||||||
import List exposing (..)
|
import List exposing (..)
|
||||||
import String
|
import String
|
||||||
|
|
||||||
|
|
||||||
sublist : List a -> List a -> String
|
sublist : List a -> List a -> String
|
||||||
sublist alist blist =
|
sublist alist blist =
|
||||||
if alist == blist then "Equal" else
|
if alist == blist then
|
||||||
if inList alist blist then "Superlist" else
|
"Equal"
|
||||||
if inList blist alist then "Sublist" else "Unequal"
|
else if inList alist blist then
|
||||||
|
"Superlist"
|
||||||
|
else if inList blist alist then
|
||||||
|
"Sublist"
|
||||||
|
else
|
||||||
|
"Unequal"
|
||||||
|
|
||||||
|
|
||||||
inList : List a -> List a -> Bool
|
inList : List a -> List a -> Bool
|
||||||
inList alist blist =
|
inList alist blist =
|
||||||
let getLastInList sublist =
|
let
|
||||||
|
getLastInList sublist =
|
||||||
case (List.tail sublist) of
|
case (List.tail sublist) of
|
||||||
Just list -> list
|
Just list ->
|
||||||
Nothing -> []
|
list
|
||||||
|
|
||||||
|
Nothing ->
|
||||||
|
[]
|
||||||
in
|
in
|
||||||
if (List.length alist) < (List.length blist) then False else
|
if (List.length alist) < (List.length blist) then
|
||||||
if (List.take (List.length blist) alist) == blist then True else
|
False
|
||||||
|
else if (List.take (List.length blist) alist) == blist then
|
||||||
|
True
|
||||||
|
else
|
||||||
inList (getLastInList alist) blist
|
inList (getLastInList alist) blist
|
||||||
|
|
|
@ -3,32 +3,70 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import Sublist exposing (sublist)
|
import Sublist exposing (sublist)
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
tests = suite "Sublist Test Suite"
|
tests =
|
||||||
[ test " empty equals empty" (assertEqual "Equal" (sublist [] [])),
|
suite
|
||||||
test "empty is a sublist of anything" (assertEqual "Sublist" (sublist [] [1,2])),
|
"Sublist"
|
||||||
test "anything is a superlist of empty" (assertEqual "Superlist" (sublist [1,2] [] )),
|
[ test
|
||||||
test "1 is not 2" (assertEqual "Unequal" (sublist [1] [2])),
|
"empty equals empty"
|
||||||
test "compare larger equal lists" (assertEqual "Equal" (sublist [1, 1, 1] [1, 1, 1])),
|
(assertEqual "Equal" (sublist [] []))
|
||||||
test "sublist at start" (assertEqual "Sublist" (sublist [1,2,3] [1,2,3,4,5])),
|
, test
|
||||||
test "sublist in the middle" (assertEqual "Sublist" (sublist [4,3,2] [5,4,3,2,1])),
|
"empty is a sublist of anything"
|
||||||
test "sublist at end" (assertEqual "Sublist" (sublist [3,4,5] [1,2,3,4,5])),
|
(assertEqual "Sublist" (sublist [] [ 1, 2 ]))
|
||||||
test "partially matching sublist at start" (assertEqual "Sublist" (sublist [1,1,2] [1,1,1,2])),
|
, test
|
||||||
test "sublist early in huge list" (assertEqual "Sublist" (sublist [3,4,5] [1 .. 100000])),
|
"anything is a superlist of empty"
|
||||||
test "huge sublist not in list" (assertEqual "Unequal" (sublist [10 .. 100001] [1 .. 100000])),
|
(assertEqual "Superlist" (sublist [ 1, 2 ] []))
|
||||||
test "superlist at start" (assertEqual "Superlist" (sublist [1,2,3,4,5] [1,2,3])),
|
, test
|
||||||
test "superlist in middle" (assertEqual "Superlist" (sublist [5,4,3,2,1] [4,3,2])),
|
"1 is not 2"
|
||||||
test "superlist at end" (assertEqual "Superlist" (sublist [1,2,3,4,5] [3,4,5])),
|
(assertEqual "Unequal" (sublist [ 1 ] [ 2 ]))
|
||||||
test "partially matching superlist at start" (assertEqual "Superlist" (sublist [1,1,1,2] [1,1,2])),
|
, test
|
||||||
test "superlist early in huge list" (assertEqual "Superlist" (sublist [1 .. 100000] [3,4,5])),
|
"compare larger equal lists"
|
||||||
test "recurring values sublist" (assertEqual "Sublist" (sublist [1,2,1,2,3] [1,2,3,1,2,1,2,3,2,1])),
|
(assertEqual "Equal" (sublist [ 1, 1, 1 ] [ 1, 1, 1 ]))
|
||||||
test "recurring values unequal" (assertEqual "Unequal" (sublist [1,2,1,2,3] [1,2,3,1,2,3,2,3,2,1]))
|
, test
|
||||||
|
"sublist at start"
|
||||||
|
(assertEqual "Sublist" (sublist [ 1, 2, 3 ] [ 1, 2, 3, 4, 5 ]))
|
||||||
|
, test
|
||||||
|
"sublist in the middle"
|
||||||
|
(assertEqual "Sublist" (sublist [ 4, 3, 2 ] [ 5, 4, 3, 2, 1 ]))
|
||||||
|
, test
|
||||||
|
"sublist at end"
|
||||||
|
(assertEqual "Sublist" (sublist [ 3, 4, 5 ] [ 1, 2, 3, 4, 5 ]))
|
||||||
|
, test
|
||||||
|
"partially matching sublist at start"
|
||||||
|
(assertEqual "Sublist" (sublist [ 1, 1, 2 ] [ 1, 1, 1, 2 ]))
|
||||||
|
, test
|
||||||
|
"sublist early in huge list"
|
||||||
|
(assertEqual "Sublist" (sublist [ 3, 4, 5 ] [1..100000]))
|
||||||
|
, test
|
||||||
|
"huge sublist not in list"
|
||||||
|
(assertEqual "Unequal" (sublist [10..100001] [1..100000]))
|
||||||
|
, test
|
||||||
|
"superlist at start"
|
||||||
|
(assertEqual "Superlist" (sublist [ 1, 2, 3, 4, 5 ] [ 1, 2, 3 ]))
|
||||||
|
, test
|
||||||
|
"superlist in middle"
|
||||||
|
(assertEqual "Superlist" (sublist [ 5, 4, 3, 2, 1 ] [ 4, 3, 2 ]))
|
||||||
|
, test
|
||||||
|
"superlist at end"
|
||||||
|
(assertEqual "Superlist" (sublist [ 1, 2, 3, 4, 5 ] [ 3, 4, 5 ]))
|
||||||
|
, test
|
||||||
|
"partially matching superlist at start"
|
||||||
|
(assertEqual "Superlist" (sublist [ 1, 1, 1, 2 ] [ 1, 1, 2 ]))
|
||||||
|
, test
|
||||||
|
"superlist early in huge list"
|
||||||
|
(assertEqual "Superlist" (sublist [1..100000] [ 3, 4, 5 ]))
|
||||||
|
, test
|
||||||
|
"recurring values sublist"
|
||||||
|
(assertEqual "Sublist" (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 1, 2, 3, 2, 1 ]))
|
||||||
|
, test
|
||||||
|
"recurring values unequal"
|
||||||
|
(assertEqual "Unequal" (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 3, 2, 3, 2, 1 ]))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
module SumOfMultiples (..) where
|
module SumOfMultiples (..) where
|
||||||
|
|
||||||
sumOfMultiples : List Int -> Int -> Int
|
|
||||||
sumOfMultiples factors upperLimit =
|
|
||||||
List.sum (List.filter (isMultipleOfAnyFactor factors) [1 .. (upperLimit - 1)])
|
|
||||||
|
|
||||||
isMultipleOfAnyFactor : List Int -> Int -> Bool
|
sumOfMultiples : List Int -> Int -> Int
|
||||||
isMultipleOfAnyFactor factors candidate =
|
sumOfMultiples multiples limit =
|
||||||
List.any (\factor -> candidate % factor == 0) factors
|
List.sum (List.filter (inMultiples multiples) [1..(limit - 1)])
|
||||||
|
|
||||||
|
|
||||||
|
inMultiples : List Int -> Int -> Bool
|
||||||
|
inMultiples multiples candidate =
|
||||||
|
List.any (\factor -> candidate % factor == 0) multiples
|
||||||
|
|
|
@ -3,21 +3,22 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
|
|
||||||
import SumOfMultiples exposing (sumOfMultiples)
|
import SumOfMultiples exposing (sumOfMultiples)
|
||||||
|
|
||||||
tests : Test
|
|
||||||
tests = suite "Sum Of Multiples Test Suite"
|
|
||||||
[ test "[3, 5] 15" (assertEqual 45 (sumOfMultiples [3, 5] 15)),
|
|
||||||
test "[7, 13, 17] 20" (assertEqual 51 (sumOfMultiples [7, 13, 17] 20)),
|
|
||||||
test "[4, 6] 15" (assertEqual 30 (sumOfMultiples [4, 6] 15)),
|
|
||||||
test "[5, 6, 8] 150" (assertEqual 4419 (sumOfMultiples [5, 6, 8] 150)),
|
|
||||||
test "[43, 47] 10000" (assertEqual 2203160 (sumOfMultiples [43, 47] 10000)),
|
|
||||||
test "[5, 25] 51" (assertEqual 275 (sumOfMultiples [5, 25] 51))
|
|
||||||
|
|
||||||
|
tests : Test
|
||||||
|
tests =
|
||||||
|
suite
|
||||||
|
"Sum Of Multiples"
|
||||||
|
[ test "[3, 5] 15" (assertEqual 45 (sumOfMultiples [ 3, 5 ] 15))
|
||||||
|
, test "[7, 13, 17] 20" (assertEqual 51 (sumOfMultiples [ 7, 13, 17 ] 20))
|
||||||
|
, test "[4, 6] 15" (assertEqual 30 (sumOfMultiples [ 4, 6 ] 15))
|
||||||
|
, test "[5, 6, 8] 150" (assertEqual 4419 (sumOfMultiples [ 5, 6, 8 ] 150))
|
||||||
|
, test "[43, 47] 10000" (assertEqual 2203160 (sumOfMultiples [ 43, 47 ] 10000))
|
||||||
|
, test "[5, 25] 51" (assertEqual 275 (sumOfMultiples [ 5, 25 ] 51))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
port runner : Signal (Task.Task x ())
|
port runner : Signal (Task.Task x ())
|
||||||
port runner =
|
port runner =
|
||||||
Console.run (consoleRunner tests)
|
Console.run (consoleRunner tests)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue