Skip to content

Commit

Permalink
Add solver algo
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-hull committed Mar 20, 2016
1 parent d5992d8 commit ab2f6fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
A multi-word anagram solver in Clojure, based on the article
**"Anagrams and Pangrams"** from _The Armchair Universe_, by A.K. Dewdney.

## Web Service

TODO

## API Usage

### Pre-requisites

You will need [Leiningen](https://github.com/technomancy/leiningen) 2.6.1 or above installed.
Expand All @@ -16,6 +22,19 @@ To build and install the library locally, run:
$ cd ars-magna
$ lein test

### Example usage

```clojure
(use 'ars-magna.dict)
(use 'ars-magna.solver)

(let [dict (load-word-list :en-GB)
index (partition-by-word-length dict)]
(sort
(search index "compute" 3 nil)))
; ("come put" "compute" "cote ump" "cut mope" "cut poem"
; "cute mop" "met coup" "mote cup" "mute cop" "tome cup")
```

## References

Expand Down
16 changes: 16 additions & 0 deletions src/ars_magna/solver.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns ars-magna.solver
(:require
[clojure.string :as s]
[ars-magna.dict :refer :all]))

(defn search [index word min-size prefix]
(if (empty? word)
(s/trim prefix)
(flatten
(for [w (find-in index word min-size :en-GB)]
(search
index
(remaining-chars word w)
min-size
(str w " " prefix))))))

12 changes: 12 additions & 0 deletions test/ars_magna/solver_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns ars-magna.solver-test
(:require
[clojure.test :refer :all]
[ars-magna.dict :refer :all]
[ars-magna.solver :refer :all]))

(deftest check-solver
(let [dict (load-word-list :en-GB)
index (partition-by-word-length dict)]
(is (= (sort (search index "compute" 3 nil))
["come put" "compute" "cote ump" "cut mope" "cut poem"
"cute mop" "met coup" "mote cup" "mute cop" "tome cup"]))))

0 comments on commit ab2f6fc

Please sign in to comment.