Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Anagrams/Common-Lisp/anagrams-1.lisp
Normal file
23
Task/Anagrams/Common-Lisp/anagrams-1.lisp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(defun anagrams (&optional (url "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"))
|
||||
(let ((words (drakma:http-request url :want-stream t))
|
||||
(wordsets (make-hash-table :test 'equalp)))
|
||||
;; populate the wordsets and close stream
|
||||
(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 and return the biggest wordsets
|
||||
(loop with maxcount = 0 with maxwordsets = '()
|
||||
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)
|
||||
finally (return (values maxwordsets maxcount)))))
|
||||
3
Task/Anagrams/Common-Lisp/anagrams-2.lisp
Normal file
3
Task/Anagrams/Common-Lisp/anagrams-2.lisp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(multiple-value-bind (wordsets count) (anagrams)
|
||||
(pprint wordsets)
|
||||
(print count))
|
||||
19
Task/Anagrams/Common-Lisp/anagrams-3.lisp
Normal file
19
Task/Anagrams/Common-Lisp/anagrams-3.lisp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(defun read-words (file)
|
||||
(with-open-file (stream file)
|
||||
(loop with w = "" while w collect (setf w (read-line stream nil)))))
|
||||
|
||||
(defun anagram (file)
|
||||
(let ((wordlist (read-words file))
|
||||
(h (make-hash-table :test #'equal))
|
||||
longest)
|
||||
(loop for w in wordlist with ws do
|
||||
(setf ws (sort (copy-seq w) #'char<))
|
||||
(setf (gethash ws h) (cons w (gethash ws h))))
|
||||
(loop for w being the hash-keys in h using (hash-value wl)
|
||||
with max-len = 0 do
|
||||
(let ((l (length wl)))
|
||||
(if (> l max-len) (setf longest nil max-len l))
|
||||
(if (= l max-len) (push wl longest))))
|
||||
longest))
|
||||
|
||||
(format t "~{~{~a ~}~^~%~}" (anagram "unixdict.txt"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue