Skip to content

Commit

Permalink
word-count: Sync tests (#702)
Browse files Browse the repository at this point in the history
* update starter file

* sync tests

* clean up example.clj

* implement tests

* add an example solution that passes the tests

* update config

* delete reimplemented test
  • Loading branch information
tasxatzial authored Jan 5, 2025
1 parent 0f5624a commit 7df71d2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 45 deletions.
3 changes: 2 additions & 1 deletion exercises/practice/word-count/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"sjwarner-bp",
"tejasbubane",
"walkermatt",
"yurrriq"
"yurrriq",
"tasxatzial"
],
"files": {
"solution": [
Expand Down
25 changes: 6 additions & 19 deletions exercises/practice/word-count/.meta/src/example.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
(ns word-count
(:require [clojure.string :refer [lower-case split]]))
(ns word-count)

(defn word-count
"return a hash of unique words and how many times they appeared in the input string"
[input]
(->> (split input #"\W+")
(map lower-case)
(group-by identity)
(reduce (fn [acc [word occurrences]]
(assoc acc word (count occurrences))) {})))

;; Another approach
(ns phrase
(:require [clojure.string :refer [lower-case]]))

(defn words [s]
(re-seq #"\w+" s))

(defn word-count [s]
(-> s lower-case words frequencies))
[phrase]
(->> phrase
clojure.string/lower-case
(re-seq #"\b\w+'?\w*\b")
frequencies))
21 changes: 18 additions & 3 deletions exercises/practice/word-count/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[61559d5f-2cad-48fb-af53-d3973a9ee9ef]
description = "count one word"
Expand Down Expand Up @@ -28,6 +35,11 @@ description = "normalize case"

[4185a902-bdb0-4074-864c-f416e42a0f19]
description = "with apostrophes"
include = false

[4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3]
description = "with apostrophes"
reimplements = "4185a902-bdb0-4074-864c-f416e42a0f19"

[be72af2b-8afe-4337-b151-b297202e4a7b]
description = "with quotations"
Expand All @@ -40,3 +52,6 @@ description = "multiple spaces not detected as a word"

[50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360]
description = "alternating word separators not detected as a word"

[6d00f1db-901c-4bec-9829-d20eb3044557]
description = "quotation for word with apostrophe"
8 changes: 5 additions & 3 deletions exercises/practice/word-count/src/word_count.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns word-count)

(defn word-count [s] ;; <- arglist goes here
;; your code goes here
)
(defn word-count
"Counts how many times each word occurs in the given string"
[s]
;; function body
)
124 changes: 105 additions & 19 deletions exercises/practice/word-count/test/word_count_test.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,113 @@
(ns word-count-test
(:require [clojure.test :refer [deftest is]]
(:require [clojure.test :refer [deftest testing is]]
word-count))

(deftest count-one-word
(is (= {"word" 1}
(word-count/word-count "word"))))
(deftest test-61559d5f-2cad-48fb-af53-d3973a9ee9ef
(testing "count one word"
(is (= {"word" 1}
(word-count/word-count "word")))))

(deftest count-one-of-each
(is (= {"one" 1 "of" 1 "each" 1}
(word-count/word-count "one of each"))))
(deftest test-5abd53a3-1aed-43a4-a15a-29f88c09cbbd
(testing "count one of each word"
(is (= {"one" 1
"of" 1
"each" 1}
(word-count/word-count "one of each")))))

(deftest count-multiple-occurrences
(is (= {"one" 1 "fish" 4 "two" 1 "red" 1 "blue" 1}
(word-count/word-count "one fish two fish red fish blue fish"))))
(deftest test-2a3091e5-952e-4099-9fac-8f85d9655c0e
(testing "multiple occurrences of a word"
(is (= {"one" 1
"fish" 4
"two" 1
"red" 1
"blue" 1}
(word-count/word-count "one fish two fish red fish blue fish")))))

(deftest ignore-punctuation
(is (= {"car" 1, "carpet" 1 "as" 1 "java" 1 "javascript" 1}
(word-count/word-count "car : carpet as java : javascript!!&@$%^&"))))
(deftest test-e81877ae-d4da-4af4-931c-d923cd621ca6
(testing "handles cramped lists"
(is (= {"one" 1
"two" 1
"three" 1}
(word-count/word-count "one,two,three")))))

(deftest include-numbers
(is (= {"testing" 2 "1" 1 "2" 1}
(word-count/word-count "testing, 1, 2 testing"))))
(deftest test-7349f682-9707-47c0-a9af-be56e1e7ff30
(testing "handles expanded lists"
(is (= {"one" 1
"two" 1
"three" 1}
(word-count/word-count "one,\ntwo,\nthree")))))

(deftest normalize-case
(is (= {"go" 3}
(word-count/word-count "go Go GO"))))
(deftest test-a514a0f2-8589-4279-8892-887f76a14c82
(testing "ignore punctuation"
(is (= {"car" 1
"carpet" 1
"as" 1
"java" 1
"javascript" 1}
(word-count/word-count "car: carpet as java: javascript!!&@$%^&")))))

(deftest test-d2e5cee6-d2ec-497b-bdc9-3ebe092ce55e
(testing "include numbers"
(is (= {"testing" 2
"1" 1
"2" 1}
(word-count/word-count "testing, 1, 2 testing")))))

(deftest test-dac6bc6a-21ae-4954-945d-d7f716392dbf
(testing "normalize case"
(is (= {"go" 3
"stop" 2}
(word-count/word-count "go Go GO Stop stop")))))

(deftest test-4ff6c7d7-fcfc-43ef-b8e7-34ff1837a2d3
(testing "with apostrophes"
(is (= {"first" 1
"don't" 2
"laugh" 1
"then" 1
"cry" 1
"you're" 1
"getting" 1
"it" 1}
(word-count/word-count "'First: don't laugh. Then: don't cry. You're getting it.'")))))

(deftest test-be72af2b-8afe-4337-b151-b297202e4a7b
(testing "with quotations"
(is (= {"joe" 1
"can't" 1
"tell" 1
"between" 1
"large" 2
"and" 1}
(word-count/word-count "Joe can't tell between 'large' and large.")))))

(deftest test-8d6815fe-8a51-4a65-96f9-2fb3f6dc6ed6
(testing "substrings from the beginning"
(is (= {"joe" 1
"can't" 1
"tell" 1
"between" 1
"app" 1
"apple" 1
"and" 1
"a" 1}
(word-count/word-count "Joe can't tell between app, apple and a.")))))

(deftest test-c5f4ef26-f3f7-4725-b314-855c04fb4c13
(testing "multiple spaces not detected as a word"
(is (= {"multiple" 1
"whitespaces" 1}
(word-count/word-count " multiple whitespaces")))))

(deftest test-50176e8a-fe8e-4f4c-b6b6-aa9cf8f20360
(testing "alternating word separators not detected as a word"
(is (= {"one" 1
"two" 1
"three" 1}
(word-count/word-count ",\n,one,\n ,two \n 'three'")))))

(deftest test-6d00f1db-901c-4bec-9829-d20eb3044557
(testing "quotation for word with apostrophe"
(is (= {"can" 1
"can't" 2}
(word-count/word-count "can, can't, 'can't'")))))

0 comments on commit 7df71d2

Please sign in to comment.