Anagrams: Difference between revisions

Content deleted Content added
added a CL solution, using DRAKMA to retrieve the wordlist
cleanup in CL
Line 153: Line 153:
<lang lisp>(defun anagrams (&optional (url "http://www.puzzlers.org/pub/wordlists/unixdict.txt"))
<lang lisp>(defun anagrams (&optional (url "http://www.puzzlers.org/pub/wordlists/unixdict.txt"))
(let ((words (drakma:http-request url :want-stream t))
(let ((words (drakma:http-request url :want-stream t))
(wordsets (make-hash-table :test 'equalp))
(wordsets (make-hash-table :test 'equalp)))
;; populate the wordsets and close stream
(maxcount 0)
(maxwordsets '()))
;; populate the wordsets, close words
(do ((word (read-line words nil nil) (read-line words nil nil)))
(do ((word (read-line words nil nil) (read-line words nil nil)))
((null word) (close words))
((null word) (close words))
Line 167: Line 165:
(setf (gethash letters wordsets)
(setf (gethash letters wordsets)
(cons 1 (list word)))))))
(cons 1 (list word)))))))
;; find the biggest wordset
;; find and return the biggest wordsets
(loop for pair being each hash-value of wordsets
(loop with maxcount = 0 with maxwordsets = '()
for pair being each hash-value of wordsets
if (> (car pair) maxcount)
if (> (car pair) maxcount)
do (setf maxcount (car pair)
do (setf maxcount (car pair)
maxwordsets (list (cdr pair)))
maxwordsets (list (cdr pair)))
else if (eql (car pair) maxcount)
else if (eql (car pair) maxcount)
do (push (cdr pair) maxwordsets))
do (push (cdr pair) maxwordsets)
finally (return (values maxwordsets maxcount)))))</lang>
;; return the biggest wordset and its size
(values maxwordsets maxcount)))</lang>


Evalutating
Evalutating