-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
0f5624a
commit 7df71d2
Showing
5 changed files
with
136 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
124
exercises/practice/word-count/test/word_count_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'"))))) |