After reading ``This turned out not to be the disaster I expected'', I got to thinking. Instead of generating all the strings of a given length and evaling them, could I generate a list of all the valid perl of a given length?
I do, of course, want to include punctuation and numbers (perl isn't all that exciting without them, mind-bending japhs aside), so the magic string autoincrement won't work for me. The code I ended up with is below.
#!/usr/bin/perl -w use strict; # redirect stderr to /dev/null when run, if you'd # like output of any use. my $chars= "abcdefghijklmnopqrstuvwxyz0123456789 `~!@#\$%^&*()-_=+[{]}\\|;:'\",<. +>/?"; my $f = substr($chars, 0, 1); my $l = substr($chars, -1, 1); my $s = ""; while(length($s) < 5) { my @tmp; my $x; if (system("perl", "-ce", $s) == 0) { print $s . "\n"; } if ($s eq "") { $s = $f; next; } elsif ($s eq ($l x length($s))) { $s = $f x (length($s) + 1); next; } @tmp = split(//, $s); $x = @tmp - 1; while (1) { # loop through the string, starting at the end if ($tmp[$x] eq $l) { # Is it the last char? $tmp[$x] = $f; # If so, make it the first $x--; # And move on to the next one } else { # If not, just increment it. $tmp[$x] = substr($chars, index($chars, $tmp[$x]) + 1, 1); last; } } $s = join('', @tmp); }
There's a big problem with this code, though. It's slow. Like, really slow. I think most of the slowdown comes from having to spawn perl every iteration of the loop[0]. I'm confident there's a better way to do the syntax checking, but I don't know what it is. So, due to that limitation the results aren't very exciting.
I think a more intelligent algorithm, something that's aware of perl's syntax, could be used to get better results faster. At the very least, random generation would be more interesting on the longer strings (if the length is 8, every string length 7 needs to be generated before the front character changes...)
-- [0] The rest is from my poor code :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Brute force perl
by jdporter (Paladin) on Jun 12, 2007 at 15:56 UTC | |
by lima1 (Curate) on Jun 12, 2007 at 16:17 UTC | |
by jdporter (Paladin) on Jun 12, 2007 at 16:27 UTC | |
by bart (Canon) on Jun 13, 2007 at 10:49 UTC | |
by jdporter (Paladin) on Jun 13, 2007 at 18:22 UTC | |
by lima1 (Curate) on Jun 12, 2007 at 18:49 UTC | |
by jagh (Monk) on Jun 12, 2007 at 16:22 UTC | |
by jdporter (Paladin) on Jun 12, 2007 at 16:25 UTC | |
by jagh (Monk) on Jun 12, 2007 at 16:49 UTC | |
by jdporter (Paladin) on Jun 12, 2007 at 19:15 UTC | |
|
Re: Brute force perl
by toma (Vicar) on Jun 12, 2007 at 17:16 UTC | |
by jdporter (Paladin) on Jun 12, 2007 at 20:18 UTC | |
|
GA perl (was: Brute force perl)
by jagh (Monk) on Jun 12, 2007 at 17:42 UTC | |
by jdporter (Paladin) on Jun 12, 2007 at 19:50 UTC | |
|
Re: Brute force perl (ex)
by tye (Sage) on Jun 12, 2007 at 19:55 UTC | |
|
Re: Brute force perl
by duelafn (Parson) on Jun 12, 2007 at 19:17 UTC |