exercism

Exercism solutions.
Log | Files | Refs

commit 9a210a1bb92699f2c3c592747b78efe610c11438
parent 4e626ce82569870ec779fbd4f6dfc75b45994e1a
Author: dwrz <dwrz@dwrz.net>
Date:   Fri,  8 Feb 2019 01:58:37 +0000

Add elisp/word-count

Diffstat:
Aelisp/word-count/README.md | 19+++++++++++++++++++
Aelisp/word-count/word-count-test.el | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aelisp/word-count/word-count.el | 21+++++++++++++++++++++
3 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/elisp/word-count/README.md b/elisp/word-count/README.md @@ -0,0 +1,19 @@ +# Word Count + +Given a phrase, count the occurrences of each word in that phrase. + +For example for the input `"olly olly in come free"` + +```plain +olly: 2 +in: 1 +come: 1 +free: 1 +``` + +## Source + +This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour. + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/elisp/word-count/word-count-test.el b/elisp/word-count/word-count-test.el @@ -0,0 +1,64 @@ +;;; word-count-test.el --- Tests for word-count (exercism) + +;;; Commentary: + +;;; Code: + +(load-file "word-count.el") + + +(defun equal-assoc (a b) + (equal (sort a #'(lambda (a b) (not (string< (car a) (car b))))) + (sort b #'(lambda (a b) (not (string< (car a) (car b))))))) + + +(ert-deftest no-words-test () + (should (equal-assoc (word-count "") + nil))) + + +(ert-deftest count-one-word-test () + (should (equal-assoc (word-count "word") + '(("word" . 1))))) + + +(ert-deftest count-one-of-each-word-test () + (should (equal-assoc (word-count "one of each") + '(("each" . 1) + ("of" . 1) + ("one" . 1))))) + + +(ert-deftest multiple-occurrences-of-a-word-test () + (should (equal-assoc (word-count "one fish two fish red fish blue fish") + '(("blue" . 1) + ("red" . 1) + ("two" . 1) + ("fish" . 4) + ("one" . 1))))) + + +(ert-deftest ignore-punctuation-test () + (should (equal-assoc (word-count "car : carpet as java : javascript!!&@$%^&") + '(("javascript" . 1) + ("java" . 1) + ("as" . 1) + ("carpet" . 1) + ("car" . 1))))) + + +(ert-deftest include-numbers-test () + (should (equal-assoc (word-count "testing, 1, 2 testing") + '(("2" . 1) + ("1" . 1) + ("testing" . 2))))) + + +(ert-deftest normalize-case-test () + (should (equal-assoc (word-count "go Go GO Stop stop") + '(("stop" . 2) + ("go" . 3))))) + + +(provide 'word-count) +;;; word-count-test.el ends here diff --git a/elisp/word-count/word-count.el b/elisp/word-count/word-count.el @@ -0,0 +1,21 @@ +;;; word-count.el --- word-count Exercise (exercism) + +;;; Commentary: + +;;; Code: + +(defun extract-words (s) +"Extract lowercased words from S. +Splits on whitespace, removes non alphanumeric characters." + (split-string (replace-regexp-in-string "[^[:alnum:]_-]" " " (downcase s)))) + +(defun word-count (s) +"Return an alist word count of words in S." + (let ((word-count ())) + (dolist (w (extract-words s) word-count) + (let ((entry (assoc w word-count))) + (if entry (cl-incf (cdr entry)) + (push (cons w 1) word-count)))))) + +(provide 'word-count) +;;; word-count.el ends here