Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
not the prettiest code i've ever written, but it seems to do the trick.
#!perl -wl use strict; $_ = shift @ARGV || "CLEANSE"; our (%kw, %words); my ($match, @matches); my $future = {"" => ""}; sub sortword { return join("", sort pop=~/./g) } sub dissect { my %h; map $h{$_}++, split(//,pop); return \%h } sub scrub { # make sure a word isn't longer than our # kw or has chars kw doesn't my $w = shift @_; return 0 if length($w) > $kw{"len"}; my $d = dissect($w); map { return 0 if !defined($kw{"d"}->{$_}) || $kw{"d"}->{$_} < $d->{$_} } keys %$d; return 1; } sub process { # run one iteration through %words my ($cand, @match, %future) = @_; for my $w (keys %words) { for my $c (keys %$cand) { my $len = length($cand->{$c}) + $words{$w}; next if $len > $kw{"len"}; my $nk = "$c:$w"; $future{$nk} = sortword($cand->{$c} . $w); if ($len == $kw{"len"}) { push @match, $nk if $future{$nk} eq $kw{"kw"}; delete $future{$nk}; } } } return (\@match, \%future); } %kw = ( "word" => $_ ,"len" => length($_) ,"kw" => sortword($_) ,"d" => dissect($_) ); map { chomp; $words{$_} = length() if scrub($_); } <DATA>; while (keys %$future) { ($match, $future) = process($future); push @matches, @$match; } print "anagrams for " . $kw{"word"} . ":"; map print, @matches; __DATA__ SEE SCALENE CLAN EEL BOB PIZZA SCAN MONKS PERL

perl -e'$_="nwdd\x7F^n\x7Flm{{llql0}qs\x14";s/./chr(ord$&^30)/ge;print'


In reply to Re: OT: How to find anagrams? by pizza_milkshake
in thread OT: How to find anagrams? by Cody Pendant

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found