neingeist
/
add-a-gram
Archived
1
0
Fork 0
master
neingeist 21 years ago
commit a8b7bf2499

173528
WORD.LST

File diff suppressed because it is too large Load Diff

@ -0,0 +1,4 @@
ail
nails
sail
aliens

@ -0,0 +1,52 @@
;; CMUCL: $ time lisp -eval '(compile-file "add-a-gram.lisp")' -eval '(load "add-a-gram.x86f")' -eval '(run)' -eval '(quit)'
;; real 0m11.834s
;; Python: $ time ./addagram.py WORD.LST
;; real 0m43.589s
;; Perl: $ time perl addagram.pl WORD.LST
;; real 0m17.541s
(defvar *wordlist* nil)
(defun run ()
(let ((best-word ""))
(setq *wordlist* (make-hash-table :test 'equal))
(read-file "WORD.LST")
(maphash #'(lambda (sorted-word word)
(if (> (length word) (length best-word))
(if (add-a-gram-p sorted-word)
(setf best-word sorted-word)))) *wordlist*)
(show-result best-word)))
(defun read-file (arg-file-name)
(let ((line nil))
(with-open-file (stream arg-file-name :direction :input)
(loop while (setq line (read-line stream nil)) do
(unless (< (length line) 3)
(setf (gethash (sort-word line) *wordlist*) line))))))
(defun add-a-gram-p (sorted-word)
(let ((foo nil))
(cond ((= (length sorted-word) 3) (gethash sorted-word *wordlist*))
((null (gethash sorted-word *wordlist*)) nil)
(t (setf foo (remove-one-char sorted-word))
(loop until (or (null foo) (add-a-gram-p (car foo)))
do (setf foo (cdr foo)))
(car foo)))))
(defun remove-one-char (word)
(let ((foo nil))
(loop for k from 1 to (length word) do
(push (concatenate 'string
(subseq word 0 (1- k))
(subseq word k (length word))) foo))
foo))
(defun sort-word (word)
(concatenate 'string (sort (coerce word 'list) #'char<)))
(defun show-result (sorted-word)
(cond ((= (length sorted-word) 3)
(format t "~A~%" (gethash sorted-word *wordlist*)) )
(t
(format t "~A~%" (gethash sorted-word *wordlist*))
(show-result (add-a-gram-p sorted-word)))))

@ -0,0 +1,33 @@
#!/usr/bin/perl -w
# Usage: addagram.pl < /net/doc/random-facts/WORD.LST
while(<>) {
chomp;
my $letters = join("", sort split( //, $_ ));
push(@{$got{$letters}}, $_);
}
$maxlen = 0;
foreach $word (sort {length($b) <=> length($a)} keys %got) {
exit if length($word) < $maxlen;
if (&deconstruct($word)) {
for (my $l = $maxlen = length($word); $l>=3; $l--) {
printf("%2d: %s\n", $l, join(',', @{$got{$solution[$l]}}));
}
print "\n";
}
}
sub deconstruct {
my($word) = @_;
return 0 if !$got{$word};
my $len = length($word);
$solution[$len] = $word;
return 1 if $len <= 3;
for (my $l=0; $l<$len; $l++) {
return 1 if &deconstruct(substr($word, 0, $l) . substr($word, $l+1));
}
return 0;
}

@ -0,0 +1,92 @@
#!/usr/bin/python
# [python] phl| ./addagram.py /net/doc/random-facts/WORD.LST
# read dictionary
# best addagram:
# indeterminations
# intermediations
# determinations
# antimodernist
# terminations
# nitrosamine
# antinomies
# antinoise
# sonatine
# enation
# eonian
# inane
# nine
# inn
import sys
import string
args = sys.argv[1:]
if len(args) not in (1, 2):
print "usage: %s <dictfile> [<start-word>]" % sys.argv[0]
sys.exit(1)
dictfile = args.pop(0)
start = args and args.pop(0) or None
startlen = start and len(start) or 3
def sort(l):
l = l[:]; l.sort(); return l
# read dictionary and build tree
dictionary = {}
dict = open(dictfile, "r")
while 1: #{
line = dict.readline()
if not line:
break
word = string.lower(string.strip(line))
wlen = len(word)
chrs = string.join(sort(list(word)), "")
if wlen > startlen or wlen == startlen and (word == start or start == None):
if not dictionary.has_key(wlen):
dictionary[wlen] = {}
dictionary[wlen][chrs] = [word, wlen == startlen and [word, None] or None]
#}
print "read dictionary"
dict.close()
def get_subchrs(chrs):
subchrs = {}
for i in range(len(chrs)):
subchrs[chrs[0:i]+chrs[i+1:]] = 1
return sort(subchrs.keys())
l = startlen
best = None
found = 1
if dictionary.has_key(startlen):
while found and dictionary.has_key(l+1):
l = l + 1
found = 0
for chrs in dictionary[l].keys():
entry = dictionary[l][chrs]
word = entry[0]
subentry = None
for subchrs in get_subchrs(chrs):
subentry = dictionary[l-1].get(subchrs, None)
if subentry and subentry[1]:
break
if subentry and subentry[1]:
entry[1] = [word, subentry[1]]
best = entry
found = 1
if best:
print "best addagram:"
while best[1]:
best = best[1]
print " ", best[0]
else:
print "no addagrams"