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. |