Skip to content

Commit fd84cb9

Browse files
authored
Sync word-count tests (#433)
1 parent ec9d755 commit fd84cb9

File tree

5 files changed

+75
-77
lines changed

5 files changed

+75
-77
lines changed

exercises/practice/word-count/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"contributors": [
66
"arguello",
7+
"BNAndras",
78
"cousinitt",
89
"PurityControl",
910
"robertpostill",

exercises/practice/word-count/.meta/example.rkt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,8 @@
33
(provide word-count)
44

55
(define (word-count sentence)
6-
(let ([normalized-sentence (normalize-input sentence)])
7-
(for/fold ([result (hash)])
8-
([word (in-list (string-split normalized-sentence))])
9-
(hash-set result word (add1 (hash-ref result word 0))))))
10-
11-
(define (normalize-input s)
12-
(handle-single-quoted-words
13-
(list->string
14-
(map
15-
(λ (c)
16-
(cond [(char<=? #\a c #\z) c]
17-
[(char<=? #\0 c #\9) c]
18-
[(char=? #\' c) c]
19-
[else #\space]))
20-
(string->list
21-
(string-downcase s))))))
22-
23-
(define (handle-single-quoted-words s)
24-
(regexp-replace* #px"'\\b(\\w+)\\b'" s " \\1 "))
6+
(let* ([lowered (string-downcase sentence)]
7+
[words (regexp-match* #px"[a-z0-9]+(?:'[a-z0-9]+)?" lowered)])
8+
(for/fold ([counts (hash)])
9+
([word (in-list words)])
10+
(hash-set counts word (add1 (hash-ref counts word 0))))))

exercises/practice/word-count/.meta/tests.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ description = "normalize case"
3535

3636
[4185a902-bdb0-4074-864c-f416e42a0f19]
3737
description = "with apostrophes"
38+
include = false
39+
40+
[4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3]
41+
description = "with apostrophes"
42+
reimplements = "4185a902-bdb0-4074-864c-f416e42a0f19"
3843

3944
[be72af2b-8afe-4337-b151-b297202e4a7b]
4045
description = "with quotations"
@@ -47,3 +52,6 @@ description = "multiple spaces not detected as a word"
4752

4853
[50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360]
4954
description = "alternating word separators not detected as a word"
55+
56+
[6d00f1db-901c-4bec-9829-d20eb3044557]
57+
description = "quotation for word with apostrophe"
Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,71 @@
11
#lang racket
22

3-
; Tests adapted from `problem-specifications/canonical-data.json v1.4.0
43
(require "word-count.rkt")
54

65
(module+ test
76
(require rackunit rackunit/text-ui)
87

9-
(define-binary-check (hashes-equal? actual expected)
10-
(equal? (sort (hash->list actual) string<? #:key car)
11-
(sort (hash->list expected) string<? #:key car)))
12-
138
(define suite
149
(test-suite
15-
"For each word in the input, count the number of times it appears in the entire sentence."
16-
17-
(hashes-equal? (word-count "word")
18-
'#hash(("word" . 1))
19-
"count one word")
20-
21-
(hashes-equal? (word-count "one of each")
22-
'#hash(("one" . 1) ("of" . 1) ("each" . 1))
23-
"count one of each word")
24-
25-
(hashes-equal? (word-count "one fish two fish red fish blue fish")
26-
'#hash(("one" . 1) ("fish" . 4) ("two" . 1) ("red" . 1) ("blue" . 1))
27-
"multiple occurrences of a word")
28-
29-
(hashes-equal? (word-count "one,two,three")
30-
'#hash(("one" . 1) ("two" . 1) ("three" . 1))
31-
"handles cramped lists")
32-
33-
(hashes-equal? (word-count "one,\ntwo,\nthree")
34-
'#hash(("one" . 1) ("two" . 1) ("three" . 1))
35-
"handles expanded lists")
36-
37-
(hashes-equal? (word-count "car : carpet as java : javascript!!&@$%^&")
38-
'#hash(("car" . 1) ("carpet" . 1) ("as" . 1) ("java" . 1) ("javascript" . 1))
39-
"ignore punctuation")
40-
41-
(hashes-equal? (word-count "testing, 1, 2, testing")
42-
'#hash(("testing" . 2) ("1" . 1) ("2" . 1))
43-
"include numbers")
44-
45-
(hashes-equal? (word-count "go Go GO Stop stop")
46-
'#hash(("go" . 3) ("stop" . 2))
47-
"normalize case")
48-
49-
(hashes-equal? (word-count "Joe can't tell between 'large' and large.")
50-
'#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("large" . 2)
51-
("and" . 1))
52-
"with quotations")
53-
54-
(hashes-equal? (word-count "Joe can't tell between app, apple and a.")
55-
'#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("app" . 1)
56-
("apple" . 1) ("and" . 1) ("a" . 1))
57-
"substrings from the beginning")
58-
59-
60-
(hashes-equal? (word-count " multiple whitespaces")
61-
'#hash(("multiple" . 1) ("whitespaces" . 1))
62-
"multiple spaces not detected as a word")
63-
64-
(hashes-equal? (word-count ",\n,one,\n ,two \n 'three'")
65-
'#hash(("one" . 1) ("two" . 1) ("three" . 1))
66-
"alternating word separators not detected as a word")))
10+
"word-count tests"
11+
12+
(test-equal? "count one word"
13+
(word-count "word")
14+
'#hash(("word" . 1)))
15+
16+
(test-equal? "count one of each word"
17+
(word-count "one of each")
18+
'#hash(("one" . 1) ("of" . 1) ("each" . 1)))
19+
20+
(test-equal? "multiple occurrences of a word"
21+
(word-count "one fish two fish red fish blue fish")
22+
'#hash(("one" . 1) ("fish" . 4) ("two" . 1) ("red" . 1) ("blue" . 1)))
23+
24+
(test-equal? "handles cramped lists"
25+
(word-count "one,two,three")
26+
'#hash(("one" . 1) ("two" . 1) ("three" . 1)))
27+
28+
(test-equal? "handles expanded lists"
29+
(word-count "one,\ntwo,\nthree")
30+
'#hash(("one" . 1) ("two" . 1) ("three" . 1)))
31+
32+
(test-equal? "ignore punctuation"
33+
(word-count "car : carpet as java : javascript!!&@$%^&")
34+
'#hash(("car" . 1) ("carpet" . 1) ("as" . 1) ("java" . 1) ("javascript" . 1)))
35+
36+
(test-equal? "include numbers"
37+
(word-count "testing, 1, 2, testing")
38+
'#hash(("testing" . 2) ("1" . 1) ("2" . 1)))
39+
40+
(test-equal? "normalize case"
41+
(word-count "go Go GO Stop stop")
42+
'#hash(("go" . 3) ("stop" . 2)))
43+
44+
(test-equal? "with apostrophes"
45+
(word-count "'First: don't laugh. Then: don't cry. You're getting it.'")
46+
'#hash(("first" . 1) ("don't" . 2) ("laugh" . 1) ("then" . 1) ("cry" . 1)
47+
("you're" . 1) ("getting" . 1) ("it" . 1)))
48+
49+
(test-equal? "with quotations"
50+
(word-count "Joe can't tell between 'large' and large.")
51+
'#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("large" . 2)
52+
("and" . 1)))
53+
54+
(test-equal? "substrings from the beginning"
55+
(word-count "Joe can't tell between app, apple and a.")
56+
'#hash(("joe" . 1) ("can't" . 1) ("tell" . 1) ("between" . 1) ("app" . 1)
57+
("apple" . 1) ("and" . 1) ("a" . 1)))
58+
59+
(test-equal? "multiple spaces not detected as a word"
60+
(word-count " multiple whitespaces")
61+
'#hash(("multiple" . 1) ("whitespaces" . 1)))
62+
63+
(test-equal? "alternating word separators not detected as a word"
64+
(word-count ",\n,one,\n ,two \n 'three'")
65+
'#hash(("one" . 1) ("two" . 1) ("three" . 1)))
66+
67+
(test-equal? "quotation for word with apostrophe"
68+
(word-count "can, can't, 'can't'")
69+
'#hash(("can" . 1) ("can't" . 2)))))
6770

6871
(run-tests suite))

exercises/practice/word-count/word-count.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
(provide word-count)
44

5-
(define (word-count string)
5+
(define (word-count sentence)
66
(error "Not implemented yet"))

0 commit comments

Comments
 (0)