Merge pull request #54 from tgecho/allergies

Add allergies exercise
This commit is contained in:
Lew Parker 2016-04-07 12:54:45 -06:00
commit d50ce07e6e
6 changed files with 124 additions and 2 deletions

View file

@ -23,7 +23,8 @@
"sublist",
"nucleotide-count",
"phone-number",
"grade-school"
"grade-school",
"allergies"
],
"deprecated": [

View file

@ -24,7 +24,8 @@
"./exercises/space-age",
"./exercises/nucleotide-count",
"./exercises/phone-number",
"./exercises/grade-school"
"./exercises/grade-school",
"./exercises/allergies"
],
"exposed-modules": [],
"dependencies": {

View file

@ -0,0 +1 @@
module Allergies (..) where

View file

@ -0,0 +1,30 @@
module Allergies (..) where
import List
import Bitwise
isAllergicTo : String -> Int -> Bool
isAllergicTo name 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 String
allergies =
[ "eggs"
, "peanuts"
, "shellfish"
, "strawberries"
, "tomatoes"
, "chocolate"
, "pollen"
, "cats"
]

View file

@ -0,0 +1,73 @@
module Main (..) where
import Task
import Console
import ElmTest exposing (..)
import Allergies exposing (isAllergicTo, toList)
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)))
]
, 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)
)
]
]
port runner : Signal (Task.Task x ())
port runner =
Console.run (consoleRunner tests)

View 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"
}