mirror of
https://github.com/correl/elm.git
synced 2024-11-15 19:19:31 +00:00
Merge pull request #39 from tgecho/bewatts-exercises
Cherry picked exercises from bewatts/xelm
This commit is contained in:
commit
8ead7aabfb
35 changed files with 855 additions and 15 deletions
33
config.json
33
config.json
|
@ -5,18 +5,26 @@
|
|||
"active": false,
|
||||
"test_pattern": "TODO",
|
||||
"problems": [
|
||||
"hello-world",
|
||||
"bob",
|
||||
"leap",
|
||||
"raindrops",
|
||||
"pangram",
|
||||
"triangle",
|
||||
"anagram",
|
||||
"difference-of-squares",
|
||||
"word-count",
|
||||
"hamming",
|
||||
"rna-transcription",
|
||||
"run-length-encoding"
|
||||
"hello-world",
|
||||
"bob",
|
||||
"leap",
|
||||
"raindrops",
|
||||
"pangram",
|
||||
"accumulate",
|
||||
"triangle",
|
||||
"anagram",
|
||||
"space-age",
|
||||
"strain",
|
||||
"difference-of-squares",
|
||||
"word-count",
|
||||
"sum-of-multiples",
|
||||
"hamming",
|
||||
"rna-transcription",
|
||||
"run-length-encoding",
|
||||
"sublist",
|
||||
"nucleotide-count",
|
||||
"phone-number",
|
||||
"grade-school"
|
||||
],
|
||||
"deprecated": [
|
||||
|
||||
|
@ -28,6 +36,5 @@
|
|||
"docs"
|
||||
],
|
||||
"foregone": [
|
||||
|
||||
]
|
||||
}
|
||||
|
|
|
@ -16,7 +16,15 @@
|
|||
"./exercises/difference-of-squares",
|
||||
"./exercises/anagram",
|
||||
"./exercises/raindrops",
|
||||
"./exercises/triangle"
|
||||
"./exercises/triangle",
|
||||
"./exercises/accumulate",
|
||||
"./exercises/sublist",
|
||||
"./exercises/sum-of-multiples",
|
||||
"./exercises/strain",
|
||||
"./exercises/space-age",
|
||||
"./exercises/nucleotide-count",
|
||||
"./exercises/phone-number",
|
||||
"./exercises/grade-school"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
|
|
1
exercises/accumulate/Accumulate.elm
Normal file
1
exercises/accumulate/Accumulate.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module Accumulate (..) where
|
6
exercises/accumulate/Accumulate.example
Normal file
6
exercises/accumulate/Accumulate.example
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Accumulate (..) where
|
||||
|
||||
|
||||
accumulate : (a -> b) -> List a -> List b
|
||||
accumulate func input =
|
||||
List.foldr (\v c -> func v :: c) [] input
|
36
exercises/accumulate/AccumulateTests.elm
Normal file
36
exercises/accumulate/AccumulateTests.elm
Normal file
|
@ -0,0 +1,36 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
import Accumulate exposing (accumulate)
|
||||
import String
|
||||
|
||||
|
||||
square : Int -> Int
|
||||
square x =
|
||||
x * x
|
||||
|
||||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Accumulate"
|
||||
[ test
|
||||
"[]] Accumulate"
|
||||
(assertEqual [] (accumulate square []))
|
||||
, test
|
||||
"square Accumulate"
|
||||
(assertEqual [ 1, 4, 9 ] (accumulate square [ 1, 2, 3 ]))
|
||||
, test
|
||||
"toUpper Accumulate"
|
||||
(assertEqual [ "HELLO", "WORLD" ] (accumulate String.toUpper [ "hello", "world" ]))
|
||||
, test
|
||||
"reverse Accumulate"
|
||||
(assertEqual [ "olleh", "dlrow" ] (accumulate String.reverse [ "hello", "world" ]))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/accumulate/elm-package.json
Normal file
16
exercises/accumulate/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
2
exercises/grade-school/GradeSchool.elm
Normal file
2
exercises/grade-school/GradeSchool.elm
Normal file
|
@ -0,0 +1,2 @@
|
|||
module GradeSchool (..) where
|
||||
|
40
exercises/grade-school/GradeSchool.example
Normal file
40
exercises/grade-school/GradeSchool.example
Normal file
|
@ -0,0 +1,40 @@
|
|||
module GradeSchool (..) where
|
||||
|
||||
import Dict exposing (..)
|
||||
|
||||
|
||||
type alias Grade =
|
||||
Int
|
||||
|
||||
|
||||
type alias Student =
|
||||
String
|
||||
|
||||
|
||||
type alias School =
|
||||
Dict Int (List Student)
|
||||
|
||||
|
||||
empty : School
|
||||
empty =
|
||||
Dict.empty
|
||||
|
||||
|
||||
addStudent : Grade -> Student -> School -> School
|
||||
addStudent grade student 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
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
|
||||
|
||||
allStudents : School -> List ( Grade, List Student )
|
||||
allStudents school =
|
||||
Dict.toList school |> List.sortBy fst
|
77
exercises/grade-school/GradeSchoolTests.elm
Normal file
77
exercises/grade-school/GradeSchoolTests.elm
Normal file
|
@ -0,0 +1,77 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
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))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/grade-school/elm-package.json
Normal file
16
exercises/grade-school/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
|
@ -5,6 +5,7 @@ helloWorld : Maybe String -> String
|
|||
helloWorld name =
|
||||
case name of
|
||||
Just name ->
|
||||
"Hello, " ++ name ++ "!"
|
||||
"Hello, " ++ name ++ "!"
|
||||
|
||||
Nothing ->
|
||||
"Hello, World!"
|
||||
|
|
1
exercises/nucleotide-count/NucleotideCount.elm
Normal file
1
exercises/nucleotide-count/NucleotideCount.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module NucleotideCount (..) where
|
18
exercises/nucleotide-count/NucleotideCount.example
Normal file
18
exercises/nucleotide-count/NucleotideCount.example
Normal file
|
@ -0,0 +1,18 @@
|
|||
module NucleotideCount (..) where
|
||||
|
||||
import String
|
||||
import List
|
||||
|
||||
|
||||
nucleotideCounts : String -> List ( Char, Int )
|
||||
nucleotideCounts sequence =
|
||||
[ (getCount 'A' sequence)
|
||||
, (getCount 'T' sequence)
|
||||
, (getCount 'C' sequence)
|
||||
, (getCount 'G' sequence)
|
||||
]
|
||||
|
||||
|
||||
getCount : Char -> String -> ( Char, Int )
|
||||
getCount base sequence =
|
||||
( base, (List.length (String.split (String.fromChar base) sequence)) - 1 )
|
36
exercises/nucleotide-count/NucleotideCountTests.elm
Normal file
36
exercises/nucleotide-count/NucleotideCountTests.elm
Normal file
|
@ -0,0 +1,36 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
import NucleotideCount exposing (nucleotideCounts)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"NucleotideCount"
|
||||
[ test
|
||||
"empty dna strand has no nucleotides"
|
||||
(assertEqual
|
||||
[ ( 'A', 0 ), ( 'T', 0 ), ( 'C', 0 ), ( 'G', 0 ) ]
|
||||
(nucleotideCounts "")
|
||||
)
|
||||
, test
|
||||
"repetitive-sequence-has-only-guanosine"
|
||||
(assertEqual
|
||||
[ ( 'A', 0 ), ( 'T', 0 ), ( 'C', 0 ), ( 'G', 8 ) ]
|
||||
(nucleotideCounts "GGGGGGGG")
|
||||
)
|
||||
, test
|
||||
"counts all nucleotides"
|
||||
(assertEqual
|
||||
[ ( 'A', 20 ), ( 'T', 21 ), ( 'C', 12 ), ( 'G', 17 ) ]
|
||||
(nucleotideCounts "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC")
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/nucleotide-count/elm-package.json
Normal file
16
exercises/nucleotide-count/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
1
exercises/phone-number/PhoneNumber.elm
Normal file
1
exercises/phone-number/PhoneNumber.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module PhoneNumber (..) where
|
44
exercises/phone-number/PhoneNumber.example
Normal file
44
exercises/phone-number/PhoneNumber.example
Normal file
|
@ -0,0 +1,44 @@
|
|||
module PhoneNumber (..) where
|
||||
|
||||
import String
|
||||
|
||||
|
||||
nums =
|
||||
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
|
||||
|
||||
|
||||
getNumber : String -> Maybe String
|
||||
getNumber text =
|
||||
getValidNum (String.filter isDigit text)
|
||||
|
||||
|
||||
isDigit : Char -> Bool
|
||||
isDigit char =
|
||||
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
|
||||
|
||||
|
||||
prettyPrint : String -> Maybe String
|
||||
prettyPrint 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)
|
||||
]
|
54
exercises/phone-number/PhoneNumberTests.elm
Normal file
54
exercises/phone-number/PhoneNumberTests.elm
Normal file
|
@ -0,0 +1,54 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
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"))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/phone-number/elm-package.json
Normal file
16
exercises/phone-number/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
11
exercises/space-age/SpaceAge.elm
Normal file
11
exercises/space-age/SpaceAge.elm
Normal file
|
@ -0,0 +1,11 @@
|
|||
module SpaceAge (..) where
|
||||
|
||||
type Planet
|
||||
= Mercury
|
||||
| Venus
|
||||
| Earth
|
||||
| Mars
|
||||
| Jupiter
|
||||
| Saturn
|
||||
| Uranus
|
||||
| Neptune
|
50
exercises/space-age/SpaceAge.example
Normal file
50
exercises/space-age/SpaceAge.example
Normal file
|
@ -0,0 +1,50 @@
|
|||
module SpaceAge (..) where
|
||||
|
||||
|
||||
type Planet
|
||||
= Mercury
|
||||
| Venus
|
||||
| Earth
|
||||
| Mars
|
||||
| Jupiter
|
||||
| Saturn
|
||||
| Uranus
|
||||
| Neptune
|
||||
|
||||
|
||||
earthYearInSeconds =
|
||||
365.25 * 24 * 60 * 60
|
||||
|
||||
|
||||
ageOn : Planet -> Float -> Float
|
||||
ageOn planet seconds =
|
||||
seconds / (secondsPerYear planet)
|
||||
|
||||
|
||||
secondsPerYear : Planet -> Float
|
||||
secondsPerYear planet =
|
||||
earthYearInSeconds
|
||||
* case planet of
|
||||
Mercury ->
|
||||
0.2408467
|
||||
|
||||
Venus ->
|
||||
0.61519726
|
||||
|
||||
Earth ->
|
||||
1
|
||||
|
||||
Mars ->
|
||||
1.8808158
|
||||
|
||||
Jupiter ->
|
||||
11.862615
|
||||
|
||||
Saturn ->
|
||||
29.447498
|
||||
|
||||
Uranus ->
|
||||
84.016846
|
||||
|
||||
Neptune ->
|
||||
164.79132
|
42
exercises/space-age/SpaceAgeTests.elm
Normal file
42
exercises/space-age/SpaceAgeTests.elm
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
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)))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/space-age/elm-package.json
Normal file
16
exercises/space-age/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
1
exercises/strain/Strain.elm
Normal file
1
exercises/strain/Strain.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module Strain (..) where
|
21
exercises/strain/Strain.example
Normal file
21
exercises/strain/Strain.example
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Strain (..) where
|
||||
|
||||
import List
|
||||
|
||||
|
||||
keep : (a -> Bool) -> List a -> List a
|
||||
keep 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
|
||||
|
||||
|
||||
consIf : (a -> Bool) -> a -> List a -> List a
|
||||
consIf predicate value list =
|
||||
if predicate value then
|
||||
value :: list
|
||||
else
|
||||
list
|
111
exercises/strain/StrainTests.elm
Normal file
111
exercises/strain/StrainTests.elm
Normal file
|
@ -0,0 +1,111 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
import Strain exposing (keep, discard)
|
||||
import String
|
||||
|
||||
|
||||
even : Int -> Bool
|
||||
even number =
|
||||
number % 2 == 0
|
||||
|
||||
|
||||
odd : Int -> Bool
|
||||
odd number =
|
||||
number % 2 == 1
|
||||
|
||||
|
||||
isFirstLetter : String -> String -> Bool
|
||||
isFirstLetter letter word =
|
||||
(String.left 1 word) == letter
|
||||
|
||||
|
||||
lessThanTen : number -> Bool
|
||||
lessThanTen num =
|
||||
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" ])
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/strain/elm-package.json
Normal file
16
exercises/strain/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
1
exercises/sublist/Sublist.elm
Normal file
1
exercises/sublist/Sublist.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module Sublist (..) where
|
35
exercises/sublist/Sublist.example
Normal file
35
exercises/sublist/Sublist.example
Normal file
|
@ -0,0 +1,35 @@
|
|||
module Sublist (..) where
|
||||
|
||||
import List exposing (..)
|
||||
import String
|
||||
|
||||
|
||||
sublist : List a -> List a -> String
|
||||
sublist alist blist =
|
||||
if alist == blist then
|
||||
"Equal"
|
||||
else if inList alist blist then
|
||||
"Superlist"
|
||||
else if inList blist alist then
|
||||
"Sublist"
|
||||
else
|
||||
"Unequal"
|
||||
|
||||
|
||||
inList : List a -> List a -> Bool
|
||||
inList alist blist =
|
||||
let
|
||||
getLastInList sublist =
|
||||
case (List.tail sublist) of
|
||||
Just list ->
|
||||
list
|
||||
|
||||
Nothing ->
|
||||
[]
|
||||
in
|
||||
if (List.length alist) < (List.length blist) then
|
||||
False
|
||||
else if (List.take (List.length blist) alist) == blist then
|
||||
True
|
||||
else
|
||||
inList (getLastInList alist) blist
|
72
exercises/sublist/SublistTests.elm
Normal file
72
exercises/sublist/SublistTests.elm
Normal file
|
@ -0,0 +1,72 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
import Sublist exposing (sublist)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests =
|
||||
suite
|
||||
"Sublist"
|
||||
[ test
|
||||
"empty equals empty"
|
||||
(assertEqual "Equal" (sublist [] []))
|
||||
, test
|
||||
"empty is a sublist of anything"
|
||||
(assertEqual "Sublist" (sublist [] [ 1, 2 ]))
|
||||
, test
|
||||
"anything is a superlist of empty"
|
||||
(assertEqual "Superlist" (sublist [ 1, 2 ] []))
|
||||
, test
|
||||
"1 is not 2"
|
||||
(assertEqual "Unequal" (sublist [ 1 ] [ 2 ]))
|
||||
, test
|
||||
"compare larger equal lists"
|
||||
(assertEqual "Equal" (sublist [ 1, 1, 1 ] [ 1, 1, 1 ]))
|
||||
, test
|
||||
"sublist at start"
|
||||
(assertEqual "Sublist" (sublist [ 1, 2, 3 ] [ 1, 2, 3, 4, 5 ]))
|
||||
, test
|
||||
"sublist in the middle"
|
||||
(assertEqual "Sublist" (sublist [ 4, 3, 2 ] [ 5, 4, 3, 2, 1 ]))
|
||||
, test
|
||||
"sublist at end"
|
||||
(assertEqual "Sublist" (sublist [ 3, 4, 5 ] [ 1, 2, 3, 4, 5 ]))
|
||||
, test
|
||||
"partially matching sublist at start"
|
||||
(assertEqual "Sublist" (sublist [ 1, 1, 2 ] [ 1, 1, 1, 2 ]))
|
||||
, test
|
||||
"sublist early in huge list"
|
||||
(assertEqual "Sublist" (sublist [ 3, 4, 5 ] [1..100000]))
|
||||
, test
|
||||
"huge sublist not in list"
|
||||
(assertEqual "Unequal" (sublist [10..100001] [1..100000]))
|
||||
, test
|
||||
"superlist at start"
|
||||
(assertEqual "Superlist" (sublist [ 1, 2, 3, 4, 5 ] [ 1, 2, 3 ]))
|
||||
, test
|
||||
"superlist in middle"
|
||||
(assertEqual "Superlist" (sublist [ 5, 4, 3, 2, 1 ] [ 4, 3, 2 ]))
|
||||
, test
|
||||
"superlist at end"
|
||||
(assertEqual "Superlist" (sublist [ 1, 2, 3, 4, 5 ] [ 3, 4, 5 ]))
|
||||
, test
|
||||
"partially matching superlist at start"
|
||||
(assertEqual "Superlist" (sublist [ 1, 1, 1, 2 ] [ 1, 1, 2 ]))
|
||||
, test
|
||||
"superlist early in huge list"
|
||||
(assertEqual "Superlist" (sublist [1..100000] [ 3, 4, 5 ]))
|
||||
, test
|
||||
"recurring values sublist"
|
||||
(assertEqual "Sublist" (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 1, 2, 3, 2, 1 ]))
|
||||
, test
|
||||
"recurring values unequal"
|
||||
(assertEqual "Unequal" (sublist [ 1, 2, 1, 2, 3 ] [ 1, 2, 3, 1, 2, 3, 2, 3, 2, 1 ]))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/sublist/elm-package.json
Normal file
16
exercises/sublist/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
1
exercises/sum-of-multiples/SumOfMultiples.elm
Normal file
1
exercises/sum-of-multiples/SumOfMultiples.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module SumOfMultiples (..) where
|
11
exercises/sum-of-multiples/SumOfMultiples.example
Normal file
11
exercises/sum-of-multiples/SumOfMultiples.example
Normal file
|
@ -0,0 +1,11 @@
|
|||
module SumOfMultiples (..) where
|
||||
|
||||
|
||||
sumOfMultiples : List Int -> Int -> Int
|
||||
sumOfMultiples multiples limit =
|
||||
List.sum (List.filter (inMultiples multiples) [1..(limit - 1)])
|
||||
|
||||
|
||||
inMultiples : List Int -> Int -> Bool
|
||||
inMultiples multiples candidate =
|
||||
List.any (\factor -> candidate % factor == 0) multiples
|
24
exercises/sum-of-multiples/SumOfMultiplesTests.elm
Normal file
24
exercises/sum-of-multiples/SumOfMultiplesTests.elm
Normal file
|
@ -0,0 +1,24 @@
|
|||
module Main (..) where
|
||||
|
||||
import Task
|
||||
import Console
|
||||
import ElmTest exposing (..)
|
||||
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))
|
||||
]
|
||||
|
||||
|
||||
port runner : Signal (Task.Task x ())
|
||||
port runner =
|
||||
Console.run (consoleRunner tests)
|
16
exercises/sum-of-multiples/elm-package.json
Normal file
16
exercises/sum-of-multiples/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/xelm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
|
||||
"elm-lang/core": "2.0.0 <= v < 4.0.0",
|
||||
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.15.0 <= v < 0.17.0"
|
||||
}
|
Loading…
Reference in a new issue