Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Jumble solver

by Plankton (Vicar)
on Dec 18, 2003 at 17:50 UTC ( [id://315582]=CUFP: print w/replies, xml ) Need Help??

Things are slow here today. Even for the Chum-Bucket!

So here's a Perl script and a shell script that solves jumbles.
#!/usr/bin/perl -wT $ENV{'PATH'} = ""; use strict; my $word = defined($_=shift) ? $_ : die "usage: $0 <word>\n"; chomp($word); ($word) = $word =~ /^(\w+)$/ or die 'Bad User!'; my @chars = split //, $word; my $speller ="spelled"; sub spelled { my $word = shift; my $out=`$speller $word`; chomp($out); if ( length($out) > 0 ) { print "[$out]\n"; } } sub swap { my $i = shift; my $j = shift; my $A = shift; # ref to array my $tmp = @{$A}[$i]; @{$A}[$i] = @{$A}[$j]; @{$A}[$j] = $tmp; } sub permute { my $i = shift; my $n = shift; my $T = shift; #ref to array if ( $i == $n ) { my $word = join( "", @{$T} ); spelled( $word ); } else { for my $j ( $i..$n) { swap( $i-1, $j-1, $T ); permute( $i+1, $n, $T ); swap( $i-1, $j-1, $T ); } } } permute (1,$#chars+1,\@chars);
The above script use this shell script.
$ cat spelled #!/bin/sh ASPELL=/usr/bin/aspell GREP=/usr/bin/grep EGREP=/usr/bin/egrep for i in $* do correct=`echo $i | $ASPELL -a | $GREP -v '^@' | $GREP -v '^&' +| $EGREP -v '^$'` if [ ! -z "$correct" ] then echo "$i" fi done
Here's some sample output:
$ jumble.pl cat [cat] [act]

Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re: Jumble solver
by gmax (Abbot) on Dec 18, 2003 at 19:27 UTC

    An alternate method, without external programs, using a method similar to the one seen in Perl's pearls:

    #!/usr/bin/perl -w use strict; use DB_File; my $search_word = shift or die "word required"; my %sorted; tie %sorted, 'DB_File', "words_jumble.db"; unless (keys %sorted) { # Creates the word index. open WORDS, "/usr/share/dict/words" # your favorite dictionary h +ere or die "can't open words file\n"; while (<WORDS>) { chomp; my $word = pack "C*", sort unpack "C*", lc $_; $sorted{$word} .= "," if $sorted{$word}; $sorted{$word} .= $_; } close WORDS; } my $sorted_word = pack "C*", sort unpack "C*", lc $search_word; die "$search_word not found\n" unless exists $sorted{$sorted_word}; print "[$_]\n" for split",", $sorted{$sorted_word}; __END__ perl jumble.pl elvis [Elvis] [evils] [Levis] [lives] [veils] perl jumble.pl parse [pares] [parse] [pears] [rapes] [reaps] [spare] [spear] perl jumble.pl steak [Keats] [skate] [stake] [steak] [takes]
     _  _ _  _  
    (_|| | |(_|><
     _|   
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://315582]
Approved by delirium
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found