From 8c632811e3857f6050bdfd233757ba59c4835691 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 21 Nov 2021 11:06:58 -0500 Subject: anagram exercise --- exercism/emacs-lisp/anagram/.exercism/config.json | 22 +++++++++ .../emacs-lisp/anagram/.exercism/metadata.json | 1 + exercism/emacs-lisp/anagram/HELP.md | 41 ++++++++++++++++ exercism/emacs-lisp/anagram/README.md | 27 +++++++++++ exercism/emacs-lisp/anagram/anagram-test.el | 56 ++++++++++++++++++++++ exercism/emacs-lisp/anagram/anagram.el | 30 ++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 exercism/emacs-lisp/anagram/.exercism/config.json create mode 100644 exercism/emacs-lisp/anagram/.exercism/metadata.json create mode 100644 exercism/emacs-lisp/anagram/HELP.md create mode 100644 exercism/emacs-lisp/anagram/README.md create mode 100644 exercism/emacs-lisp/anagram/anagram-test.el create mode 100644 exercism/emacs-lisp/anagram/anagram.el diff --git a/exercism/emacs-lisp/anagram/.exercism/config.json b/exercism/emacs-lisp/anagram/.exercism/config.json new file mode 100644 index 0000000..72e38a3 --- /dev/null +++ b/exercism/emacs-lisp/anagram/.exercism/config.json @@ -0,0 +1,22 @@ +{ + "blurb": "Given a word and a list of possible anagrams, select the correct sublist.", + "authors": [ + "canweriotnow" + ], + "contributors": [ + "vermiculus" + ], + "files": { + "solution": [ + "anagram.el" + ], + "test": [ + "anagram-test.el" + ], + "example": [ + ".meta/example.el" + ] + }, + "source": "Inspired by the Extreme Startup game", + "source_url": "https://github.com/rchatley/extreme_startup" +} diff --git a/exercism/emacs-lisp/anagram/.exercism/metadata.json b/exercism/emacs-lisp/anagram/.exercism/metadata.json new file mode 100644 index 0000000..a36b7a7 --- /dev/null +++ b/exercism/emacs-lisp/anagram/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"emacs-lisp","exercise":"anagram","id":"656e40c2881446418e9a72a2c421c891","url":"https://exercism.org/tracks/emacs-lisp/exercises/anagram","handle":"orbekk","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/exercism/emacs-lisp/anagram/HELP.md b/exercism/emacs-lisp/anagram/HELP.md new file mode 100644 index 0000000..ab454af --- /dev/null +++ b/exercism/emacs-lisp/anagram/HELP.md @@ -0,0 +1,41 @@ +# Help + +## Running the tests + +Tests can be run several ways: + +1. Interactively and individually, with `M-x ert RET test-name RET` +2. Interactively and all at once, with `M-x ert RET t RET` +3. From the terminal, in batch mode, with `emacs -batch -l ert -l my-test.el -f ert-run-tests-batch-and-exit` +4. Other options can be found in the docs, `C-h i m ert RET` + +## Submitting your solution + +You can submit your solution using the `exercism submit anagram.el` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Emacs Lisp track's documentation](https://exercism.org/docs/tracks/emacs-lisp) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [The Emacs Wiki](http://emacswiki.org/) is invaluable. Spend lots of time here. +- [The Emacs Editor](http://www.gnu.org/software/emacs/manual/html_node/emacs/index.html) is the official manual for GNU Emacs. +- IRC - there are [freenode](https://freenode.net/) channels for `#emacs`, `#prelude`, and many Emacs + packages, and many helpful folks around. And with emacs, IRC is as close as + `M-x erc`. +- [Exercism Support](https://gitter.im/exercism/support) Gitter chat is also a good place to get help from the + exercism community. +- [StackOverflow](http://stackoverflow.com/questions/tagged/elisp) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. \ No newline at end of file diff --git a/exercism/emacs-lisp/anagram/README.md b/exercism/emacs-lisp/anagram/README.md new file mode 100644 index 0000000..97a53a6 --- /dev/null +++ b/exercism/emacs-lisp/anagram/README.md @@ -0,0 +1,27 @@ +# Anagram + +Welcome to Anagram on Exercism's Emacs Lisp Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +An anagram is a rearrangement of letters to form a new word. +Given a word and a list of candidates, select the sublist of anagrams of the given word. + +Given `"listen"` and a list of candidates like `"enlists" "google" +"inlets" "banana"` the program should return a list containing +`"inlets"`. + +## Source + +### Created by + +- @canweriotnow + +### Contributed to by + +- @vermiculus + +### Based on + +Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup \ No newline at end of file diff --git a/exercism/emacs-lisp/anagram/anagram-test.el b/exercism/emacs-lisp/anagram/anagram-test.el new file mode 100644 index 0000000..9900d43 --- /dev/null +++ b/exercism/emacs-lisp/anagram/anagram-test.el @@ -0,0 +1,56 @@ +;;; 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"))))) +(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 diff --git a/exercism/emacs-lisp/anagram/anagram.el b/exercism/emacs-lisp/anagram/anagram.el new file mode 100644 index 0000000..a2c0bc0 --- /dev/null +++ b/exercism/emacs-lisp/anagram/anagram.el @@ -0,0 +1,30 @@ +;;; anagram.el --- Anagram (exercism) + +;;; Commentary: + +;;; Code: + +(require 'cl-lib) + +(defun anagram-key (text) + (let ((text-list (split-string (downcase text) ""))) + (apply 'concat (sort text-list 'string<)))) + +(defun anagram-make-table (words) + (let* ((table (make-hash-table :test 'equal)) + (add-word + (lambda (w) (let* ((key (anagram-key w)) + (entry (nconc (gethash key table '()) (list w)))) + (puthash (anagram-key w) entry table))))) + (progn (mapc add-word words) + table))) + +(defun anagrams-for (word candidates) + (let ((table (anagram-make-table candidates)) + (key (anagram-key word)) + (filter-results (lambda (words) + (seq-filter (lambda (w) (not (equal word w))) words)))) + (funcall filter-results (gethash key table '())))) + +(provide 'anagram) +;;; anagram.el ends here -- cgit v1.2.3