mirror of
https://github.com/correl/elm.git
synced 2024-12-22 03:00:07 +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,19 +4,32 @@ import Task
|
|||
import Console
|
||||
import ElmTest exposing (..)
|
||||
import Accumulate exposing (accumulate)
|
||||
|
||||
import String
|
||||
|
||||
|
||||
square : Int -> Int
|
||||
square x = x * x
|
||||
square x =
|
||||
x * x
|
||||
|
||||
|
||||
tests : Test
|
||||
tests = suite "Accumulate"
|
||||
[ test "[]] 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"]))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"Accumulate"
|
||||
[ test
|
||||
"[]] 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 =
|
||||
|
|
|
@ -2,30 +2,47 @@ module GradeSchool (..) 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
|
||||
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
|
||||
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)
|
||||
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 -> []
|
||||
Just list ->
|
||||
list
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
|
|
|
@ -3,24 +3,42 @@ module Main (..) where
|
|||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
import GradeSchool as S exposing (..)
|
||||
|
||||
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))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"GradeSchool"
|
||||
[ 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))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
|
|
|
@ -5,6 +5,7 @@ helloWorld : Maybe String -> String
|
|||
helloWorld name =
|
||||
case name of
|
||||
Just name ->
|
||||
"Hello, " ++ name ++ "!"
|
||||
"Hello, " ++ name ++ "!"
|
||||
|
||||
Nothing ->
|
||||
"Hello, World!"
|
||||
|
|
|
@ -3,13 +3,16 @@ module NucleotideCount (..) where
|
|||
import String
|
||||
import List
|
||||
|
||||
nucleotideCounts : String -> List (Char, Int)
|
||||
nucleotideCounts sequence = [
|
||||
(getCount 'A' sequence),
|
||||
(getCount 'T' sequence),
|
||||
(getCount 'C' sequence),
|
||||
(getCount 'G' sequence)
|
||||
|
||||
nucleotideCounts : String -> List ( Char, Int )
|
||||
nucleotideCounts sequence =
|
||||
[ (getCount 'A' sequence)
|
||||
, (getCount 'T' sequence)
|
||||
, (getCount 'C' sequence)
|
||||
, (getCount 'G' sequence)
|
||||
]
|
||||
|
||||
getCount : Char -> String -> (Char, Int)
|
||||
getCount base sequence = (base, (List.length (String.split (String.fromChar base) sequence)) - 1)
|
||||
|
||||
getCount : Char -> String -> ( Char, Int )
|
||||
getCount base sequence =
|
||||
( base, (List.length (String.split (String.fromChar base) sequence)) - 1 )
|
||||
|
|
|
@ -3,19 +3,33 @@ module Main (..) where
|
|||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
import NucleotideCount exposing (nucleotideCounts)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests = suite "NucleotideCount test suite"
|
||||
[
|
||||
test "empty dna strand has no nucleotides" (assertEqual [('A', 0), ('T', 0), ('C', 0), ('G', 0)]
|
||||
(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"))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"NucleotideCount"
|
||||
[ test
|
||||
"empty dna strand has no nucleotides"
|
||||
(assertEqual
|
||||
[ ( 'A', 0 ), ( 'T', 0 ), ( 'C', 0 ), ( 'G', 0 ) ]
|
||||
(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 =
|
||||
|
|
|
@ -3,27 +3,52 @@ module Main (..) where
|
|||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
import PhoneNumber exposing (getNumber, printPretty)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests = suite "PhoneNumber test suite"
|
||||
[
|
||||
test "cleans number" (assertEqual "1234567890" (getNumber "(123) 456-7890")),
|
||||
test "cleans number with dots" (assertEqual "1234567890" (getNumber "123.456.7890")),
|
||||
test "valid when 11 digits and first is 1" (assertEqual "1234567890" (getNumber "11234567890")),
|
||||
test "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"))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"PhoneNumber"
|
||||
[ test
|
||||
"cleans number"
|
||||
(assertEqual "1234567890" (getNumber "(123) 456-7890"))
|
||||
, test
|
||||
"cleans number with dots"
|
||||
(assertEqual "1234567890" (getNumber "123.456.7890"))
|
||||
, test
|
||||
"valid when 11 digits and first is 1"
|
||||
(assertEqual "1234567890" (getNumber "11234567890"))
|
||||
, test
|
||||
"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 =
|
||||
Console.run (consoleRunner tests)
|
||||
|
||||
|
|
|
@ -1,28 +1,50 @@
|
|||
module SpaceAge (..) where
|
||||
|
||||
type Planet = Mercury
|
||||
| Venus
|
||||
| Earth
|
||||
| Mars
|
||||
| Jupiter
|
||||
| Saturn
|
||||
| Uranus
|
||||
| Neptune
|
||||
|
||||
type Planet
|
||||
= Mercury
|
||||
| Venus
|
||||
| Earth
|
||||
| Mars
|
||||
| Jupiter
|
||||
| Saturn
|
||||
| Uranus
|
||||
| Neptune
|
||||
|
||||
|
||||
earthYearInSeconds = 365.25 * 24 * 60 * 60
|
||||
earthYearInSeconds =
|
||||
365.25 * 24 * 60 * 60
|
||||
|
||||
|
||||
ageOn : Planet -> Float -> Float
|
||||
ageOn planet seconds = seconds / (secondsPerYear planet)
|
||||
ageOn planet seconds =
|
||||
seconds / (secondsPerYear planet)
|
||||
|
||||
|
||||
secondsPerYear : Planet -> Float
|
||||
secondsPerYear planet =
|
||||
earthYearInSeconds * case planet of
|
||||
Mercury -> 0.2408467
|
||||
Venus -> 0.61519726
|
||||
Earth -> 1
|
||||
Mars -> 1.8808158
|
||||
Jupiter -> 11.862615
|
||||
Saturn -> 29.447498
|
||||
Uranus -> 84.016846
|
||||
Neptune -> 164.79132
|
||||
earthYearInSeconds
|
||||
* case planet of
|
||||
Mercury ->
|
||||
0.2408467
|
||||
|
||||
Venus ->
|
||||
0.61519726
|
||||
|
||||
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 Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
import SpaceAge exposing (Planet(..), ageOn)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests = suite "SpaceAge Test Suite"
|
||||
[
|
||||
test "age in earth years" (assertEqual 32 (round (ageOn Earth 1000000000))),
|
||||
test "age in mercury years" (assertEqual 281 (round (ageOn Mercury 2134835688))),
|
||||
test "age in venus years" (assertEqual 10 (round (ageOn Venus 189839836))),
|
||||
test "age on mars" (assertEqual 39 (round (ageOn Mars 2329871239))),
|
||||
test "age on jupiter" (assertEqual 2 (round (ageOn Jupiter 901876382))),
|
||||
test "age on saturn" (assertEqual 3 (round (ageOn Saturn 3000000000))),
|
||||
test "age on uranus" (assertEqual 1 (round (ageOn Uranus 3210123456))),
|
||||
test "age on neptune" (assertEqual 2 (round (ageOn Neptune 8210123456)))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"SpaceAge Test Suite"
|
||||
[ test "age in earth years" (assertEqual 32 (round (ageOn Earth 1000000000)))
|
||||
, test "age in mercury years" (assertEqual 281 (round (ageOn Mercury 2134835688)))
|
||||
, test "age in venus years" (assertEqual 10 (round (ageOn Venus 189839836)))
|
||||
, test "age on mars" (assertEqual 39 (round (ageOn Mars 2329871239)))
|
||||
, test "age on jupiter" (assertEqual 2 (round (ageOn Jupiter 901876382)))
|
||||
, test "age on saturn" (assertEqual 3 (round (ageOn Saturn 3000000000)))
|
||||
, 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 =
|
||||
Console.run (consoleRunner tests)
|
||||
|
||||
|
|
|
@ -2,8 +2,12 @@ module Strain (..) where
|
|||
|
||||
import List
|
||||
|
||||
keep : (a -> Bool) -> List a -> List a
|
||||
keep predicate list = List.filter predicate list
|
||||
|
||||
keep : (a -> Bool) -> List a -> List a
|
||||
keep predicate list =
|
||||
List.filter predicate list
|
||||
|
||||
|
||||
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 Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
import Strain exposing (keep, discard)
|
||||
|
||||
import String
|
||||
|
||||
even = \number -> number % 2 == 0
|
||||
odd = \number -> number % 2 == 1
|
||||
isFirstLetter = \letter-> \word -> (String.left 1 word) == letter
|
||||
lessThanTen = \num -> num < 10
|
||||
|
||||
even =
|
||||
\number -> number % 2 == 0
|
||||
|
||||
|
||||
odd =
|
||||
\number -> number % 2 == 1
|
||||
|
||||
|
||||
isFirstLetter =
|
||||
\letter -> \word -> (String.left 1 word) == letter
|
||||
|
||||
|
||||
lessThanTen =
|
||||
\num -> num < 10
|
||||
|
||||
|
||||
tests : Test
|
||||
tests = suite "Strain Test Suite"
|
||||
[ test "empty keep" (assertEqual [] (keep lessThanTen [])),
|
||||
test "keep everything" (assertEqual [1, 2, 3] (keep lessThanTen [1, 2, 3])),
|
||||
test "keep first and last" (assertEqual [1, 3] (keep odd [1, 2, 3])),
|
||||
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"]))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"Strain"
|
||||
[ test
|
||||
"empty keep"
|
||||
(assertEqual [] (keep lessThanTen []))
|
||||
, test
|
||||
"keep everything"
|
||||
(assertEqual [ 1, 2, 3 ] (keep lessThanTen [ 1, 2, 3 ]))
|
||||
, test
|
||||
"keep first and last"
|
||||
(assertEqual [ 1, 3 ] (keep odd [ 1, 2, 3 ]))
|
||||
, 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 =
|
||||
Console.run (consoleRunner tests)
|
||||
|
||||
|
|
|
@ -1,21 +1,35 @@
|
|||
module Sublist (..) where
|
||||
|
||||
import List exposing (..)
|
||||
import String
|
||||
|
||||
|
||||
sublist : List a -> List a -> String
|
||||
sublist alist blist =
|
||||
if alist == blist then "Equal" else
|
||||
if inList alist blist then "Superlist" else
|
||||
if inList blist alist then "Sublist" else "Unequal"
|
||||
if alist == blist then
|
||||
"Equal"
|
||||
else if inList alist blist then
|
||||
"Superlist"
|
||||
else if inList blist alist then
|
||||
"Sublist"
|
||||
else
|
||||
"Unequal"
|
||||
|
||||
|
||||
inList : List a -> List a -> Bool
|
||||
inList alist blist =
|
||||
let getLastInList sublist =
|
||||
case (List.tail sublist) of
|
||||
Just list -> list
|
||||
Nothing -> []
|
||||
let
|
||||
getLastInList sublist =
|
||||
case (List.tail sublist) of
|
||||
Just list ->
|
||||
list
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
in
|
||||
if (List.length alist) < (List.length blist) then False else
|
||||
if (List.take (List.length blist) alist) == blist then True else
|
||||
inList (getLastInList alist) blist
|
||||
if (List.length alist) < (List.length blist) then
|
||||
False
|
||||
else if (List.take (List.length blist) alist) == blist then
|
||||
True
|
||||
else
|
||||
inList (getLastInList alist) blist
|
||||
|
|
|
@ -3,32 +3,70 @@ module Main (..) where
|
|||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
import Sublist exposing (sublist)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests = suite "Sublist Test Suite"
|
||||
[ test " empty equals empty" (assertEqual "Equal" (sublist [] [])),
|
||||
test "empty is a sublist of anything" (assertEqual "Sublist" (sublist [] [1,2])),
|
||||
test "anything is a superlist of empty" (assertEqual "Superlist" (sublist [1,2] [] )),
|
||||
test "1 is not 2" (assertEqual "Unequal" (sublist [1] [2])),
|
||||
test "compare larger equal lists" (assertEqual "Equal" (sublist [1, 1, 1] [1, 1, 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]))
|
||||
]
|
||||
tests =
|
||||
suite
|
||||
"Sublist"
|
||||
[ test
|
||||
"empty equals empty"
|
||||
(assertEqual "Equal" (sublist [] []))
|
||||
, test
|
||||
"empty is a sublist of anything"
|
||||
(assertEqual "Sublist" (sublist [] [ 1, 2 ]))
|
||||
, test
|
||||
"anything is a superlist of empty"
|
||||
(assertEqual "Superlist" (sublist [ 1, 2 ] []))
|
||||
, test
|
||||
"1 is not 2"
|
||||
(assertEqual "Unequal" (sublist [ 1 ] [ 2 ]))
|
||||
, test
|
||||
"compare larger equal lists"
|
||||
(assertEqual "Equal" (sublist [ 1, 1, 1 ] [ 1, 1, 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 =
|
||||
Console.run (consoleRunner tests)
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
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
|
||||
isMultipleOfAnyFactor factors candidate =
|
||||
List.any (\factor -> candidate % factor == 0) factors
|
||||
sumOfMultiples : List Int -> Int -> Int
|
||||
sumOfMultiples multiples limit =
|
||||
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 Console
|
||||
import ElmTest exposing (..)
|
||||
|
||||
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 =
|
||||
Console.run (consoleRunner tests)
|
||||
|
||||
|
|
Loading…
Reference in a new issue