mirror of
https://github.com/correl/elm.git
synced 2025-03-07 04:35:48 -10:00
Add word-count exercise (closes #25)
This commit is contained in:
parent
38eafb9a5a
commit
b822d3b83e
6 changed files with 98 additions and 0 deletions
|
@ -10,6 +10,7 @@
|
|||
"pangram",
|
||||
"rna-transcription",
|
||||
"hamming",
|
||||
"word-count",
|
||||
"bob"
|
||||
],
|
||||
"deprecated": [
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"./exercises/pangram",
|
||||
"./exercises/rna-transcription",
|
||||
"./exercises/hamming",
|
||||
"./exercises/word-count",
|
||||
"./exercises/bob"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
|
|
1
exercises/word-count/WordCount.elm
Normal file
1
exercises/word-count/WordCount.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module WordCount (..) where
|
24
exercises/word-count/WordCount.example
Normal file
24
exercises/word-count/WordCount.example
Normal 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
|
55
exercises/word-count/WordCountTests.elm
Normal file
55
exercises/word-count/WordCountTests.elm
Normal 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)
|
16
exercises/word-count/elm-package.json
Normal file
16
exercises/word-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"
|
||||
}
|
Loading…
Add table
Reference in a new issue