mirror of
https://github.com/correl/elm.git
synced 2024-12-18 11:06:17 +00:00
Run pre Elm 0.17 exercises through elm-format version 0.3.0-alpha
This commit is contained in:
parent
65db27e7e0
commit
91aaf056a1
49 changed files with 1178 additions and 1469 deletions
|
@ -3,4 +3,4 @@ module Accumulate exposing (..)
|
|||
|
||||
accumulate : (a -> b) -> List a -> List b
|
||||
accumulate func input =
|
||||
List.foldr (\v c -> func v :: c) [] input
|
||||
List.foldr (\v c -> func v :: c) [] input
|
||||
|
|
|
@ -7,28 +7,23 @@ import String
|
|||
|
||||
square : Int -> Int
|
||||
square x =
|
||||
x * 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" ]))
|
||||
]
|
||||
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" ]))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -6,25 +6,25 @@ import Bitwise
|
|||
|
||||
isAllergicTo : String -> Int -> Bool
|
||||
isAllergicTo name score =
|
||||
List.member name (toList score)
|
||||
List.member name (toList score)
|
||||
|
||||
|
||||
toList : Int -> List String
|
||||
toList score =
|
||||
allergies
|
||||
|> List.indexedMap (\i n -> ( Bitwise.shiftLeft 1 i, n ))
|
||||
|> List.filter (\( s, n ) -> Bitwise.and s score > 0)
|
||||
|> List.map snd
|
||||
allergies
|
||||
|> List.indexedMap (\i n -> ( Bitwise.shiftLeft 1 i, n ))
|
||||
|> List.filter (\( s, n ) -> Bitwise.and s score > 0)
|
||||
|> List.map snd
|
||||
|
||||
|
||||
allergies : List String
|
||||
allergies =
|
||||
[ "eggs"
|
||||
, "peanuts"
|
||||
, "shellfish"
|
||||
, "strawberries"
|
||||
, "tomatoes"
|
||||
, "chocolate"
|
||||
, "pollen"
|
||||
, "cats"
|
||||
]
|
||||
[ "eggs"
|
||||
, "peanuts"
|
||||
, "shellfish"
|
||||
, "strawberries"
|
||||
, "tomatoes"
|
||||
, "chocolate"
|
||||
, "pollen"
|
||||
, "cats"
|
||||
]
|
||||
|
|
|
@ -7,65 +7,46 @@ import List
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Allergies"
|
||||
[ suite
|
||||
"isAllergicTo"
|
||||
[ suite
|
||||
"no allergies means not allergic"
|
||||
[ test
|
||||
"peanuts"
|
||||
(assert (not (isAllergicTo "peanuts" 0)))
|
||||
, test
|
||||
"cats"
|
||||
(assert (not (isAllergicTo "cats" 0)))
|
||||
, test
|
||||
"strawberries"
|
||||
(assert (not (isAllergicTo "strawberries" 0)))
|
||||
suite "Allergies"
|
||||
[ suite "isAllergicTo"
|
||||
[ suite "no allergies means not allergic"
|
||||
[ test "peanuts"
|
||||
(assert (not (isAllergicTo "peanuts" 0)))
|
||||
, test "cats"
|
||||
(assert (not (isAllergicTo "cats" 0)))
|
||||
, test "strawberries"
|
||||
(assert (not (isAllergicTo "strawberries" 0)))
|
||||
]
|
||||
, test "is allergic to eggs"
|
||||
(assert (isAllergicTo "eggs" 1))
|
||||
, suite "has the right allergies"
|
||||
[ test "eggs"
|
||||
(assert (isAllergicTo "eggs" 5))
|
||||
, test "shellfish"
|
||||
(assert (isAllergicTo "shellfish" 5))
|
||||
, test "strawberries"
|
||||
(assert (not (isAllergicTo "strawberries" 5)))
|
||||
]
|
||||
]
|
||||
, test
|
||||
"is allergic to eggs"
|
||||
(assert (isAllergicTo "eggs" 1))
|
||||
, suite
|
||||
"has the right allergies"
|
||||
[ test
|
||||
"eggs"
|
||||
(assert (isAllergicTo "eggs" 5))
|
||||
, test
|
||||
"shellfish"
|
||||
(assert (isAllergicTo "shellfish" 5))
|
||||
, test
|
||||
"strawberries"
|
||||
(assert (not (isAllergicTo "strawberries" 5)))
|
||||
, suite "toList"
|
||||
[ test "no allergies at all"
|
||||
(assertEqual [] (toList (0)))
|
||||
, test "allergic to just peanuts"
|
||||
(assertEqual [ "peanuts" ] (toList (2)))
|
||||
, test "allergic to everything"
|
||||
(assertEqual (List.sort [ "eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats" ])
|
||||
(255 |> toList |> List.sort)
|
||||
)
|
||||
, test "ignore non allergen score parts"
|
||||
(assertEqual [ "eggs" ] (toList 257))
|
||||
, test "ignore non allergen score parts"
|
||||
(assertEqual (List.sort [ "eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats" ])
|
||||
(509 |> toList |> List.sort)
|
||||
)
|
||||
]
|
||||
]
|
||||
, suite
|
||||
"toList"
|
||||
[ test
|
||||
"no allergies at all"
|
||||
(assertEqual [] (toList (0)))
|
||||
, test
|
||||
"allergic to just peanuts"
|
||||
(assertEqual [ "peanuts" ] (toList (2)))
|
||||
, test
|
||||
"allergic to everything"
|
||||
(assertEqual
|
||||
(List.sort [ "eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats" ])
|
||||
(255 |> toList |> List.sort)
|
||||
)
|
||||
, test
|
||||
"ignore non allergen score parts"
|
||||
(assertEqual [ "eggs" ] (toList 257))
|
||||
, test
|
||||
"ignore non allergen score parts"
|
||||
(assertEqual
|
||||
(List.sort [ "eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats" ])
|
||||
(509 |> toList |> List.sort)
|
||||
)
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,16 +5,16 @@ import String exposing (toLower, toList)
|
|||
|
||||
detect : String -> List String -> List String
|
||||
detect word candidates =
|
||||
let
|
||||
original =
|
||||
toLower word
|
||||
let
|
||||
original =
|
||||
toLower word
|
||||
|
||||
ref =
|
||||
normalize word
|
||||
in
|
||||
List.filter (\w -> normalize w == ref && toLower w /= original) candidates
|
||||
ref =
|
||||
normalize word
|
||||
in
|
||||
List.filter (\w -> normalize w == ref && toLower w /= original) candidates
|
||||
|
||||
|
||||
normalize : String -> List Char
|
||||
normalize word =
|
||||
word |> toLower |> toList |> List.sort
|
||||
word |> toLower |> toList |> List.sort
|
||||
|
|
|
@ -6,131 +6,90 @@ import Anagram exposing (detect)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Anagram"
|
||||
[ test
|
||||
"no matches"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "diaper" [ "hello", "world", "zombies", "pants" ])
|
||||
)
|
||||
, test
|
||||
"detects simple anagram"
|
||||
(assertEqual
|
||||
[ "tan" ]
|
||||
(detect "ant" [ "tan", "stand", "at" ])
|
||||
)
|
||||
, test
|
||||
"does not detect false positives"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "galea" [ "eagle" ])
|
||||
)
|
||||
, test
|
||||
"detects multiple anagrams"
|
||||
(assertEqual
|
||||
[ "stream", "maters" ]
|
||||
(detect "master" [ "stream", "pigeon", "maters" ])
|
||||
)
|
||||
, test
|
||||
"does not detect anagram subsets"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "good" [ "dog", "goody" ])
|
||||
)
|
||||
, test
|
||||
"detects anagram"
|
||||
(assertEqual
|
||||
[ "inlets" ]
|
||||
(detect "listen" [ "enlists", "google", "inlets", "banana" ])
|
||||
)
|
||||
, test
|
||||
"detects multiple anagrams"
|
||||
(assertEqual
|
||||
[ "gallery", "regally", "largely" ]
|
||||
(detect "allergy" [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ])
|
||||
)
|
||||
, test
|
||||
"does not detect indentical words"
|
||||
(assertEqual
|
||||
[ "cron" ]
|
||||
(detect "corn" [ "corn", "dark", "Corn", "rank", "CORN", "cron", "park" ])
|
||||
)
|
||||
, test
|
||||
"does not detect non-anagrams with identical checksum"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "mass" [ "last" ])
|
||||
)
|
||||
, test
|
||||
"detects anagrams case-insensitively"
|
||||
(assertEqual
|
||||
[ "Carthorse" ]
|
||||
(detect "Orchestra" [ "cashregister", "Carthorse", "radishes" ])
|
||||
)
|
||||
, test
|
||||
"detects anagrams using case-insensitive subject"
|
||||
(assertEqual
|
||||
[ "carthorse" ]
|
||||
(detect "Orchestra" [ "cashregister", "carthorse", "radishes" ])
|
||||
)
|
||||
, test
|
||||
"detects anagrams using case-insensitve possible matches"
|
||||
(assertEqual
|
||||
[ "Carthorse" ]
|
||||
(detect "orchestra" [ "cashregister", "Carthorse", "radishes" ])
|
||||
)
|
||||
, test
|
||||
"does not detect a word as its own anagram"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "banana" [ "Banana" ])
|
||||
)
|
||||
, test
|
||||
"does not detect a anagram if the original word is repeated"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "go" [ "go Go GO" ])
|
||||
)
|
||||
, test
|
||||
"anagrams must use all letters exactly once"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "tapper" [ "patter" ])
|
||||
)
|
||||
, test
|
||||
"eliminates anagrams with the same checksum"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "mass" [ "last" ])
|
||||
)
|
||||
, test
|
||||
"detects unicode anagrams"
|
||||
(assertEqual
|
||||
[ "ΒΓΑ", "γβα" ]
|
||||
(detect "ΑΒΓ" [ "ΒΓΑ", "ΒΓΔ", "γβα" ])
|
||||
)
|
||||
, test
|
||||
"eliminates misleading unicode anagrams"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "ΑΒΓ" [ "ABΓ" ])
|
||||
)
|
||||
, test
|
||||
"capital word is not own anagram"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "BANANA" [ "Banana" ])
|
||||
)
|
||||
, test
|
||||
"anagrams must use all letters exactly once"
|
||||
(assertEqual
|
||||
[]
|
||||
(detect "patter" [ "tapper" ])
|
||||
)
|
||||
]
|
||||
suite "Anagram"
|
||||
[ test "no matches"
|
||||
(assertEqual []
|
||||
(detect "diaper" [ "hello", "world", "zombies", "pants" ])
|
||||
)
|
||||
, test "detects simple anagram"
|
||||
(assertEqual [ "tan" ]
|
||||
(detect "ant" [ "tan", "stand", "at" ])
|
||||
)
|
||||
, test "does not detect false positives"
|
||||
(assertEqual []
|
||||
(detect "galea" [ "eagle" ])
|
||||
)
|
||||
, test "detects multiple anagrams"
|
||||
(assertEqual [ "stream", "maters" ]
|
||||
(detect "master" [ "stream", "pigeon", "maters" ])
|
||||
)
|
||||
, test "does not detect anagram subsets"
|
||||
(assertEqual []
|
||||
(detect "good" [ "dog", "goody" ])
|
||||
)
|
||||
, test "detects anagram"
|
||||
(assertEqual [ "inlets" ]
|
||||
(detect "listen" [ "enlists", "google", "inlets", "banana" ])
|
||||
)
|
||||
, test "detects multiple anagrams"
|
||||
(assertEqual [ "gallery", "regally", "largely" ]
|
||||
(detect "allergy" [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ])
|
||||
)
|
||||
, test "does not detect indentical words"
|
||||
(assertEqual [ "cron" ]
|
||||
(detect "corn" [ "corn", "dark", "Corn", "rank", "CORN", "cron", "park" ])
|
||||
)
|
||||
, test "does not detect non-anagrams with identical checksum"
|
||||
(assertEqual []
|
||||
(detect "mass" [ "last" ])
|
||||
)
|
||||
, test "detects anagrams case-insensitively"
|
||||
(assertEqual [ "Carthorse" ]
|
||||
(detect "Orchestra" [ "cashregister", "Carthorse", "radishes" ])
|
||||
)
|
||||
, test "detects anagrams using case-insensitive subject"
|
||||
(assertEqual [ "carthorse" ]
|
||||
(detect "Orchestra" [ "cashregister", "carthorse", "radishes" ])
|
||||
)
|
||||
, test "detects anagrams using case-insensitve possible matches"
|
||||
(assertEqual [ "Carthorse" ]
|
||||
(detect "orchestra" [ "cashregister", "Carthorse", "radishes" ])
|
||||
)
|
||||
, test "does not detect a word as its own anagram"
|
||||
(assertEqual []
|
||||
(detect "banana" [ "Banana" ])
|
||||
)
|
||||
, test "does not detect a anagram if the original word is repeated"
|
||||
(assertEqual []
|
||||
(detect "go" [ "go Go GO" ])
|
||||
)
|
||||
, test "anagrams must use all letters exactly once"
|
||||
(assertEqual []
|
||||
(detect "tapper" [ "patter" ])
|
||||
)
|
||||
, test "eliminates anagrams with the same checksum"
|
||||
(assertEqual []
|
||||
(detect "mass" [ "last" ])
|
||||
)
|
||||
, test "detects unicode anagrams"
|
||||
(assertEqual [ "ΒΓΑ", "γβα" ]
|
||||
(detect "ΑΒΓ" [ "ΒΓΑ", "ΒΓΔ", "γβα" ])
|
||||
)
|
||||
, test "eliminates misleading unicode anagrams"
|
||||
(assertEqual []
|
||||
(detect "ΑΒΓ" [ "ABΓ" ])
|
||||
)
|
||||
, test "capital word is not own anagram"
|
||||
(assertEqual []
|
||||
(detect "BANANA" [ "Banana" ])
|
||||
)
|
||||
, test "anagrams must use all letters exactly once"
|
||||
(assertEqual []
|
||||
(detect "patter" [ "tapper" ])
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -6,41 +6,41 @@ import Regex
|
|||
|
||||
hey : String -> String
|
||||
hey remark =
|
||||
if isShouting remark then
|
||||
"Whoa, chill out!"
|
||||
else if isQuestion remark then
|
||||
"Sure."
|
||||
else if isSilence remark then
|
||||
"Fine. Be that way!"
|
||||
else
|
||||
"Whatever."
|
||||
if isShouting remark then
|
||||
"Whoa, chill out!"
|
||||
else if isQuestion remark then
|
||||
"Sure."
|
||||
else if isSilence remark then
|
||||
"Fine. Be that way!"
|
||||
else
|
||||
"Whatever."
|
||||
|
||||
|
||||
isShouting : String -> Bool
|
||||
isShouting remark =
|
||||
isUppercase remark && hasCharacters remark
|
||||
isUppercase remark && hasCharacters remark
|
||||
|
||||
|
||||
isUppercase : String -> Bool
|
||||
isUppercase remark =
|
||||
remark == String.toUpper remark
|
||||
remark == String.toUpper remark
|
||||
|
||||
|
||||
isQuestion : String -> Bool
|
||||
isQuestion remark =
|
||||
String.endsWith "?" remark
|
||||
String.endsWith "?" remark
|
||||
|
||||
|
||||
hasCharacters : String -> Bool
|
||||
hasCharacters remark =
|
||||
Regex.contains characterRegex remark
|
||||
Regex.contains characterRegex remark
|
||||
|
||||
|
||||
characterRegex : Regex.Regex
|
||||
characterRegex =
|
||||
Regex.regex "[a-zA-Z]"
|
||||
Regex.regex "[a-zA-Z]"
|
||||
|
||||
|
||||
isSilence : String -> Bool
|
||||
isSilence remark =
|
||||
String.isEmpty (String.trim remark)
|
||||
String.isEmpty (String.trim remark)
|
||||
|
|
|
@ -9,66 +9,65 @@ import Bob
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Bob"
|
||||
[ test "stating something" (assertEqual "Whatever." (Bob.hey "Tom-ay-to, tom-aaaah-to."))
|
||||
, test "shouting" (assertEqual "Whoa, chill out!" (Bob.hey "WATCH OUT!"))
|
||||
, test "shouting gibberish" (assertEqual "Whoa, chill out!" (Bob.hey (uppercaseGibberish 10)))
|
||||
, test "asking a question" (assertEqual "Sure." (Bob.hey "Does this cryogenic chamber make me look fat?"))
|
||||
, test "asking a numeric question" (assertEqual "Sure." (Bob.hey "You are, what, like 15?"))
|
||||
, test "asking gibberish" (assertEqual "Sure." (Bob.hey (gibberishQuestion 20)))
|
||||
, test "talking forcefully" (assertEqual "Whatever." (Bob.hey "Let's go make out behind the gym!"))
|
||||
, test "using acronyms in regular speech" (assertEqual "Whatever." (Bob.hey "It's OK if you don't want to go to the DMV."))
|
||||
, test "forceful questions" (assertEqual "Whoa, chill out!" (Bob.hey "WHAT THE HELL WERE YOU THINKING?"))
|
||||
, test "shouting numbers" (assertEqual "Whoa, chill out!" (Bob.hey "1, 2, 3 GO!"))
|
||||
, test "only numbers" (assertEqual "Whatever." (Bob.hey "1, 2, 3"))
|
||||
, test "question with only numbers" (assertEqual "Sure." (Bob.hey "4?"))
|
||||
, test "shouting with special characters" (assertEqual "Whoa, chill out!" (Bob.hey "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!)"))
|
||||
, test "shouting with no exclamation mark" (assertEqual "Whoa, chill out!" (Bob.hey "I HATE YOU"))
|
||||
, test "statement containing a question mark" (assertEqual "Whatever." (Bob.hey "Ending with ? means a question."))
|
||||
, test "prattling on" (assertEqual "Sure." (Bob.hey "Wait! Hang on. Are you going to be OK?"))
|
||||
, test "silence" (assertEqual "Fine. Be that way!" (Bob.hey ""))
|
||||
, test "prolonged silence" (assertEqual "Fine. Be that way!" (Bob.hey " "))
|
||||
, test "alternate silences" (assertEqual "Fine. Be that way!" (Bob.hey "\t \n \t "))
|
||||
, test "on multiple line questions" (assertEqual "Whatever." (Bob.hey "\nDoes this cryogenic chamber make me look fat?\nno"))
|
||||
]
|
||||
suite "Bob"
|
||||
[ test "stating something" (assertEqual "Whatever." (Bob.hey "Tom-ay-to, tom-aaaah-to."))
|
||||
, test "shouting" (assertEqual "Whoa, chill out!" (Bob.hey "WATCH OUT!"))
|
||||
, test "shouting gibberish" (assertEqual "Whoa, chill out!" (Bob.hey (uppercaseGibberish 10)))
|
||||
, test "asking a question" (assertEqual "Sure." (Bob.hey "Does this cryogenic chamber make me look fat?"))
|
||||
, test "asking a numeric question" (assertEqual "Sure." (Bob.hey "You are, what, like 15?"))
|
||||
, test "asking gibberish" (assertEqual "Sure." (Bob.hey (gibberishQuestion 20)))
|
||||
, test "talking forcefully" (assertEqual "Whatever." (Bob.hey "Let's go make out behind the gym!"))
|
||||
, test "using acronyms in regular speech" (assertEqual "Whatever." (Bob.hey "It's OK if you don't want to go to the DMV."))
|
||||
, test "forceful questions" (assertEqual "Whoa, chill out!" (Bob.hey "WHAT THE HELL WERE YOU THINKING?"))
|
||||
, test "shouting numbers" (assertEqual "Whoa, chill out!" (Bob.hey "1, 2, 3 GO!"))
|
||||
, test "only numbers" (assertEqual "Whatever." (Bob.hey "1, 2, 3"))
|
||||
, test "question with only numbers" (assertEqual "Sure." (Bob.hey "4?"))
|
||||
, test "shouting with special characters" (assertEqual "Whoa, chill out!" (Bob.hey "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!)"))
|
||||
, test "shouting with no exclamation mark" (assertEqual "Whoa, chill out!" (Bob.hey "I HATE YOU"))
|
||||
, test "statement containing a question mark" (assertEqual "Whatever." (Bob.hey "Ending with ? means a question."))
|
||||
, test "prattling on" (assertEqual "Sure." (Bob.hey "Wait! Hang on. Are you going to be OK?"))
|
||||
, test "silence" (assertEqual "Fine. Be that way!" (Bob.hey ""))
|
||||
, test "prolonged silence" (assertEqual "Fine. Be that way!" (Bob.hey " "))
|
||||
, test "alternate silences" (assertEqual "Fine. Be that way!" (Bob.hey "\t \n \t "))
|
||||
, test "on multiple line questions" (assertEqual "Whatever." (Bob.hey "\nDoes this cryogenic chamber make me look fat?\nno"))
|
||||
]
|
||||
|
||||
|
||||
character : Int -> Int -> Random.Generator Char
|
||||
character start end =
|
||||
Random.map Char.fromCode (Random.int start end)
|
||||
Random.map Char.fromCode (Random.int start end)
|
||||
|
||||
|
||||
anyCharacter : Random.Generator Char
|
||||
anyCharacter =
|
||||
character 32 126
|
||||
character 32 126
|
||||
|
||||
|
||||
uppercaseCharacter : Random.Generator Char
|
||||
uppercaseCharacter =
|
||||
character 65 90
|
||||
character 65 90
|
||||
|
||||
|
||||
listOfCharacters : Int -> Random.Generator Char -> Random.Generator (List Char)
|
||||
listOfCharacters length characterList =
|
||||
Random.list length characterList
|
||||
Random.list length characterList
|
||||
|
||||
|
||||
gibberish : Int -> Random.Generator Char -> String
|
||||
gibberish length characterList =
|
||||
fst (Random.step (Random.map String.fromList (listOfCharacters length characterList)) (Random.initialSeed 424242))
|
||||
fst (Random.step (Random.map String.fromList (listOfCharacters length characterList)) (Random.initialSeed 424242))
|
||||
|
||||
|
||||
uppercaseGibberish : Int -> String
|
||||
uppercaseGibberish length =
|
||||
gibberish length uppercaseCharacter
|
||||
gibberish length uppercaseCharacter
|
||||
|
||||
|
||||
gibberishQuestion : Int -> String
|
||||
gibberishQuestion length =
|
||||
(gibberish length anyCharacter) ++ "?"
|
||||
(gibberish length anyCharacter) ++ "?"
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -3,18 +3,18 @@ module DifferenceOfSquares exposing (..)
|
|||
|
||||
squareOfSum : Int -> Int
|
||||
squareOfSum n =
|
||||
let
|
||||
sum =
|
||||
n * (n + 1) // 2
|
||||
in
|
||||
sum * sum
|
||||
let
|
||||
sum =
|
||||
n * (n + 1) // 2
|
||||
in
|
||||
sum * sum
|
||||
|
||||
|
||||
sumOfSquares : Int -> Int
|
||||
sumOfSquares n =
|
||||
List.sum (List.map (\m -> m * m) [0..n])
|
||||
List.sum (List.map (\m -> m * m) [0..n])
|
||||
|
||||
|
||||
difference : Int -> Int
|
||||
difference n =
|
||||
squareOfSum n - sumOfSquares n
|
||||
squareOfSum n - sumOfSquares n
|
||||
|
|
|
@ -6,30 +6,26 @@ import DifferenceOfSquares exposing (squareOfSum, sumOfSquares, difference)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"DifferenceOfSquares"
|
||||
[ suite
|
||||
"square the sum of the numbers up to the given number"
|
||||
[ test "square of sum 5" (assertEqual 225 (squareOfSum 5))
|
||||
, test "square of sum 10" (assertEqual 3025 (squareOfSum 10))
|
||||
, test "square of sum 100" (assertEqual 25502500 (squareOfSum 100))
|
||||
suite "DifferenceOfSquares"
|
||||
[ suite "square the sum of the numbers up to the given number"
|
||||
[ test "square of sum 5" (assertEqual 225 (squareOfSum 5))
|
||||
, test "square of sum 10" (assertEqual 3025 (squareOfSum 10))
|
||||
, test "square of sum 100" (assertEqual 25502500 (squareOfSum 100))
|
||||
]
|
||||
, suite "sum the squares of the numbers up to the given number"
|
||||
[ test "sum of squares 5" (assertEqual 55 (sumOfSquares 5))
|
||||
, test "sum of squares 10" (assertEqual 385 (sumOfSquares 10))
|
||||
, test "sum of squares 100" (assertEqual 338350 (sumOfSquares 100))
|
||||
]
|
||||
, suite "subtract sum of squares from square of sums"
|
||||
[ test "difference of squares 0" (assertEqual 0 (difference 0))
|
||||
, test "difference of squares 5" (assertEqual 170 (difference 5))
|
||||
, test "difference of squares 10" (assertEqual 2640 (difference 10))
|
||||
, test "difference of squares 100" (assertEqual 25164150 (difference 100))
|
||||
]
|
||||
]
|
||||
, suite
|
||||
"sum the squares of the numbers up to the given number"
|
||||
[ test "sum of squares 5" (assertEqual 55 (sumOfSquares 5))
|
||||
, test "sum of squares 10" (assertEqual 385 (sumOfSquares 10))
|
||||
, test "sum of squares 100" (assertEqual 338350 (sumOfSquares 100))
|
||||
]
|
||||
, suite
|
||||
"subtract sum of squares from square of sums"
|
||||
[ test "difference of squares 0" (assertEqual 0 (difference 0))
|
||||
, test "difference of squares 5" (assertEqual 170 (difference 5))
|
||||
, test "difference of squares 10" (assertEqual 2640 (difference 10))
|
||||
, test "difference of squares 100" (assertEqual 25164150 (difference 100))
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -4,37 +4,37 @@ import Dict exposing (..)
|
|||
|
||||
|
||||
type alias Grade =
|
||||
Int
|
||||
Int
|
||||
|
||||
|
||||
type alias Student =
|
||||
String
|
||||
String
|
||||
|
||||
|
||||
type alias School =
|
||||
Dict Int (List Student)
|
||||
Dict Int (List Student)
|
||||
|
||||
|
||||
empty : School
|
||||
empty =
|
||||
Dict.empty
|
||||
Dict.empty
|
||||
|
||||
|
||||
addStudent : Grade -> Student -> School -> 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
|
||||
|
||||
|
||||
studentsInGrade : Grade -> School -> List Student
|
||||
studentsInGrade grade school =
|
||||
case (Dict.get grade school) of
|
||||
Just list ->
|
||||
list
|
||||
case (Dict.get grade school) of
|
||||
Just list ->
|
||||
list
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
Nothing ->
|
||||
[]
|
||||
|
||||
|
||||
allStudents : School -> List ( Grade, List Student )
|
||||
allStudents school =
|
||||
Dict.toList school |> List.sortBy fst
|
||||
Dict.toList school |> List.sortBy fst
|
||||
|
|
|
@ -6,70 +6,58 @@ import GradeSchool exposing (addStudent, studentsInGrade, allStudents)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"GradeSchool"
|
||||
[ test
|
||||
"add student"
|
||||
(assertEqual
|
||||
[ "Aimee" ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 2 "Aimee"
|
||||
|> studentsInGrade 2
|
||||
)
|
||||
)
|
||||
, test
|
||||
"add more students in same class"
|
||||
(assertEqual
|
||||
[ "Blair", "James", "Paul" ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 2 "James"
|
||||
|> addStudent 2 "Blair"
|
||||
|> addStudent 2 "Paul"
|
||||
|> studentsInGrade 2
|
||||
)
|
||||
)
|
||||
, test
|
||||
"add students to different grades"
|
||||
(assertEqual
|
||||
[ [ "Chelsea" ], [ "Logan" ] ]
|
||||
(let
|
||||
school =
|
||||
GradeSchool.empty
|
||||
|> addStudent 3 "Chelsea"
|
||||
|> addStudent 7 "Logan"
|
||||
in
|
||||
[ studentsInGrade 3 school, studentsInGrade 7 school ]
|
||||
)
|
||||
)
|
||||
, test
|
||||
"get students in a grade"
|
||||
(assertEqual
|
||||
[ "Bradley", "Franklin" ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 5 "Franklin"
|
||||
|> addStudent 5 "Bradley"
|
||||
|> addStudent 1 "Jeff"
|
||||
|> studentsInGrade 5
|
||||
)
|
||||
)
|
||||
, test
|
||||
"get all students in the school"
|
||||
(assertEqual
|
||||
[ ( 3, [ "Kyle" ] ), ( 4, [ "Christopher", "Jennifer" ] ), ( 6, [ "Kareem" ] ) ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 4 "Jennifer"
|
||||
|> addStudent 6 "Kareem"
|
||||
|> addStudent 4 "Christopher"
|
||||
|> addStudent 3 "Kyle"
|
||||
|> allStudents
|
||||
)
|
||||
)
|
||||
, test
|
||||
"get students in a non-existent grade"
|
||||
(assertEqual [] (studentsInGrade 1 GradeSchool.empty))
|
||||
]
|
||||
suite "GradeSchool"
|
||||
[ test "add student"
|
||||
(assertEqual [ "Aimee" ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 2 "Aimee"
|
||||
|> studentsInGrade 2
|
||||
)
|
||||
)
|
||||
, test "add more students in same class"
|
||||
(assertEqual [ "Blair", "James", "Paul" ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 2 "James"
|
||||
|> addStudent 2 "Blair"
|
||||
|> addStudent 2 "Paul"
|
||||
|> studentsInGrade 2
|
||||
)
|
||||
)
|
||||
, test "add students to different grades"
|
||||
(assertEqual [ [ "Chelsea" ], [ "Logan" ] ]
|
||||
(let
|
||||
school =
|
||||
GradeSchool.empty
|
||||
|> addStudent 3 "Chelsea"
|
||||
|> addStudent 7 "Logan"
|
||||
in
|
||||
[ studentsInGrade 3 school, studentsInGrade 7 school ]
|
||||
)
|
||||
)
|
||||
, test "get students in a grade"
|
||||
(assertEqual [ "Bradley", "Franklin" ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 5 "Franklin"
|
||||
|> addStudent 5 "Bradley"
|
||||
|> addStudent 1 "Jeff"
|
||||
|> studentsInGrade 5
|
||||
)
|
||||
)
|
||||
, test "get all students in the school"
|
||||
(assertEqual [ ( 3, [ "Kyle" ] ), ( 4, [ "Christopher", "Jennifer" ] ), ( 6, [ "Kareem" ] ) ]
|
||||
(GradeSchool.empty
|
||||
|> addStudent 4 "Jennifer"
|
||||
|> addStudent 6 "Kareem"
|
||||
|> addStudent 4 "Christopher"
|
||||
|> addStudent 3 "Kyle"
|
||||
|> allStudents
|
||||
)
|
||||
)
|
||||
, test "get students in a non-existent grade"
|
||||
(assertEqual [] (studentsInGrade 1 GradeSchool.empty))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,10 +5,10 @@ import String exposing (length, toList)
|
|||
|
||||
distance : String -> String -> Maybe Int
|
||||
distance left right =
|
||||
if length left /= length right then
|
||||
Nothing
|
||||
else
|
||||
List.map2 (\l r -> l /= r) (toList left) (toList right)
|
||||
|> List.filter identity
|
||||
|> List.length
|
||||
|> Just
|
||||
if length left /= length right then
|
||||
Nothing
|
||||
else
|
||||
List.map2 (\l r -> l /= r) (toList left) (toList right)
|
||||
|> List.filter identity
|
||||
|> List.length
|
||||
|> Just
|
||||
|
|
|
@ -6,53 +6,38 @@ import Hamming exposing (distance)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Hamming"
|
||||
[ test
|
||||
"identical strands"
|
||||
(assertEqual (Just 0) (distance "A" "A"))
|
||||
, test
|
||||
"long identical strands"
|
||||
(assertEqual (Just 0) (distance "GGACTGA" "GGACTGA"))
|
||||
, test
|
||||
"complete distance in single nucleotide strands"
|
||||
(assertEqual (Just 1) (distance "A" "G"))
|
||||
, test
|
||||
"complete distance in small strands"
|
||||
(assertEqual (Just 2) (distance "AG" "CT"))
|
||||
, test
|
||||
"small distance in small strands"
|
||||
(assertEqual (Just 1) (distance "AT" "CT"))
|
||||
, test
|
||||
"small distance"
|
||||
(assertEqual (Just 1) (distance "GGACG" "GGTCG"))
|
||||
, test
|
||||
"small distance in long strands"
|
||||
(assertEqual (Just 2) (distance "ACCAGGG" "ACTATGG"))
|
||||
, test
|
||||
"non-unique character in first strand"
|
||||
(assertEqual (Just 1) (distance "AGA" "AGG"))
|
||||
, test
|
||||
"non-unique character in second strand"
|
||||
(assertEqual (Just 1) (distance "AGG" "AGA"))
|
||||
, test
|
||||
"large distance"
|
||||
(assertEqual (Just 4) (distance "GATACA" "GCATAA"))
|
||||
, test
|
||||
"large distance in off-by-one strand"
|
||||
(assertEqual (Just 9) (distance "GGACGGATTCTG" "AGGACGGATTCT"))
|
||||
, test
|
||||
"empty strands"
|
||||
(assertEqual (Just 0) (distance "" ""))
|
||||
, test
|
||||
"disallow first strand longer"
|
||||
(assertEqual Nothing (distance "AATG" "AAA"))
|
||||
, test
|
||||
"disallow second strand longer"
|
||||
(assertEqual Nothing (distance "ATA" "AGTG"))
|
||||
]
|
||||
suite "Hamming"
|
||||
[ test "identical strands"
|
||||
(assertEqual (Just 0) (distance "A" "A"))
|
||||
, test "long identical strands"
|
||||
(assertEqual (Just 0) (distance "GGACTGA" "GGACTGA"))
|
||||
, test "complete distance in single nucleotide strands"
|
||||
(assertEqual (Just 1) (distance "A" "G"))
|
||||
, test "complete distance in small strands"
|
||||
(assertEqual (Just 2) (distance "AG" "CT"))
|
||||
, test "small distance in small strands"
|
||||
(assertEqual (Just 1) (distance "AT" "CT"))
|
||||
, test "small distance"
|
||||
(assertEqual (Just 1) (distance "GGACG" "GGTCG"))
|
||||
, test "small distance in long strands"
|
||||
(assertEqual (Just 2) (distance "ACCAGGG" "ACTATGG"))
|
||||
, test "non-unique character in first strand"
|
||||
(assertEqual (Just 1) (distance "AGA" "AGG"))
|
||||
, test "non-unique character in second strand"
|
||||
(assertEqual (Just 1) (distance "AGG" "AGA"))
|
||||
, test "large distance"
|
||||
(assertEqual (Just 4) (distance "GATACA" "GCATAA"))
|
||||
, test "large distance in off-by-one strand"
|
||||
(assertEqual (Just 9) (distance "GGACGGATTCTG" "AGGACGGATTCT"))
|
||||
, test "empty strands"
|
||||
(assertEqual (Just 0) (distance "" ""))
|
||||
, test "disallow first strand longer"
|
||||
(assertEqual Nothing (distance "AATG" "AAA"))
|
||||
, test "disallow second strand longer"
|
||||
(assertEqual Nothing (distance "ATA" "AGTG"))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -3,9 +3,9 @@ module HelloWorld exposing (..)
|
|||
|
||||
helloWorld : Maybe String -> String
|
||||
helloWorld name =
|
||||
case name of
|
||||
Just name ->
|
||||
"Hello, " ++ name ++ "!"
|
||||
case name of
|
||||
Just name ->
|
||||
"Hello, " ++ name ++ "!"
|
||||
|
||||
Nothing ->
|
||||
"Hello, World!"
|
||||
Nothing ->
|
||||
"Hello, World!"
|
||||
|
|
|
@ -6,14 +6,13 @@ import HelloWorld exposing (helloWorld)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Hello, World!"
|
||||
[ test "Hello with no name" (assertEqual "Hello, World!" (helloWorld Nothing))
|
||||
, test "Hello to a sample name" (assertEqual "Hello, Alice!" (helloWorld (Just "Alice")))
|
||||
, test "Hello to another sample name" (assertEqual "Hello, Bob!" (helloWorld (Just "Bob")))
|
||||
]
|
||||
suite "Hello, World!"
|
||||
[ test "Hello with no name" (assertEqual "Hello, World!" (helloWorld Nothing))
|
||||
, test "Hello to a sample name" (assertEqual "Hello, Alice!" (helloWorld (Just "Alice")))
|
||||
, test "Hello to another sample name" (assertEqual "Hello, Bob!" (helloWorld (Just "Bob")))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -3,4 +3,4 @@ module Leap exposing (..)
|
|||
|
||||
isLeapYear : Int -> Bool
|
||||
isLeapYear year =
|
||||
year % 4 == 0 && (year % 100 /= 0 || year % 400 == 0)
|
||||
year % 4 == 0 && (year % 100 /= 0 || year % 400 == 0)
|
||||
|
|
|
@ -6,18 +6,17 @@ import Leap
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Leap"
|
||||
[ test "leap year" (assertEqual True (Leap.isLeapYear 1996))
|
||||
, test "non-leap year" (assertEqual False (Leap.isLeapYear 1997))
|
||||
, test "non-leap even year" (assertEqual False (Leap.isLeapYear 1998))
|
||||
, test "century" (assertEqual False (Leap.isLeapYear 1900))
|
||||
, test "second century" (assertEqual False (Leap.isLeapYear 1800))
|
||||
, test "fourth century" (assertEqual True (Leap.isLeapYear 2400))
|
||||
, test "y2k" (assertEqual True (Leap.isLeapYear 2000))
|
||||
]
|
||||
suite "Leap"
|
||||
[ test "leap year" (assertEqual True (Leap.isLeapYear 1996))
|
||||
, test "non-leap year" (assertEqual False (Leap.isLeapYear 1997))
|
||||
, test "non-leap even year" (assertEqual False (Leap.isLeapYear 1998))
|
||||
, test "century" (assertEqual False (Leap.isLeapYear 1900))
|
||||
, test "second century" (assertEqual False (Leap.isLeapYear 1800))
|
||||
, test "fourth century" (assertEqual True (Leap.isLeapYear 2400))
|
||||
, test "y2k" (assertEqual True (Leap.isLeapYear 2000))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -3,57 +3,57 @@ module ListOps exposing (..)
|
|||
|
||||
length : List a -> Int
|
||||
length list =
|
||||
foldl (\_ acc -> 1 + acc) 0 list
|
||||
foldl (\_ acc -> 1 + acc) 0 list
|
||||
|
||||
|
||||
reverse : List a -> List a
|
||||
reverse list =
|
||||
foldl (::) [] list
|
||||
foldl (::) [] list
|
||||
|
||||
|
||||
foldl : (a -> b -> b) -> b -> List a -> b
|
||||
foldl f acc list =
|
||||
case list of
|
||||
[] ->
|
||||
acc
|
||||
case list of
|
||||
[] ->
|
||||
acc
|
||||
|
||||
head :: tail ->
|
||||
foldl f (f head acc) tail
|
||||
head :: tail ->
|
||||
foldl f (f head acc) tail
|
||||
|
||||
|
||||
foldr : (a -> b -> b) -> b -> List a -> b
|
||||
foldr f acc list =
|
||||
case list of
|
||||
[] ->
|
||||
acc
|
||||
case list of
|
||||
[] ->
|
||||
acc
|
||||
|
||||
head :: tail ->
|
||||
f head (foldr f acc tail)
|
||||
head :: tail ->
|
||||
f head (foldr f acc tail)
|
||||
|
||||
|
||||
map : (a -> b) -> List a -> List b
|
||||
map f list =
|
||||
foldr (\x acc -> f x :: acc) [] list
|
||||
foldr (\x acc -> f x :: acc) [] list
|
||||
|
||||
|
||||
filter : (a -> Bool) -> List a -> List a
|
||||
filter f list =
|
||||
foldr
|
||||
(\x acc ->
|
||||
if f x then
|
||||
x :: acc
|
||||
else
|
||||
acc
|
||||
)
|
||||
[]
|
||||
list
|
||||
foldr
|
||||
(\x acc ->
|
||||
if f x then
|
||||
x :: acc
|
||||
else
|
||||
acc
|
||||
)
|
||||
[]
|
||||
list
|
||||
|
||||
|
||||
append : List a -> List a -> List a
|
||||
append xs ys =
|
||||
foldr (::) ys xs
|
||||
foldr (::) ys xs
|
||||
|
||||
|
||||
concat : List (List a) -> List a
|
||||
concat list =
|
||||
foldr append [] list
|
||||
foldr append [] list
|
||||
|
|
|
@ -6,61 +6,48 @@ import ListOps exposing (..)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"List Ops"
|
||||
[ suite
|
||||
"length"
|
||||
[ test "empty list" (assertEqual 0 (length []))
|
||||
, test "non-empty list" (assertEqual 4 (length [1..4]))
|
||||
suite "List Ops"
|
||||
[ suite "length"
|
||||
[ test "empty list" (assertEqual 0 (length []))
|
||||
, test "non-empty list" (assertEqual 4 (length [1..4]))
|
||||
]
|
||||
, suite "reverse"
|
||||
[ test "empty list" (assertEqual [] (reverse []))
|
||||
, test "non-empty list" (assertEqual [ 4, 3, 2, 1 ] (reverse [1..4]))
|
||||
]
|
||||
, suite "map"
|
||||
[ test "empty list" (assertEqual [] (map ((+) 1) []))
|
||||
, test "non-empty list" (assertEqual [2..5] (map ((+) 1) [1..4]))
|
||||
]
|
||||
, suite "filter"
|
||||
[ test "empty list" (assertEqual [] (filter (\_ -> True) []))
|
||||
, test "non-empty list"
|
||||
(assertEqual [ 2, 4 ] (filter (\x -> x % 2 == 0) [1..4]))
|
||||
]
|
||||
, suite "foldl"
|
||||
[ test "empty list" (assertEqual 0 (foldl (+) 0 []))
|
||||
, test "non-empty list" (assertEqual 10 (foldl (+) 0 [1..4]))
|
||||
]
|
||||
, suite "foldr"
|
||||
[ test "empty list" (assertEqual 0 (foldr (+) 0 []))
|
||||
, test "non-empty list" (assertEqual 10 (foldr (+) 0 [1..4]))
|
||||
]
|
||||
, suite "append"
|
||||
[ test "empty lists" (assertEqual [] (append [] []))
|
||||
, test "empty and non-empty lists"
|
||||
(assertEqual [1..4] (append [] [1..4]))
|
||||
, test "non-empty and empty lists"
|
||||
(assertEqual [1..4] (append [1..4] []))
|
||||
, test "non-empty lists" (assertEqual [1..8] (append [1..4] [5..8]))
|
||||
]
|
||||
, suite "concat"
|
||||
[ test "empty list" (assertEqual [] (concat []))
|
||||
, test "list of lists"
|
||||
(assertEqual [1..10] (concat [ [1..3], [], [4..7], [8..10] ]))
|
||||
]
|
||||
]
|
||||
, suite
|
||||
"reverse"
|
||||
[ test "empty list" (assertEqual [] (reverse []))
|
||||
, test "non-empty list" (assertEqual [ 4, 3, 2, 1 ] (reverse [1..4]))
|
||||
]
|
||||
, suite
|
||||
"map"
|
||||
[ test "empty list" (assertEqual [] (map ((+) 1) []))
|
||||
, test "non-empty list" (assertEqual [2..5] (map ((+) 1) [1..4]))
|
||||
]
|
||||
, suite
|
||||
"filter"
|
||||
[ test "empty list" (assertEqual [] (filter (\_ -> True) []))
|
||||
, test
|
||||
"non-empty list"
|
||||
(assertEqual [ 2, 4 ] (filter (\x -> x % 2 == 0) [1..4]))
|
||||
]
|
||||
, suite
|
||||
"foldl"
|
||||
[ test "empty list" (assertEqual 0 (foldl (+) 0 []))
|
||||
, test "non-empty list" (assertEqual 10 (foldl (+) 0 [1..4]))
|
||||
]
|
||||
, suite
|
||||
"foldr"
|
||||
[ test "empty list" (assertEqual 0 (foldr (+) 0 []))
|
||||
, test "non-empty list" (assertEqual 10 (foldr (+) 0 [1..4]))
|
||||
]
|
||||
, suite
|
||||
"append"
|
||||
[ test "empty lists" (assertEqual [] (append [] []))
|
||||
, test
|
||||
"empty and non-empty lists"
|
||||
(assertEqual [1..4] (append [] [1..4]))
|
||||
, test
|
||||
"non-empty and empty lists"
|
||||
(assertEqual [1..4] (append [1..4] []))
|
||||
, test "non-empty lists" (assertEqual [1..8] (append [1..4] [5..8]))
|
||||
]
|
||||
, suite
|
||||
"concat"
|
||||
[ test "empty list" (assertEqual [] (concat []))
|
||||
, test
|
||||
"list of lists"
|
||||
(assertEqual [1..10] (concat [ [1..3], [], [4..7], [8..10] ]))
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,45 +5,45 @@ import String
|
|||
|
||||
version : Int
|
||||
version =
|
||||
2
|
||||
2
|
||||
|
||||
|
||||
type alias NucleotideCounts =
|
||||
{ a : Int
|
||||
, t : Int
|
||||
, c : Int
|
||||
, g : Int
|
||||
}
|
||||
{ a : Int
|
||||
, t : Int
|
||||
, c : Int
|
||||
, g : Int
|
||||
}
|
||||
|
||||
|
||||
init : NucleotideCounts
|
||||
init =
|
||||
{ a = 0
|
||||
, t = 0
|
||||
, c = 0
|
||||
, g = 0
|
||||
}
|
||||
{ a = 0
|
||||
, t = 0
|
||||
, c = 0
|
||||
, g = 0
|
||||
}
|
||||
|
||||
|
||||
update : Char -> NucleotideCounts -> NucleotideCounts
|
||||
update char counts =
|
||||
case char of
|
||||
'A' ->
|
||||
{ counts | a = counts.a + 1 }
|
||||
case char of
|
||||
'A' ->
|
||||
{ counts | a = counts.a + 1 }
|
||||
|
||||
'T' ->
|
||||
{ counts | t = counts.t + 1 }
|
||||
'T' ->
|
||||
{ counts | t = counts.t + 1 }
|
||||
|
||||
'C' ->
|
||||
{ counts | c = counts.c + 1 }
|
||||
'C' ->
|
||||
{ counts | c = counts.c + 1 }
|
||||
|
||||
'G' ->
|
||||
{ counts | g = counts.g + 1 }
|
||||
'G' ->
|
||||
{ counts | g = counts.g + 1 }
|
||||
|
||||
_ ->
|
||||
counts
|
||||
_ ->
|
||||
counts
|
||||
|
||||
|
||||
nucleotideCounts : String -> NucleotideCounts
|
||||
nucleotideCounts sequence =
|
||||
String.foldl update init sequence
|
||||
String.foldl update init sequence
|
||||
|
|
|
@ -6,32 +6,24 @@ import NucleotideCount exposing (nucleotideCounts, version)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"NucleotideCount"
|
||||
[ test
|
||||
"the solution is for the correct version of the test"
|
||||
(assertEqual 2 version)
|
||||
, 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")
|
||||
)
|
||||
]
|
||||
suite "NucleotideCount"
|
||||
[ test "the solution is for the correct version of the test"
|
||||
(assertEqual 2 version)
|
||||
, 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")
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,13 +5,13 @@ import String exposing (toLower, contains, fromChar)
|
|||
|
||||
isPangram : String -> Bool
|
||||
isPangram sentence =
|
||||
let
|
||||
normalized =
|
||||
toLower sentence
|
||||
in
|
||||
String.all (\c -> contains (fromChar c) normalized) alphabet
|
||||
let
|
||||
normalized =
|
||||
toLower sentence
|
||||
in
|
||||
String.all (\c -> contains (fromChar c) normalized) alphabet
|
||||
|
||||
|
||||
alphabet : String
|
||||
alphabet =
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
|
|
|
@ -6,65 +6,46 @@ import Pangram exposing (isPangram)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Pangram"
|
||||
[ test
|
||||
"sentence empty"
|
||||
(assertEqual
|
||||
False
|
||||
(isPangram "")
|
||||
)
|
||||
, test
|
||||
"pangram with only lower case"
|
||||
(assertEqual
|
||||
True
|
||||
(isPangram "the quick brown fox jumps over the lazy dog")
|
||||
)
|
||||
, test
|
||||
"missing character 'x'"
|
||||
(assertEqual
|
||||
False
|
||||
(isPangram "a quick movement of the enemy will jeopardize five gunboats")
|
||||
)
|
||||
, test
|
||||
"another missing character 'x'"
|
||||
(assertEqual
|
||||
False
|
||||
(isPangram "the quick brown fish jumps over the lazy dog")
|
||||
)
|
||||
, test
|
||||
"pangram with underscores"
|
||||
(assertEqual
|
||||
True
|
||||
(isPangram "the_quick_brown_fox_jumps_over_the_lazy_dog")
|
||||
)
|
||||
, test
|
||||
"pangram with numbers"
|
||||
(assertEqual
|
||||
True
|
||||
(isPangram "the 1 quick brown fox jumps over the 2 lazy dogs")
|
||||
)
|
||||
, test
|
||||
"missing letters replaced by numbers"
|
||||
(assertEqual
|
||||
False
|
||||
(isPangram "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")
|
||||
)
|
||||
, test
|
||||
"pangram with mixed case and punctuation"
|
||||
(assertEqual
|
||||
True
|
||||
(isPangram "\"Five quacking Zephyrs jolt my wax bed.\"")
|
||||
)
|
||||
, test
|
||||
"pangram with non ascii characters"
|
||||
(assertEqual
|
||||
True
|
||||
(isPangram "Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.")
|
||||
)
|
||||
]
|
||||
suite "Pangram"
|
||||
[ test "sentence empty"
|
||||
(assertEqual False
|
||||
(isPangram "")
|
||||
)
|
||||
, test "pangram with only lower case"
|
||||
(assertEqual True
|
||||
(isPangram "the quick brown fox jumps over the lazy dog")
|
||||
)
|
||||
, test "missing character 'x'"
|
||||
(assertEqual False
|
||||
(isPangram "a quick movement of the enemy will jeopardize five gunboats")
|
||||
)
|
||||
, test "another missing character 'x'"
|
||||
(assertEqual False
|
||||
(isPangram "the quick brown fish jumps over the lazy dog")
|
||||
)
|
||||
, test "pangram with underscores"
|
||||
(assertEqual True
|
||||
(isPangram "the_quick_brown_fox_jumps_over_the_lazy_dog")
|
||||
)
|
||||
, test "pangram with numbers"
|
||||
(assertEqual True
|
||||
(isPangram "the 1 quick brown fox jumps over the 2 lazy dogs")
|
||||
)
|
||||
, test "missing letters replaced by numbers"
|
||||
(assertEqual False
|
||||
(isPangram "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")
|
||||
)
|
||||
, test "pangram with mixed case and punctuation"
|
||||
(assertEqual True
|
||||
(isPangram "\"Five quacking Zephyrs jolt my wax bed.\"")
|
||||
)
|
||||
, test "pangram with non ascii characters"
|
||||
(assertEqual True
|
||||
(isPangram "Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.")
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -4,41 +4,41 @@ import String
|
|||
|
||||
|
||||
nums =
|
||||
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
|
||||
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
|
||||
|
||||
|
||||
getNumber : String -> Maybe String
|
||||
getNumber text =
|
||||
getValidNum (String.filter isDigit text)
|
||||
getValidNum (String.filter isDigit text)
|
||||
|
||||
|
||||
isDigit : Char -> Bool
|
||||
isDigit char =
|
||||
List.any (\n -> n == char) nums
|
||||
List.any (\n -> n == char) nums
|
||||
|
||||
|
||||
getValidNum : String -> Maybe String
|
||||
getValidNum num =
|
||||
if String.length num == 10 then
|
||||
Just num
|
||||
else if (String.length num == 11) && (String.left 1 num == "1") then
|
||||
Just (String.dropLeft 1 num)
|
||||
else
|
||||
Nothing
|
||||
if String.length num == 10 then
|
||||
Just num
|
||||
else if (String.length num == 11) && (String.left 1 num == "1") then
|
||||
Just (String.dropLeft 1 num)
|
||||
else
|
||||
Nothing
|
||||
|
||||
|
||||
prettyPrint : String -> Maybe String
|
||||
prettyPrint input =
|
||||
Maybe.map formatNumber (getNumber input)
|
||||
Maybe.map formatNumber (getNumber input)
|
||||
|
||||
|
||||
formatNumber : String -> String
|
||||
formatNumber input =
|
||||
String.concat
|
||||
[ "("
|
||||
, (String.slice 0 3 input)
|
||||
, ") "
|
||||
, (String.slice 3 6 input)
|
||||
, "-"
|
||||
, (String.slice 6 10 input)
|
||||
]
|
||||
String.concat
|
||||
[ "("
|
||||
, (String.slice 0 3 input)
|
||||
, ") "
|
||||
, (String.slice 3 6 input)
|
||||
, "-"
|
||||
, (String.slice 6 10 input)
|
||||
]
|
||||
|
|
|
@ -6,47 +6,34 @@ import PhoneNumber exposing (getNumber, prettyPrint)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"PhoneNumber"
|
||||
[ test
|
||||
"cleans number"
|
||||
(assertEqual (Just "1234567890") (getNumber "(123) 456-7890"))
|
||||
, test
|
||||
"cleans number with dots"
|
||||
(assertEqual (Just "1234567890") (getNumber "123.456.7890"))
|
||||
, test
|
||||
"valid when 11 digits and first is 1"
|
||||
(assertEqual (Just "1234567890") (getNumber "11234567890"))
|
||||
, test
|
||||
"invalid when 11 digits"
|
||||
(assertEqual Nothing (getNumber "21234567890"))
|
||||
, test
|
||||
"invalid when 9 digits"
|
||||
(assertEqual Nothing (getNumber "123456789"))
|
||||
, test
|
||||
"invalid when 12 digits"
|
||||
(assertEqual Nothing (getNumber "123456789012"))
|
||||
, test
|
||||
"invalid when empty"
|
||||
(assertEqual Nothing (getNumber ""))
|
||||
, test
|
||||
"invalid when no digits present"
|
||||
(assertEqual Nothing (getNumber " (-) "))
|
||||
, test
|
||||
"valid with leading characters"
|
||||
(assertEqual (Just "1234567890") (getNumber "my number is 123 456 7890"))
|
||||
, test
|
||||
"valid with trailing characters"
|
||||
(assertEqual (Just "1234567890") (getNumber "123 456 7890 - bob"))
|
||||
, test
|
||||
"pretty print"
|
||||
(assertEqual (Just "(123) 456-7890") (prettyPrint "1234567890"))
|
||||
, test
|
||||
"pretty print with full us phone number"
|
||||
(assertEqual (Just "(123) 456-7890") (prettyPrint "11234567890"))
|
||||
]
|
||||
suite "PhoneNumber"
|
||||
[ test "cleans number"
|
||||
(assertEqual (Just "1234567890") (getNumber "(123) 456-7890"))
|
||||
, test "cleans number with dots"
|
||||
(assertEqual (Just "1234567890") (getNumber "123.456.7890"))
|
||||
, test "valid when 11 digits and first is 1"
|
||||
(assertEqual (Just "1234567890") (getNumber "11234567890"))
|
||||
, test "invalid when 11 digits"
|
||||
(assertEqual Nothing (getNumber "21234567890"))
|
||||
, test "invalid when 9 digits"
|
||||
(assertEqual Nothing (getNumber "123456789"))
|
||||
, test "invalid when 12 digits"
|
||||
(assertEqual Nothing (getNumber "123456789012"))
|
||||
, test "invalid when empty"
|
||||
(assertEqual Nothing (getNumber ""))
|
||||
, test "invalid when no digits present"
|
||||
(assertEqual Nothing (getNumber " (-) "))
|
||||
, test "valid with leading characters"
|
||||
(assertEqual (Just "1234567890") (getNumber "my number is 123 456 7890"))
|
||||
, test "valid with trailing characters"
|
||||
(assertEqual (Just "1234567890") (getNumber "123 456 7890 - bob"))
|
||||
, test "pretty print"
|
||||
(assertEqual (Just "(123) 456-7890") (prettyPrint "1234567890"))
|
||||
, test "pretty print with full us phone number"
|
||||
(assertEqual (Just "(123) 456-7890") (prettyPrint "11234567890"))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,26 +5,26 @@ import String
|
|||
|
||||
raindrops : Int -> String
|
||||
raindrops number =
|
||||
let
|
||||
drops =
|
||||
[ if number % 3 == 0 then
|
||||
"Pling"
|
||||
else
|
||||
""
|
||||
, if number % 5 == 0 then
|
||||
"Plang"
|
||||
else
|
||||
""
|
||||
, if number % 7 == 0 then
|
||||
"Plong"
|
||||
else
|
||||
""
|
||||
]
|
||||
let
|
||||
drops =
|
||||
[ if number % 3 == 0 then
|
||||
"Pling"
|
||||
else
|
||||
""
|
||||
, if number % 5 == 0 then
|
||||
"Plang"
|
||||
else
|
||||
""
|
||||
, if number % 7 == 0 then
|
||||
"Plong"
|
||||
else
|
||||
""
|
||||
]
|
||||
|
||||
result =
|
||||
String.join "" drops
|
||||
in
|
||||
if result == "" then
|
||||
toString number
|
||||
else
|
||||
result
|
||||
result =
|
||||
String.join "" drops
|
||||
in
|
||||
if result == "" then
|
||||
toString number
|
||||
else
|
||||
result
|
||||
|
|
|
@ -6,26 +6,25 @@ import Raindrops exposing (raindrops)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Raindrops"
|
||||
[ test "1" (assertEqual "1" (raindrops 1))
|
||||
, test "3" (assertEqual "Pling" (raindrops 3))
|
||||
, test "5" (assertEqual "Plang" (raindrops 5))
|
||||
, test "7" (assertEqual "Plong" (raindrops 7))
|
||||
, test "6" (assertEqual "Pling" (raindrops 6))
|
||||
, test "9" (assertEqual "Pling" (raindrops 9))
|
||||
, test "10" (assertEqual "Plang" (raindrops 10))
|
||||
, test "14" (assertEqual "Plong" (raindrops 14))
|
||||
, test "15" (assertEqual "PlingPlang" (raindrops 15))
|
||||
, test "21" (assertEqual "PlingPlong" (raindrops 21))
|
||||
, test "25" (assertEqual "Plang" (raindrops 25))
|
||||
, test "35" (assertEqual "PlangPlong" (raindrops 35))
|
||||
, test "49" (assertEqual "Plong" (raindrops 49))
|
||||
, test "52" (assertEqual "52" (raindrops 52))
|
||||
, test "105" (assertEqual "PlingPlangPlong" (raindrops 105))
|
||||
]
|
||||
suite "Raindrops"
|
||||
[ test "1" (assertEqual "1" (raindrops 1))
|
||||
, test "3" (assertEqual "Pling" (raindrops 3))
|
||||
, test "5" (assertEqual "Plang" (raindrops 5))
|
||||
, test "7" (assertEqual "Plong" (raindrops 7))
|
||||
, test "6" (assertEqual "Pling" (raindrops 6))
|
||||
, test "9" (assertEqual "Pling" (raindrops 9))
|
||||
, test "10" (assertEqual "Plang" (raindrops 10))
|
||||
, test "14" (assertEqual "Plong" (raindrops 14))
|
||||
, test "15" (assertEqual "PlingPlang" (raindrops 15))
|
||||
, test "21" (assertEqual "PlingPlong" (raindrops 21))
|
||||
, test "25" (assertEqual "Plang" (raindrops 25))
|
||||
, test "35" (assertEqual "PlangPlong" (raindrops 35))
|
||||
, test "49" (assertEqual "Plong" (raindrops 49))
|
||||
, test "52" (assertEqual "52" (raindrops 52))
|
||||
, test "105" (assertEqual "PlingPlangPlong" (raindrops 105))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,12 +5,12 @@ import String
|
|||
|
||||
toRNA : String -> Result Char String
|
||||
toRNA dna =
|
||||
dna
|
||||
|> String.toList
|
||||
|> List.map toRNANucleotide
|
||||
|> resultExtraCombine
|
||||
|> Result.map (List.map String.fromChar)
|
||||
|> Result.map (String.join "")
|
||||
dna
|
||||
|> String.toList
|
||||
|> List.map toRNANucleotide
|
||||
|> resultExtraCombine
|
||||
|> Result.map (List.map String.fromChar)
|
||||
|> Result.map (String.join "")
|
||||
|
||||
|
||||
|
||||
|
@ -19,23 +19,23 @@ toRNA dna =
|
|||
|
||||
resultExtraCombine : List (Result x a) -> Result x (List a)
|
||||
resultExtraCombine =
|
||||
List.foldr (Result.map2 (::)) (Ok [])
|
||||
List.foldr (Result.map2 (::)) (Ok [])
|
||||
|
||||
|
||||
toRNANucleotide : Char -> Result Char Char
|
||||
toRNANucleotide nuc =
|
||||
case nuc of
|
||||
'C' ->
|
||||
Ok 'G'
|
||||
case nuc of
|
||||
'C' ->
|
||||
Ok 'G'
|
||||
|
||||
'G' ->
|
||||
Ok 'C'
|
||||
'G' ->
|
||||
Ok 'C'
|
||||
|
||||
'A' ->
|
||||
Ok 'U'
|
||||
'A' ->
|
||||
Ok 'U'
|
||||
|
||||
'T' ->
|
||||
Ok 'A'
|
||||
'T' ->
|
||||
Ok 'A'
|
||||
|
||||
_ ->
|
||||
Err nuc
|
||||
_ ->
|
||||
Err nuc
|
||||
|
|
|
@ -6,32 +6,24 @@ import RNATranscription exposing (toRNA)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"RNATranscription"
|
||||
[ test
|
||||
"complement of cytosine is guanine"
|
||||
(assertEqual (Ok "G") (toRNA "C"))
|
||||
, test
|
||||
"complement of guanine is cytosine"
|
||||
(assertEqual (Ok "C") (toRNA "G"))
|
||||
, test
|
||||
"complement of thymine is adenine"
|
||||
(assertEqual (Ok "A") (toRNA "T"))
|
||||
, test
|
||||
"complement of adenine is uracil"
|
||||
(assertEqual (Ok "U") (toRNA "A"))
|
||||
, test
|
||||
"complement"
|
||||
(assertEqual (Ok "UGCACCAGAAUU") (toRNA "ACGTGGTCTTAA"))
|
||||
, test
|
||||
"correctly handles completely invalid input"
|
||||
(assertEqual (Err 'X') (toRNA "XXX"))
|
||||
, test
|
||||
"correctly handles partially invalid input"
|
||||
(assertEqual (Err 'U') (toRNA "UGAAXXXGACAUG"))
|
||||
]
|
||||
suite "RNATranscription"
|
||||
[ test "complement of cytosine is guanine"
|
||||
(assertEqual (Ok "G") (toRNA "C"))
|
||||
, test "complement of guanine is cytosine"
|
||||
(assertEqual (Ok "C") (toRNA "G"))
|
||||
, test "complement of thymine is adenine"
|
||||
(assertEqual (Ok "A") (toRNA "T"))
|
||||
, test "complement of adenine is uracil"
|
||||
(assertEqual (Ok "U") (toRNA "A"))
|
||||
, test "complement"
|
||||
(assertEqual (Ok "UGCACCAGAAUU") (toRNA "ACGTGGTCTTAA"))
|
||||
, test "correctly handles completely invalid input"
|
||||
(assertEqual (Err 'X') (toRNA "XXX"))
|
||||
, test "correctly handles partially invalid input"
|
||||
(assertEqual (Err 'U') (toRNA "UGAAXXXGACAUG"))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -4,95 +4,95 @@ import String
|
|||
|
||||
|
||||
type Bearing
|
||||
= North
|
||||
| East
|
||||
| South
|
||||
| West
|
||||
= North
|
||||
| East
|
||||
| South
|
||||
| West
|
||||
|
||||
|
||||
type alias Robot =
|
||||
{ bearing : Bearing
|
||||
, coordinates : { x : Int, y : Int }
|
||||
}
|
||||
{ bearing : Bearing
|
||||
, coordinates : { x : Int, y : Int }
|
||||
}
|
||||
|
||||
|
||||
defaultRobot : Robot
|
||||
defaultRobot =
|
||||
{ bearing = North
|
||||
, coordinates = { x = 0, y = 0 }
|
||||
}
|
||||
{ bearing = North
|
||||
, coordinates = { x = 0, y = 0 }
|
||||
}
|
||||
|
||||
|
||||
turnRight : Robot -> Robot
|
||||
turnRight robot =
|
||||
case robot.bearing of
|
||||
North ->
|
||||
{ robot | bearing = East }
|
||||
case robot.bearing of
|
||||
North ->
|
||||
{ robot | bearing = East }
|
||||
|
||||
East ->
|
||||
{ robot | bearing = South }
|
||||
East ->
|
||||
{ robot | bearing = South }
|
||||
|
||||
South ->
|
||||
{ robot | bearing = West }
|
||||
South ->
|
||||
{ robot | bearing = West }
|
||||
|
||||
West ->
|
||||
{ robot | bearing = North }
|
||||
West ->
|
||||
{ robot | bearing = North }
|
||||
|
||||
|
||||
turnLeft : Robot -> Robot
|
||||
turnLeft robot =
|
||||
case robot.bearing of
|
||||
North ->
|
||||
{ robot | bearing = West }
|
||||
case robot.bearing of
|
||||
North ->
|
||||
{ robot | bearing = West }
|
||||
|
||||
West ->
|
||||
{ robot | bearing = South }
|
||||
West ->
|
||||
{ robot | bearing = South }
|
||||
|
||||
South ->
|
||||
{ robot | bearing = East }
|
||||
South ->
|
||||
{ robot | bearing = East }
|
||||
|
||||
East ->
|
||||
{ robot | bearing = North }
|
||||
East ->
|
||||
{ robot | bearing = North }
|
||||
|
||||
|
||||
advance : Robot -> Robot
|
||||
advance { bearing, coordinates } =
|
||||
let
|
||||
updated =
|
||||
case bearing of
|
||||
North ->
|
||||
{ coordinates | y = coordinates.y + 1 }
|
||||
let
|
||||
updated =
|
||||
case bearing of
|
||||
North ->
|
||||
{ coordinates | y = coordinates.y + 1 }
|
||||
|
||||
East ->
|
||||
{ coordinates | x = coordinates.x + 1 }
|
||||
East ->
|
||||
{ coordinates | x = coordinates.x + 1 }
|
||||
|
||||
South ->
|
||||
{ coordinates | y = coordinates.y - 1 }
|
||||
South ->
|
||||
{ coordinates | y = coordinates.y - 1 }
|
||||
|
||||
West ->
|
||||
{ coordinates | x = coordinates.x - 1 }
|
||||
in
|
||||
{ bearing = bearing, coordinates = updated }
|
||||
West ->
|
||||
{ coordinates | x = coordinates.x - 1 }
|
||||
in
|
||||
{ bearing = bearing, coordinates = updated }
|
||||
|
||||
|
||||
simulate : String -> Robot -> Robot
|
||||
simulate directions robot =
|
||||
let
|
||||
action direction =
|
||||
case direction of
|
||||
'L' ->
|
||||
turnLeft
|
||||
let
|
||||
action direction =
|
||||
case direction of
|
||||
'L' ->
|
||||
turnLeft
|
||||
|
||||
'R' ->
|
||||
turnRight
|
||||
'R' ->
|
||||
turnRight
|
||||
|
||||
'A' ->
|
||||
advance
|
||||
'A' ->
|
||||
advance
|
||||
|
||||
_ ->
|
||||
identity
|
||||
in
|
||||
directions
|
||||
|> String.toList
|
||||
|> List.map action
|
||||
|> List.foldl (\a r -> a r) robot
|
||||
_ ->
|
||||
identity
|
||||
in
|
||||
directions
|
||||
|> String.toList
|
||||
|> List.map action
|
||||
|> List.foldl (\a r -> a r) robot
|
||||
|
|
|
@ -6,124 +6,112 @@ import RobotSimulator exposing (defaultRobot, Robot, Bearing(North, East, West,
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"RobotSimulator"
|
||||
[ suite
|
||||
"init"
|
||||
(let
|
||||
robot =
|
||||
defaultRobot
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 0, y = 0 } robot.coordinates)
|
||||
, test "bearing" (assertEqual North robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"setup"
|
||||
(let
|
||||
robot =
|
||||
Robot South { x = -1, y = 1 }
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -1, y = 1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual South robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"turn right"
|
||||
([1..3]
|
||||
|> List.scanl (\_ r -> turnRight r) defaultRobot
|
||||
|> List.map .bearing
|
||||
|> assertionList [ North, East, South, West ]
|
||||
|> List.map defaultTest
|
||||
)
|
||||
, suite
|
||||
"turn left"
|
||||
([1..3]
|
||||
|> List.scanl (\_ r -> turnLeft r) defaultRobot
|
||||
|> List.map .bearing
|
||||
|> assertionList [ North, West, South, East ]
|
||||
|> List.map defaultTest
|
||||
)
|
||||
, suite
|
||||
"advance positive north"
|
||||
(let
|
||||
robot =
|
||||
Robot North { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 0, y = 1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual North robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"advance positive east"
|
||||
(let
|
||||
robot =
|
||||
Robot East { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 1, y = 0 } robot.coordinates)
|
||||
, test "bearing" (assertEqual East robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"advance negative south"
|
||||
(let
|
||||
robot =
|
||||
Robot South { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 0, y = -1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual South robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"advance positive west"
|
||||
(let
|
||||
robot =
|
||||
Robot West { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -1, y = 0 } robot.coordinates)
|
||||
, test "bearing" (assertEqual West robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"simulate prog 1"
|
||||
(let
|
||||
robot =
|
||||
Robot North { x = 0, y = 0 }
|
||||
|> simulate "LAAARALA"
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -4, y = 1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual West robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"simulate prog 2"
|
||||
(let
|
||||
robot =
|
||||
Robot East { x = 2, y = -7 }
|
||||
|> simulate "RRAAAAALA"
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -3, y = -8 } robot.coordinates)
|
||||
, test "bearing" (assertEqual South robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite
|
||||
"simulate prog 3"
|
||||
(let
|
||||
robot =
|
||||
Robot South { x = 8, y = 4 }
|
||||
|> simulate "LAAARRRALLLL"
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 11, y = 5 } robot.coordinates)
|
||||
, test "bearing" (assertEqual North robot.bearing)
|
||||
]
|
||||
)
|
||||
]
|
||||
suite "RobotSimulator"
|
||||
[ suite "init"
|
||||
(let
|
||||
robot =
|
||||
defaultRobot
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 0, y = 0 } robot.coordinates)
|
||||
, test "bearing" (assertEqual North robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "setup"
|
||||
(let
|
||||
robot =
|
||||
Robot South { x = -1, y = 1 }
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -1, y = 1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual South robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "turn right"
|
||||
([1..3]
|
||||
|> List.scanl (\_ r -> turnRight r) defaultRobot
|
||||
|> List.map .bearing
|
||||
|> assertionList [ North, East, South, West ]
|
||||
|> List.map defaultTest
|
||||
)
|
||||
, suite "turn left"
|
||||
([1..3]
|
||||
|> List.scanl (\_ r -> turnLeft r) defaultRobot
|
||||
|> List.map .bearing
|
||||
|> assertionList [ North, West, South, East ]
|
||||
|> List.map defaultTest
|
||||
)
|
||||
, suite "advance positive north"
|
||||
(let
|
||||
robot =
|
||||
Robot North { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 0, y = 1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual North robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "advance positive east"
|
||||
(let
|
||||
robot =
|
||||
Robot East { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 1, y = 0 } robot.coordinates)
|
||||
, test "bearing" (assertEqual East robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "advance negative south"
|
||||
(let
|
||||
robot =
|
||||
Robot South { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 0, y = -1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual South robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "advance positive west"
|
||||
(let
|
||||
robot =
|
||||
Robot West { x = 0, y = 0 }
|
||||
|> advance
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -1, y = 0 } robot.coordinates)
|
||||
, test "bearing" (assertEqual West robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "simulate prog 1"
|
||||
(let
|
||||
robot =
|
||||
Robot North { x = 0, y = 0 }
|
||||
|> simulate "LAAARALA"
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -4, y = 1 } robot.coordinates)
|
||||
, test "bearing" (assertEqual West robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "simulate prog 2"
|
||||
(let
|
||||
robot =
|
||||
Robot East { x = 2, y = -7 }
|
||||
|> simulate "RRAAAAALA"
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = -3, y = -8 } robot.coordinates)
|
||||
, test "bearing" (assertEqual South robot.bearing)
|
||||
]
|
||||
)
|
||||
, suite "simulate prog 3"
|
||||
(let
|
||||
robot =
|
||||
Robot South { x = 8, y = 4 }
|
||||
|> simulate "LAAARRRALLLL"
|
||||
in
|
||||
[ test "coordinates" (assertEqual { x = 11, y = 5 } robot.coordinates)
|
||||
, test "bearing" (assertEqual North robot.bearing)
|
||||
]
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -7,63 +7,63 @@ import Regex
|
|||
|
||||
|
||||
{-
|
||||
To the unaware: this was written by a very green elmer, so don't consider
|
||||
it an idiomatic exemplar to emulate.
|
||||
To the unaware: this was written by a very green elmer, so don't consider
|
||||
it an idiomatic exemplar to emulate.
|
||||
-}
|
||||
|
||||
|
||||
version =
|
||||
2
|
||||
2
|
||||
|
||||
|
||||
encode : String -> String
|
||||
encode string =
|
||||
String.toList string
|
||||
|> List.foldr countChars []
|
||||
|> List.map stringifyCounts
|
||||
|> String.join ""
|
||||
String.toList string
|
||||
|> List.foldr countChars []
|
||||
|> List.map stringifyCounts
|
||||
|> String.join ""
|
||||
|
||||
|
||||
countChars : a -> List ( number, a ) -> List ( number, a )
|
||||
countChars current counted =
|
||||
case head counted of
|
||||
Just ( count, previous ) ->
|
||||
if previous == current then
|
||||
( count + 1, current ) :: withDefault [] (tail counted)
|
||||
else
|
||||
( 1, current ) :: counted
|
||||
case head counted of
|
||||
Just ( count, previous ) ->
|
||||
if previous == current then
|
||||
( count + 1, current ) :: withDefault [] (tail counted)
|
||||
else
|
||||
( 1, current ) :: counted
|
||||
|
||||
Nothing ->
|
||||
[ ( 1, current ) ]
|
||||
Nothing ->
|
||||
[ ( 1, current ) ]
|
||||
|
||||
|
||||
stringifyCounts : ( number, Char ) -> String
|
||||
stringifyCounts ( count, char ) =
|
||||
if count > 1 then
|
||||
toString count ++ fromChar char
|
||||
else
|
||||
fromChar char
|
||||
if count > 1 then
|
||||
toString count ++ fromChar char
|
||||
else
|
||||
fromChar char
|
||||
|
||||
|
||||
decode : String -> String
|
||||
decode string =
|
||||
string
|
||||
|> Regex.find Regex.All (Regex.regex "(\\d+)|(\\D)")
|
||||
|> List.map .match
|
||||
|> List.foldl expandCounts ( "", Nothing )
|
||||
|> fst
|
||||
string
|
||||
|> Regex.find Regex.All (Regex.regex "(\\d+)|(\\D)")
|
||||
|> List.map .match
|
||||
|> List.foldl expandCounts ( "", Nothing )
|
||||
|> fst
|
||||
|
||||
|
||||
expandCounts : String -> ( String, Maybe Int ) -> ( String, Maybe Int )
|
||||
expandCounts match ( result, count ) =
|
||||
case count of
|
||||
Just number ->
|
||||
( result ++ String.repeat number match, Nothing )
|
||||
case count of
|
||||
Just number ->
|
||||
( result ++ String.repeat number match, Nothing )
|
||||
|
||||
Nothing ->
|
||||
case String.toInt match of
|
||||
Ok number ->
|
||||
( result, Just number )
|
||||
Nothing ->
|
||||
case String.toInt match of
|
||||
Ok number ->
|
||||
( result, Just number )
|
||||
|
||||
Err _ ->
|
||||
( result ++ match, Nothing )
|
||||
Err _ ->
|
||||
( result ++ match, Nothing )
|
||||
|
|
|
@ -10,57 +10,54 @@ import Char
|
|||
|
||||
|
||||
{-
|
||||
Currently disabled until elm-check is updated to support Elm 0.17
|
||||
Currently disabled until elm-check is updated to support Elm 0.17
|
||||
|
||||
Welcome! This is a property based test which will generate a bunch of random
|
||||
test cases in an attempt to find edge cases in your solution. If all goes well,
|
||||
any code that passes the regular tests should be fine here as well. If it goes
|
||||
less well, this should hopefully narrow the failure down to a useful test case.
|
||||
Welcome! This is a property based test which will generate a bunch of random
|
||||
test cases in an attempt to find edge cases in your solution. If all goes well,
|
||||
any code that passes the regular tests should be fine here as well. If it goes
|
||||
less well, this should hopefully narrow the failure down to a useful test case.
|
||||
|
||||
Good luck!
|
||||
Good luck!
|
||||
-}
|
||||
|
||||
|
||||
claims : Check.Claim
|
||||
claims =
|
||||
suite
|
||||
"List Reverse"
|
||||
[ claim
|
||||
"Encoding and decoding yields the original string"
|
||||
`that` (\input -> decode (encode (S.concat input)))
|
||||
`is` S.concat
|
||||
`for` inputProducer
|
||||
]
|
||||
suite "List Reverse"
|
||||
[ claim "Encoding and decoding yields the original string"
|
||||
`that` (\input -> decode (encode (S.concat input)))
|
||||
`is` S.concat
|
||||
`for` inputProducer
|
||||
]
|
||||
|
||||
|
||||
inputProducer : P.Producer (List String)
|
||||
inputProducer =
|
||||
P.tuple ( P.rangeInt 0 1001, upperCaseLetter )
|
||||
|> P.convert
|
||||
(\( n, c ) -> S.repeat n (S.fromChar c))
|
||||
(\s ->
|
||||
( S.length s
|
||||
, S.toList s |> List.head |> crashIfNothing
|
||||
)
|
||||
)
|
||||
|> P.list
|
||||
P.tuple ( P.rangeInt 0 1001, upperCaseLetter )
|
||||
|> P.convert (\( n, c ) -> S.repeat n (S.fromChar c))
|
||||
(\s ->
|
||||
( S.length s
|
||||
, S.toList s |> List.head |> crashIfNothing
|
||||
)
|
||||
)
|
||||
|> P.list
|
||||
|
||||
|
||||
upperCaseLetter : P.Producer Char
|
||||
upperCaseLetter =
|
||||
P.filter Char.isUpper P.upperCaseChar
|
||||
P.filter Char.isUpper P.upperCaseChar
|
||||
|
||||
|
||||
crashIfNothing : Maybe a -> a
|
||||
crashIfNothing a =
|
||||
case a of
|
||||
Just a ->
|
||||
a
|
||||
case a of
|
||||
Just a ->
|
||||
a
|
||||
|
||||
Nothing ->
|
||||
Debug.crash "Nothing!"
|
||||
Nothing ->
|
||||
Debug.crash "Nothing!"
|
||||
|
||||
|
||||
propertyTests : ElmTest.Test
|
||||
propertyTests =
|
||||
Check.Test.evidenceToTest (quickCheck claims)
|
||||
Check.Test.evidenceToTest (quickCheck claims)
|
||||
|
|
|
@ -6,50 +6,36 @@ import RunLengthEncoding exposing (version, decode, encode)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"RunLengthEncoding"
|
||||
[ test
|
||||
"the solution is for the correct version of the test"
|
||||
(assertEqual 2 version)
|
||||
, test
|
||||
"encode simple"
|
||||
(assertEqual "2A3B4C" (encode "AABBBCCCC"))
|
||||
, test
|
||||
"decode simple"
|
||||
(assertEqual "AABBBCCCC" (decode "2A3B4C"))
|
||||
, test
|
||||
"encode with single values"
|
||||
(assertEqual
|
||||
"12WB12W3B24WB"
|
||||
(encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB")
|
||||
)
|
||||
, test
|
||||
"decode with single values"
|
||||
(assertEqual
|
||||
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
|
||||
(decode "12WB12W3B24WB")
|
||||
)
|
||||
, test
|
||||
"(decode (encode (...)) combination"
|
||||
(assertEqual
|
||||
"zzz ZZ zZ"
|
||||
(decode (encode "zzz ZZ zZ"))
|
||||
)
|
||||
, test
|
||||
"decode with a x10 value"
|
||||
(assertEqual
|
||||
"WWWWWWWWWW"
|
||||
(decode "10W")
|
||||
)
|
||||
, test
|
||||
"encode unicode"
|
||||
(assertEqual "⏰3⚽2⭐⏰" (encode "⏰⚽⚽⚽⭐⭐⏰"))
|
||||
, test
|
||||
"decode unicode"
|
||||
(assertEqual "⏰⚽⚽⚽⭐⭐⏰" (decode "⏰3⚽2⭐⏰"))
|
||||
]
|
||||
suite "RunLengthEncoding"
|
||||
[ test "the solution is for the correct version of the test"
|
||||
(assertEqual 2 version)
|
||||
, test "encode simple"
|
||||
(assertEqual "2A3B4C" (encode "AABBBCCCC"))
|
||||
, test "decode simple"
|
||||
(assertEqual "AABBBCCCC" (decode "2A3B4C"))
|
||||
, test "encode with single values"
|
||||
(assertEqual "12WB12W3B24WB"
|
||||
(encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB")
|
||||
)
|
||||
, test "decode with single values"
|
||||
(assertEqual "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
|
||||
(decode "12WB12W3B24WB")
|
||||
)
|
||||
, test "(decode (encode (...)) combination"
|
||||
(assertEqual "zzz ZZ zZ"
|
||||
(decode (encode "zzz ZZ zZ"))
|
||||
)
|
||||
, test "decode with a x10 value"
|
||||
(assertEqual "WWWWWWWWWW"
|
||||
(decode "10W")
|
||||
)
|
||||
, test "encode unicode"
|
||||
(assertEqual "⏰3⚽2⭐⏰" (encode "⏰⚽⚽⚽⭐⭐⏰"))
|
||||
, test "decode unicode"
|
||||
(assertEqual "⏰⚽⚽⚽⭐⭐⏰" (decode "⏰3⚽2⭐⏰"))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -7,27 +7,27 @@ import Result
|
|||
|
||||
slices : Int -> String -> Result String (List (List Int))
|
||||
slices size input =
|
||||
if size < 1 then
|
||||
Err ("Invalid size: " ++ toString size)
|
||||
else
|
||||
String.split "" input
|
||||
|> List.map String.toInt
|
||||
|> combine
|
||||
|> Result.map (takeRuns size)
|
||||
if size < 1 then
|
||||
Err ("Invalid size: " ++ toString size)
|
||||
else
|
||||
String.split "" input
|
||||
|> List.map String.toInt
|
||||
|> combine
|
||||
|> Result.map (takeRuns size)
|
||||
|
||||
|
||||
takeRuns : Int -> List Int -> List (List Int)
|
||||
takeRuns size numbers =
|
||||
let
|
||||
candidate =
|
||||
List.take size numbers
|
||||
in
|
||||
if List.length candidate < size || size < 1 then
|
||||
[]
|
||||
else
|
||||
candidate :: takeRuns size (List.drop 1 numbers)
|
||||
let
|
||||
candidate =
|
||||
List.take size numbers
|
||||
in
|
||||
if List.length candidate < size || size < 1 then
|
||||
[]
|
||||
else
|
||||
candidate :: takeRuns size (List.drop 1 numbers)
|
||||
|
||||
|
||||
combine : List (Result x a) -> Result x (List a)
|
||||
combine =
|
||||
List.foldr (Result.map2 (::)) (Ok [])
|
||||
List.foldr (Result.map2 (::)) (Ok [])
|
||||
|
|
|
@ -6,59 +6,42 @@ import Series exposing (slices)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Series"
|
||||
[ test
|
||||
"slices of one"
|
||||
(assertEqual
|
||||
(Ok [ [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ] ])
|
||||
(slices 1 "01234")
|
||||
)
|
||||
, test
|
||||
"slices of two"
|
||||
(assertEqual
|
||||
(Ok [ [ 9, 7 ], [ 7, 8 ], [ 8, 6 ], [ 6, 7 ], [ 7, 5 ], [ 5, 6 ], [ 6, 4 ] ])
|
||||
(slices 2 "97867564")
|
||||
)
|
||||
, test
|
||||
"slices of three"
|
||||
(assertEqual
|
||||
(Ok [ [ 9, 7, 8 ], [ 7, 8, 6 ], [ 8, 6, 7 ], [ 6, 7, 5 ], [ 7, 5, 6 ], [ 5, 6, 4 ] ])
|
||||
(slices 3 "97867564")
|
||||
)
|
||||
, test
|
||||
"slices of four"
|
||||
(assertEqual
|
||||
(Ok [ [ 0, 1, 2, 3 ], [ 1, 2, 3, 4 ] ])
|
||||
(slices 4 "01234")
|
||||
)
|
||||
, test
|
||||
"slices of five"
|
||||
(assertEqual
|
||||
(Ok [ [ 0, 1, 2, 3, 4 ] ])
|
||||
(slices 5 "01234")
|
||||
)
|
||||
, test
|
||||
"overly long slice"
|
||||
(assertEqual
|
||||
(Ok [])
|
||||
(slices 4 "012")
|
||||
)
|
||||
, test
|
||||
"overly short slice"
|
||||
(assertEqual
|
||||
(Err ("Invalid size: 0"))
|
||||
(slices 0 "01234")
|
||||
)
|
||||
, test
|
||||
"input has non numbers"
|
||||
(assertEqual
|
||||
(Err "could not convert string 'a' to an Int")
|
||||
(slices 2 "0123abc")
|
||||
)
|
||||
]
|
||||
suite "Series"
|
||||
[ test "slices of one"
|
||||
(assertEqual (Ok [ [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ] ])
|
||||
(slices 1 "01234")
|
||||
)
|
||||
, test "slices of two"
|
||||
(assertEqual (Ok [ [ 9, 7 ], [ 7, 8 ], [ 8, 6 ], [ 6, 7 ], [ 7, 5 ], [ 5, 6 ], [ 6, 4 ] ])
|
||||
(slices 2 "97867564")
|
||||
)
|
||||
, test "slices of three"
|
||||
(assertEqual (Ok [ [ 9, 7, 8 ], [ 7, 8, 6 ], [ 8, 6, 7 ], [ 6, 7, 5 ], [ 7, 5, 6 ], [ 5, 6, 4 ] ])
|
||||
(slices 3 "97867564")
|
||||
)
|
||||
, test "slices of four"
|
||||
(assertEqual (Ok [ [ 0, 1, 2, 3 ], [ 1, 2, 3, 4 ] ])
|
||||
(slices 4 "01234")
|
||||
)
|
||||
, test "slices of five"
|
||||
(assertEqual (Ok [ [ 0, 1, 2, 3, 4 ] ])
|
||||
(slices 5 "01234")
|
||||
)
|
||||
, test "overly long slice"
|
||||
(assertEqual (Ok [])
|
||||
(slices 4 "012")
|
||||
)
|
||||
, test "overly short slice"
|
||||
(assertEqual (Err ("Invalid size: 0"))
|
||||
(slices 0 "01234")
|
||||
)
|
||||
, test "input has non numbers"
|
||||
(assertEqual (Err "could not convert string 'a' to an Int")
|
||||
(slices 2 "0123abc")
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -2,49 +2,49 @@ module SpaceAge exposing (..)
|
|||
|
||||
|
||||
type Planet
|
||||
= Mercury
|
||||
| Venus
|
||||
| Earth
|
||||
| Mars
|
||||
| Jupiter
|
||||
| Saturn
|
||||
| Uranus
|
||||
| Neptune
|
||||
= Mercury
|
||||
| Venus
|
||||
| Earth
|
||||
| Mars
|
||||
| Jupiter
|
||||
| Saturn
|
||||
| Uranus
|
||||
| Neptune
|
||||
|
||||
|
||||
earthYearInSeconds =
|
||||
365.25 * 24 * 60 * 60
|
||||
365.25 * 24 * 60 * 60
|
||||
|
||||
|
||||
ageOn : Planet -> Float -> Float
|
||||
ageOn planet seconds =
|
||||
seconds / (secondsPerYear planet)
|
||||
seconds / (secondsPerYear planet)
|
||||
|
||||
|
||||
secondsPerYear : Planet -> Float
|
||||
secondsPerYear planet =
|
||||
earthYearInSeconds
|
||||
* case planet of
|
||||
Mercury ->
|
||||
0.2408467
|
||||
earthYearInSeconds
|
||||
* case planet of
|
||||
Mercury ->
|
||||
0.2408467
|
||||
|
||||
Venus ->
|
||||
0.61519726
|
||||
Venus ->
|
||||
0.61519726
|
||||
|
||||
Earth ->
|
||||
1
|
||||
Earth ->
|
||||
1
|
||||
|
||||
Mars ->
|
||||
1.8808158
|
||||
Mars ->
|
||||
1.8808158
|
||||
|
||||
Jupiter ->
|
||||
11.862615
|
||||
Jupiter ->
|
||||
11.862615
|
||||
|
||||
Saturn ->
|
||||
29.447498
|
||||
Saturn ->
|
||||
29.447498
|
||||
|
||||
Uranus ->
|
||||
84.016846
|
||||
Uranus ->
|
||||
84.016846
|
||||
|
||||
Neptune ->
|
||||
164.79132
|
||||
Neptune ->
|
||||
164.79132
|
||||
|
|
|
@ -6,35 +6,26 @@ import SpaceAge exposing (Planet(..), ageOn)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"SpaceAge"
|
||||
[ 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)))
|
||||
]
|
||||
suite "SpaceAge"
|
||||
[ 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)))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,17 +5,17 @@ import List
|
|||
|
||||
keep : (a -> Bool) -> List a -> List a
|
||||
keep predicate list =
|
||||
List.foldr (consIf predicate) [] list
|
||||
List.foldr (consIf predicate) [] list
|
||||
|
||||
|
||||
discard : (a -> Bool) -> List a -> List a
|
||||
discard predicate list =
|
||||
List.foldr (consIf (\v -> not <| predicate v)) [] list
|
||||
List.foldr (consIf (\v -> not <| predicate v)) [] list
|
||||
|
||||
|
||||
consIf : (a -> Bool) -> a -> List a -> List a
|
||||
consIf predicate value list =
|
||||
if predicate value then
|
||||
value :: list
|
||||
else
|
||||
list
|
||||
if predicate value then
|
||||
value :: list
|
||||
else
|
||||
list
|
||||
|
|
|
@ -7,103 +7,78 @@ import String
|
|||
|
||||
even : Int -> Bool
|
||||
even number =
|
||||
number % 2 == 0
|
||||
number % 2 == 0
|
||||
|
||||
|
||||
odd : Int -> Bool
|
||||
odd number =
|
||||
number % 2 == 1
|
||||
number % 2 == 1
|
||||
|
||||
|
||||
isFirstLetter : String -> String -> Bool
|
||||
isFirstLetter letter word =
|
||||
(String.left 1 word) == letter
|
||||
(String.left 1 word) == letter
|
||||
|
||||
|
||||
lessThanTen : number -> Bool
|
||||
lessThanTen num =
|
||||
num < 10
|
||||
num < 10
|
||||
|
||||
|
||||
tests : Test
|
||||
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" ])
|
||||
)
|
||||
]
|
||||
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" ])
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -3,37 +3,37 @@ module Sublist exposing (..)
|
|||
|
||||
version : Int
|
||||
version =
|
||||
2
|
||||
2
|
||||
|
||||
|
||||
type ListComparison
|
||||
= Equal
|
||||
| Superlist
|
||||
| Sublist
|
||||
| Unequal
|
||||
= Equal
|
||||
| Superlist
|
||||
| Sublist
|
||||
| Unequal
|
||||
|
||||
|
||||
sublist : List a -> List a -> ListComparison
|
||||
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 =
|
||||
Maybe.withDefault [] (List.tail sublist)
|
||||
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
|
||||
let
|
||||
getLastInList sublist =
|
||||
Maybe.withDefault [] (List.tail sublist)
|
||||
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
|
||||
|
|
|
@ -6,68 +6,48 @@ import Sublist exposing (version, sublist, ListComparison(..))
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Sublist"
|
||||
[ test
|
||||
"the solution is for the correct version of the test"
|
||||
(assertEqual 2 version)
|
||||
, 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..5001] [1..5000]))
|
||||
, 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 ]))
|
||||
]
|
||||
suite "Sublist"
|
||||
[ test "the solution is for the correct version of the test"
|
||||
(assertEqual 2 version)
|
||||
, 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..5001] [1..5000]))
|
||||
, 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 ]))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -3,9 +3,9 @@ module SumOfMultiples exposing (..)
|
|||
|
||||
sumOfMultiples : List Int -> Int -> Int
|
||||
sumOfMultiples multiples limit =
|
||||
List.sum (List.filter (inMultiples multiples) [1..(limit - 1)])
|
||||
List.sum (List.filter (inMultiples multiples) [1..(limit - 1)])
|
||||
|
||||
|
||||
inMultiples : List Int -> Int -> Bool
|
||||
inMultiples multiples candidate =
|
||||
List.any (\factor -> candidate % factor == 0) multiples
|
||||
List.any (\factor -> candidate % factor == 0) multiples
|
||||
|
|
|
@ -6,17 +6,16 @@ import SumOfMultiples exposing (sumOfMultiples)
|
|||
|
||||
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))
|
||||
]
|
||||
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))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -5,17 +5,17 @@ import Set
|
|||
|
||||
triangleKind : number -> number -> number -> Result String String
|
||||
triangleKind x y z =
|
||||
if x <= 0 || y <= 0 || z <= 0 then
|
||||
Err "Invalid lengths"
|
||||
else if x + y <= z || x + z <= y || y + z <= x then
|
||||
Err "Violates inequality"
|
||||
else
|
||||
case Set.size (Set.fromList [ x, y, z ]) of
|
||||
1 ->
|
||||
Ok "equilateral"
|
||||
if x <= 0 || y <= 0 || z <= 0 then
|
||||
Err "Invalid lengths"
|
||||
else if x + y <= z || x + z <= y || y + z <= x then
|
||||
Err "Violates inequality"
|
||||
else
|
||||
case Set.size (Set.fromList [ x, y, z ]) of
|
||||
1 ->
|
||||
Ok "equilateral"
|
||||
|
||||
2 ->
|
||||
Ok "isosceles"
|
||||
2 ->
|
||||
Ok "isosceles"
|
||||
|
||||
_ ->
|
||||
Ok "scalene"
|
||||
_ ->
|
||||
Ok "scalene"
|
||||
|
|
|
@ -6,56 +6,40 @@ import Triangle exposing (triangleKind)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"triangleKind"
|
||||
[ test
|
||||
"equilateral triangles have equal sides"
|
||||
(assertEqual (Ok "equilateral") (triangleKind 2 2 2))
|
||||
, test
|
||||
"larger equilateral triangles also have equal sides"
|
||||
(assertEqual (Ok "equilateral") (triangleKind 10 10 10))
|
||||
, test
|
||||
"isosceles triangles have last two sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 3 4 4))
|
||||
, test
|
||||
"isosceles triangles have first and last sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 4 3 4))
|
||||
, test
|
||||
"isosceles triangles have two first sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 4 4 3))
|
||||
, test
|
||||
"isosceles triangles have in fact exactly two sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 10 10 2))
|
||||
, test
|
||||
"scalene triangles have no equal sides"
|
||||
(assertEqual (Ok "scalene") (triangleKind 3 4 5))
|
||||
, test
|
||||
"scalene triangles have no equal sides at a larger scale too"
|
||||
(assertEqual (Ok "scalene") (triangleKind 10 11 12))
|
||||
, test
|
||||
"scalene triangles have no equal sides at a larger scale too 2"
|
||||
(assertEqual (Ok "scalene") (triangleKind 5 4 2))
|
||||
, test
|
||||
"very small triangles are legal"
|
||||
(assertEqual (Ok "scalene") (triangleKind 0.4 0.6 0.3))
|
||||
, test
|
||||
"triangles with no size are illegal"
|
||||
(assertEqual (Err "Invalid lengths") (triangleKind 0 0 0))
|
||||
, test
|
||||
"triangles with negative sides are illegal"
|
||||
(assertEqual (Err "Invalid lengths") (triangleKind 3 4 -5))
|
||||
, test
|
||||
"triangles violating triangle inequality are illegal 1"
|
||||
(assertEqual (Err "Violates inequality") (triangleKind 1 1 3))
|
||||
, test
|
||||
"triangles violating triangle inequality are illegal 2"
|
||||
(assertEqual (Err "Violates inequality") (triangleKind 2 4 2))
|
||||
, test
|
||||
"triangles violating triangle inequality are illegal 3"
|
||||
(assertEqual (Err "Violates inequality") (triangleKind 7 3 2))
|
||||
]
|
||||
suite "triangleKind"
|
||||
[ test "equilateral triangles have equal sides"
|
||||
(assertEqual (Ok "equilateral") (triangleKind 2 2 2))
|
||||
, test "larger equilateral triangles also have equal sides"
|
||||
(assertEqual (Ok "equilateral") (triangleKind 10 10 10))
|
||||
, test "isosceles triangles have last two sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 3 4 4))
|
||||
, test "isosceles triangles have first and last sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 4 3 4))
|
||||
, test "isosceles triangles have two first sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 4 4 3))
|
||||
, test "isosceles triangles have in fact exactly two sides equal"
|
||||
(assertEqual (Ok "isosceles") (triangleKind 10 10 2))
|
||||
, test "scalene triangles have no equal sides"
|
||||
(assertEqual (Ok "scalene") (triangleKind 3 4 5))
|
||||
, test "scalene triangles have no equal sides at a larger scale too"
|
||||
(assertEqual (Ok "scalene") (triangleKind 10 11 12))
|
||||
, test "scalene triangles have no equal sides at a larger scale too 2"
|
||||
(assertEqual (Ok "scalene") (triangleKind 5 4 2))
|
||||
, test "very small triangles are legal"
|
||||
(assertEqual (Ok "scalene") (triangleKind 0.4 0.6 0.3))
|
||||
, test "triangles with no size are illegal"
|
||||
(assertEqual (Err "Invalid lengths") (triangleKind 0 0 0))
|
||||
, test "triangles with negative sides are illegal"
|
||||
(assertEqual (Err "Invalid lengths") (triangleKind 3 4 -5))
|
||||
, test "triangles violating triangle inequality are illegal 1"
|
||||
(assertEqual (Err "Violates inequality") (triangleKind 1 1 3))
|
||||
, test "triangles violating triangle inequality are illegal 2"
|
||||
(assertEqual (Err "Violates inequality") (triangleKind 2 4 2))
|
||||
, test "triangles violating triangle inequality are illegal 3"
|
||||
(assertEqual (Err "Violates inequality") (triangleKind 7 3 2))
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
|
@ -7,18 +7,18 @@ import Regex
|
|||
|
||||
wordCount : String -> Dict String Int
|
||||
wordCount sentence =
|
||||
sentence
|
||||
|> String.toLower
|
||||
|> depunctuate
|
||||
|> String.words
|
||||
|> List.foldl (\w d -> Dict.update w incrMaybe d) Dict.empty
|
||||
sentence
|
||||
|> String.toLower
|
||||
|> depunctuate
|
||||
|> String.words
|
||||
|> List.foldl (\w d -> Dict.update w incrMaybe d) Dict.empty
|
||||
|
||||
|
||||
depunctuate : String -> String
|
||||
depunctuate =
|
||||
Regex.replace Regex.All (Regex.regex "[^a-z0-9 ]") (\_ -> "")
|
||||
Regex.replace Regex.All (Regex.regex "[^a-z0-9 ]") (\_ -> "")
|
||||
|
||||
|
||||
incrMaybe : Maybe Int -> Maybe Int
|
||||
incrMaybe maybe =
|
||||
(Maybe.withDefault 0 maybe) + 1 |> Just
|
||||
(Maybe.withDefault 0 maybe) + 1 |> Just
|
||||
|
|
|
@ -7,47 +7,34 @@ import WordCount exposing (wordCount)
|
|||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Word Count"
|
||||
[ test
|
||||
"count one word"
|
||||
(assertEqual
|
||||
[ ( "word", 1 ) ]
|
||||
(wordCount "word" |> Dict.toList)
|
||||
)
|
||||
, test
|
||||
"count one of each word"
|
||||
(assertEqual
|
||||
[ ( "each", 1 ), ( "of", 1 ), ( "one", 1 ) ]
|
||||
(wordCount "one of each" |> Dict.toList)
|
||||
)
|
||||
, test
|
||||
"multiple occurrences of a word"
|
||||
(assertEqual
|
||||
[ ( "blue", 1 ), ( "fish", 4 ), ( "one", 1 ), ( "red", 1 ), ( "two", 1 ) ]
|
||||
(wordCount "one fish two fish red fish blue fish" |> Dict.toList)
|
||||
)
|
||||
, test
|
||||
"ignore punctuation"
|
||||
(assertEqual
|
||||
[ ( "as", 1 ), ( "car", 1 ), ( "carpet", 1 ), ( "java", 1 ), ( "javascript", 1 ) ]
|
||||
(wordCount "car : carpet as java : javascript!!&@$%^&" |> Dict.toList)
|
||||
)
|
||||
, test
|
||||
"include numbers"
|
||||
(assertEqual
|
||||
[ ( "1", 1 ), ( "2", 1 ), ( "testing", 2 ) ]
|
||||
(wordCount "testing, 1, 2 testing" |> Dict.toList)
|
||||
)
|
||||
, test
|
||||
"normalize case"
|
||||
(assertEqual
|
||||
[ ( "go", 3 ), ( "stop", 2 ) ]
|
||||
(wordCount "go Go GO Stop stop" |> Dict.toList)
|
||||
)
|
||||
]
|
||||
suite "Word Count"
|
||||
[ test "count one word"
|
||||
(assertEqual [ ( "word", 1 ) ]
|
||||
(wordCount "word" |> Dict.toList)
|
||||
)
|
||||
, test "count one of each word"
|
||||
(assertEqual [ ( "each", 1 ), ( "of", 1 ), ( "one", 1 ) ]
|
||||
(wordCount "one of each" |> Dict.toList)
|
||||
)
|
||||
, test "multiple occurrences of a word"
|
||||
(assertEqual [ ( "blue", 1 ), ( "fish", 4 ), ( "one", 1 ), ( "red", 1 ), ( "two", 1 ) ]
|
||||
(wordCount "one fish two fish red fish blue fish" |> Dict.toList)
|
||||
)
|
||||
, test "ignore punctuation"
|
||||
(assertEqual [ ( "as", 1 ), ( "car", 1 ), ( "carpet", 1 ), ( "java", 1 ), ( "javascript", 1 ) ]
|
||||
(wordCount "car : carpet as java : javascript!!&@$%^&" |> Dict.toList)
|
||||
)
|
||||
, test "include numbers"
|
||||
(assertEqual [ ( "1", 1 ), ( "2", 1 ), ( "testing", 2 ) ]
|
||||
(wordCount "testing, 1, 2 testing" |> Dict.toList)
|
||||
)
|
||||
, test "normalize case"
|
||||
(assertEqual [ ( "go", 3 ), ( "stop", 2 ) ]
|
||||
(wordCount "go Go GO Stop stop" |> Dict.toList)
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
||||
runSuite tests
|
||||
|
|
Loading…
Reference in a new issue