Add word-count exercise (closes #25)

This commit is contained in:
Erik Simmler 2016-03-12 06:34:35 -05:00
parent 38eafb9a5a
commit b822d3b83e
6 changed files with 98 additions and 0 deletions

View file

@ -10,6 +10,7 @@
"pangram",
"rna-transcription",
"hamming",
"word-count",
"bob"
],
"deprecated": [

View file

@ -10,6 +10,7 @@
"./exercises/pangram",
"./exercises/rna-transcription",
"./exercises/hamming",
"./exercises/word-count",
"./exercises/bob"
],
"exposed-modules": [],

View file

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

View file

@ -0,0 +1,24 @@
module WordCount (..) where
import String
import Dict exposing (Dict)
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
depunctuate : String -> String
depunctuate =
Regex.replace Regex.All (Regex.regex "[^a-z0-9 ]") (\_ -> "")
incrMaybe : Maybe Int -> Maybe Int
incrMaybe maybe =
(Maybe.withDefault 0 maybe) + 1 |> Just

View file

@ -0,0 +1,55 @@
module Main (..) where
import Task
import Console
import ElmTest exposing (..)
import Dict exposing (Dict)
import WordCount exposing (wordCount)
tests : Test
tests =
suite
"Hamming"
[ test
"count one word"
(assertEqual
(Dict.fromList [ ( "word", 1 ) ])
(wordCount "word")
)
, test
"count one of each word"
(assertEqual
(Dict.fromList [ ( "one", 1 ), ( "of", 1 ), ( "each", 1 ) ])
(wordCount "one of each")
)
, test
"multiple occurrences of a word"
(assertEqual
(Dict.fromList [ ( "one", 1 ), ( "fish", 4 ), ( "two", 1 ), ( "red", 1 ), ( "blue", 1 ) ])
(wordCount "one fish two fish red fish blue fish")
)
, test
"ignore punctuation"
(assertEqual
(Dict.fromList [ ( "car", 1 ), ( "carpet", 1 ), ( "as", 1 ), ( "java", 1 ), ( "javascript", 1 ) ])
(wordCount "car : carpet as java : javascript!!&@$%^&")
)
, test
"include numbers"
(assertEqual
(Dict.fromList [ ( "testing", 2 ), ( "1", 1 ), ( "2", 1 ) ])
(wordCount "testing, 1, 2 testing")
)
, test
"normalize case"
(assertEqual
(Dict.fromList [ ( "go", 3 ), ( "stop", 2 ) ])
(wordCount "go Go GO Stop stop")
)
]
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"
}