diff --git a/config.json b/config.json index 6dc26b3..e0f6c7f 100644 --- a/config.json +++ b/config.json @@ -4,6 +4,7 @@ "repository": "https://github.com/exercism/xracket", "active": false, "problems": [ - "bob" + "bob", + "word-count" ] } diff --git a/word-count/example.rkt b/word-count/example.rkt new file mode 100644 index 0000000..a3f5236 --- /dev/null +++ b/word-count/example.rkt @@ -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) diff --git a/word-count/word-count-test.rkt b/word-count/word-count-test.rkt new file mode 100644 index 0000000..5ab84ad --- /dev/null +++ b/word-count/word-count-test.rkt @@ -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))