Jump to content

Anagrams: Difference between revisions

1,637 bytes added ,  14 years ago
added a CL solution, using DRAKMA to retrieve the wordlist
No edit summary
(added a CL solution, using DRAKMA to retrieve the wordlist)
Line 146:
elan, lane, lean, lena, neal,
evil, levi, live, veil, vile,
 
=={{header|Common Lisp}}==
 
Using [http://weitz.de/drakma/ DRAKMA] to retrieve the wordlist:
 
<lang lisp>(defun anagrams (&optional (url "http://www.puzzlers.org/pub/wordlists/unixdict.txt"))
(let ((words (drakma:http-request url :want-stream t))
(wordsets (make-hash-table :test 'equalp))
(maxcount 0)
(maxwordsets '()))
;; populate the wordsets, close words
(do ((word (read-line words nil nil) (read-line words nil nil)))
((null word) (close words))
(let ((letters (sort (copy-seq word) 'char<)))
(multiple-value-bind (pair presentp)
(gethash letters wordsets)
(if presentp
(setf (car pair) (1+ (car pair))
(cdr pair) (cons word (cdr pair)))
(setf (gethash letters wordsets)
(cons 1 (list word)))))))
;; find the biggest wordset
(loop for pair being each hash-value of wordsets
if (> (car pair) maxcount)
do (setf maxcount (car pair)
maxwordsets (list (cdr pair)))
else if (eql (car pair) maxcount)
do (push (cdr pair) maxwordsets))
;; return the biggest wordset and its size
(values maxwordsets maxcount)))</lang>
 
Evalutating
 
<lang lisp>(multiple-value-bind (wordsets count) (anagrams)
(pprint wordsets)
(print count))</lang>
 
produces the following output.
 
<pre>(("vile" "veil" "live" "levi" "evil")
("regal" "large" "lager" "glare" "alger")
("lange" "glean" "galen" "angle" "angel")
("neal" "lena" "lean" "lane" "elan")
("trace" "crate" "cater" "carte" "caret")
("elba" "bela" "bale" "able" "abel"))
5</pre>
 
== {{header|D}} ==
Line 165 ⟶ 211:
}
</lang>
 
=={{header|E}}==
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.