in reply to scrambling all letters in a word until all combinations are found
A very simple linear matching algorithm. Something like the following:
I sort of slapped this together on the spot, so don't expect it to be pretty. It should work, however.use strict; use warnings; my $letters = 'balphe_'; my (%lhash, @solutions, $blanks, $bcopy, $l); $blanks++ while $letters =~ s/[^a-z]//; $lhash{$_}++ for split //, $letters; while (<DATA>) { $bcopy = $blanks; my %whash; chomp; $whash{$_}++ for split //; for $l (keys %whash) { no warnings; last if $lhash{$l} < $whash{$l} && ($bcopy -= $whash{$l} - $lhash{$l}) < 0; } push @solutions, $_ if $bcopy > -1; } print join "\n", sort {length($b) <=> length($a) || $a cmp $b} @solutions; __DATA__ alpha beta gamma delta epsilon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: scrambling all letters in a word until all combinations are found
by Anonymous Monk on Jan 23, 2006 at 22:26 UTC | |
|
Re^2: scrambling all letters in a word until all combinations are found
by Anonymous Monk on Jan 26, 2006 at 05:46 UTC |