[Israel.pm] BruteForcing function
Gaal Yahas
gaal at forum2.org
Mon Dec 20 08:02:33 PST 2004
On Mon, Dec 20, 2004 at 05:17:37PM +0200, amit sides wrote:
> Gaal Yahas wrote:
>
> > @alphabet = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9', qw/!_-/ );
> > for (0 .. $#alphabet) {
> > print $alphabet[$_];
> > }
>
> i thought about it.....so we solve the first problem ...now i need to
> solve the sec one
> i dont see how this helps me show all the possible option of combinations
> do you have an idea how to print @alphabet chars by loops ? how to
> arrange the loops ?
You haven't defined your problem, then. The above generates all possible
single-letter "words" from the alphabet. If you want to generate all
"words" with *more* letters, you simply want to enumerate over a larger
numerical range, in base n (where n is the size of the alphabet).
Think of it this way: you have a combination lock with three rolling
digits, that is, it offers numbers like [1][2][3] and [0][0][7]. To try
all combinations you simply need to try all numbers from 0 to 999. But
if I go and replace the *label* on each roller to say [A] to [I] instead
[0] to [9], your problem has remained essentially the same; all you need
is a presentation function to translate the output from (say) [2][0][1]
to [C][A][B].
Now your real rollers are bigger. Instead of 0-9 they accept that range
you defined earlier, which has 65 "digits". So to generate all
combinations of three letter "words", you have 65*65*65 (65^3) numbers
to visit, but what you do with each one is still the same: thinking of
it as a base-65 number, translate it to your alphabet.
foreach my $num (0 .. (@alphabet ** $width - 1)) {
print base_n(\@alphabet, $num);
}
You still have to write base_n, but if you Google for "base conversions
perl" you'll get plenty of ideas on how to do that.
--
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/
More information about the Perl
mailing list