Add elisp/anagram
Work-in-progress.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# Anagram
|
||||
|
||||
Given a word and a list of possible anagrams, select the correct sublist.
|
||||
|
||||
Given `"listen"` and a list of candidates like `"enlists" "google"
|
||||
"inlets" "banana"` the program should return a list containing
|
||||
`"inlets"`.
|
||||
## Source
|
||||
|
||||
Inspired by the Extreme Startup game [https://github.com/rchatley/extreme_startup](https://github.com/rchatley/extreme_startup)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
@@ -0,0 +1,53 @@
|
||||
;;; anagram-test.el --- Tests for Anagram (exercism)
|
||||
;;; Commentary:
|
||||
|
||||
;;; Code:
|
||||
|
||||
(load-file "anagram.el")
|
||||
|
||||
(ert-deftest no-matches ()
|
||||
(should (equal '() (anagrams-for
|
||||
"diaper"
|
||||
'("hello" "world" "zombies" "pants")))))
|
||||
|
||||
(ert-deftest detect-simple-anagram ()
|
||||
(should (equal '("tan") (anagrams-for
|
||||
"ant"
|
||||
'("tan" "stand" "at")))))
|
||||
|
||||
(ert-deftest does-not-confuse-different-duplicates ()
|
||||
(should (equal '() (anagrams-for
|
||||
"galea"
|
||||
'("eagle")))))
|
||||
|
||||
(ert-deftest eliminate-anagram-subsets ()
|
||||
(should (equal '() (anagrams-for
|
||||
"good"
|
||||
'("dog" "goody")))))
|
||||
|
||||
(ert-deftest detect-anagram ()
|
||||
(should (equal '("inlets") (anagrams-for
|
||||
"listen"
|
||||
'("enlists" "google" "inlets" "banana")))))
|
||||
|
||||
(ert-deftest multiple-anagrams ()
|
||||
(should (equal '("gallery" "regally" "largely")
|
||||
(anagrams-for
|
||||
"allergy"
|
||||
'("gallery" "ballerina" "regally" "clergy" "largely" "leading")))))
|
||||
|
||||
(ert-deftest case-insensitive-anagrams ()
|
||||
(should (equal '("Carthorse")
|
||||
(anagrams-for
|
||||
"Orchestra"
|
||||
'("cashregister" "Carthorse" "radishes")))))
|
||||
|
||||
(ert-deftest word-is-not-own-anagram ()
|
||||
(should (equal '()
|
||||
(anagrams-for
|
||||
"banana"
|
||||
'("banana")))))
|
||||
|
||||
|
||||
(provide 'anagram-test)
|
||||
;;; anagram-test.el ends here
|
||||
@@ -0,0 +1,44 @@
|
||||
;;; anagram.el --- Anagram (exercism)
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;;; Code:
|
||||
|
||||
(require 'cl)
|
||||
|
||||
(provide 'anagram)
|
||||
|
||||
(defun make-char-count(s)
|
||||
(let ((char-list (mapcar (lambda (char) (string char)) s))
|
||||
(char-counts ()))
|
||||
;; Build an associative list from the character list.
|
||||
(dolist (char char-list char-counts)
|
||||
(if (assoc char char-counts)
|
||||
(setcdr (assoc char char-counts)
|
||||
(1+ (cdr (assoc char char-counts))))
|
||||
(setq char-counts (cons (cons char 1) char-counts))))))
|
||||
|
||||
(defun compare-char-counts(char-counts comparison-char-counts)
|
||||
(let ((are-equal t))
|
||||
(dolist (char-count char-counts are-equal)
|
||||
(if (not are-equal) nil)
|
||||
(if (assoc (car char-count) comparison-char-counts)
|
||||
(if (not (equal
|
||||
(cdr char-count)
|
||||
(cdr (assoc (car char-count) comparison-char-counts))))
|
||||
(setq are-equal nil))
|
||||
(setq are-equal nil)))))
|
||||
|
||||
(defun anagrams-for(word anagrams)
|
||||
(let ((candidates ())
|
||||
(word-char-count (make-char-count word)))
|
||||
(dolist (candidate anagrams candidates)
|
||||
(if (and
|
||||
(not (string-equal word candidate))
|
||||
(equal (length word) (length candidate))
|
||||
(compare-char-counts word-char-count (make-char-count candidate)))
|
||||
(if (> (length candidates) 0)
|
||||
(add-to-list 'candidates candidate t)
|
||||
(setq candidates (cons candidate nil)))))))
|
||||
|
||||
;;; anagram.el ends here
|
||||
Reference in New Issue
Block a user