Running on an ancient PII 266 this takes less than a minute to process the standard unix dictionary. It uses a fairly efficient algorithm. Basically we bulid a hash which has the sorted letters of the words as its keys and the corresponding words (as an array ref) as its values. Thus we have a hash like:

... 'aet' => [ ate eat ] ...

As we have represented all the words as their sorted letters in a hash we make the search quick and easy. We work from the longest keys to the shortest and use recursion to test all the possibilities. We keep a list of 'winners' - any word which is in a winning list need not be tested again when we descend to it's level.

#!usr/bin/perl -w use strict; # get words into an array and a hash keyed on sorted letters open DICT, "c:/windows/desktop/unixdict.txt" or die "Oops $!\n"; my (%words,%winners); while(<DICT>) { chomp; my $sort = join '', (sort split'',$_); push @{$words{$sort}}, $_; } # test the words by length - longest first, don't repeat known winners for my $word (sort {length $b <=> length $a} keys %words) { defined $winners{$word} ? next : test($word); } sub test { my $word = $_[-1]; if (length $word <=3) { winner(@_); return; } my @letters = sort split '', $word; for my $i (0..$#letters) { my @temp = @letters; $temp[$i] = ''; my $short = join '', @temp; test(@_,$short) if exists $words{$short}; } } sub winner { local $, = " "; for (@_) { $winners{$_}++; print @{$words{$_}}, "\n"; } print "\n"; } __DATA__ planetarium manipulate aluminate laminate matinal animal manila milan main min [blah]

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Add-A-Gram Performance by tachyon
in thread Add-A-Gram Performance by smgfc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.