Skip to content
This repository was archived by the owner on Feb 11, 2018. It is now read-only.

Commit

Permalink
Add word-count exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
mbertheau committed May 22, 2015
1 parent 89493f7 commit 08a2941
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"repository": "https://github.com/exercism/xracket",
"active": false,
"problems": [
"bob"
"bob",
"word-count"
]
}
8 changes: 8 additions & 0 deletions word-count/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#lang racket

(define (word-count word)
(for/fold ([result (hash)])
([word (in-list (string-split word))])
(hash-set result word (add1 (hash-ref result word 0)))))

(provide word-count)
38 changes: 38 additions & 0 deletions word-count/word-count-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#lang racket

(require rackunit
rackunit/text-ui
"word-count.rkt")

(define suite
(test-suite
"Tests for the word-count exercise"
(check-equal? (word-count "word")
'#hash(("word" . 1))
"one word")
(check-equal? (word-count "one of each")
'#hash(("one" . 1) ("of" . 1) ("each" . 1))
"one of each")
(check-equal? (word-count "one fish two fish red fish blue fish")
'#hash(("one" . 1) ("fish" . 4) ("two" . 1) ("red" . 1) ("blue" . 1))
"multiple occurrences")
(check-equal? (word-count "car : carpet as java : javascript!!&@$%^&")
'#hash(("car" . 1) (":" . 2) ("carpet" . 1) ("as" . 1) ("java" . 1)
("javascript!!&@$%^&" . 1))
"preserves punctuation")
(check-equal? (word-count "testing 1 2 testing")
'#hash(("testing" . 2) ("1" . 1) ("2" . 1))
"include numbers")
(check-equal? (word-count "go Go GO")
'#hash(("go" . 1) ("Go" . 1) ("GO" . 1))
"mixed case")
(check-equal? (word-count "wait for it")
'#hash(("wait" . 1) ("for" . 1) ("it" . 1))
"multiple spaces")
(check-equal? (word-count "rah rah ah ah ah\nroma roma ma\nga ga oh la la\nwant your bad romance")
'#hash(("rah" . 2) ("ah" . 3) ("roma" . 2) ("ma" . 1) ("ga" . 2) ("oh" . 1)
("la" . 2) ("want" . 1) ("your" . 1) ("bad" . 1)
("romance" . 1))
"newlines")))

(exit (if (zero? (run-tests suite)) 0 1))

0 comments on commit 08a2941

Please sign in to comment.