|Think about Loose Coupling|
Re^4: secret code generatorby tilly (Archbishop)
|on Dec 21, 2006 at 03:58 UTC||Need Help??|
Are you finished the self-congratulation?
The fact is that if I did not know what your index-based code was supposed to be doing, I would have had a dickens of a time figuring out what that code does. Perhaps in your coding style names like $di and @is make sense to you, but they are meaningless to me. And presumably would be meaningless to any other maintainance programmer.
Now about my first attempt. How it works doesn't matter. That it does, does. The key piece is
which means for every length of string, print all combinations. How does that work? Well it depends on nested_for, which should be pushed somewhere else and documented. In fact it is useful enough that I'd want to see something like that on CPAN. Lo and behold, it has been put on CPAN! See Algorithm::Loops' NestedLoops function. (Thanks, tye.) I avoided using the module largely because I wanted to give the complete solution rather than hiding any bits.
Why did I pick that solution? Well I knew I had the code for that lying around, easy to pick up, so it was very fast to code. And secondly the Python programmer was obviously trying to snow the (admittedly novice) Perl programmer with techniques the Perl guy didn't know, so I was coming back with techniques the Python guy probably wouldn't know. In other words I was being cute. And was definitely not trying to be maintainable. (Though once the complicated bit is pushed to a CPAN module, it actually is maintainable as well.)
Now the solution that you're deriding did have a bug. I just fixed that. Guess I should have tested it first, but I was just trying to use even fewer features than you did. (And the reason why I did that is that you were criticizing me for using so many features.) As for how it works, it is the mirror image of yours, but rather than keep an array and then move an index, I'm just keeping a partial array of what is left to go through. It is slower because I'm constantly creating and destroying arrays of scalars while you aren't.
About performance, that was obviously not a goal of mine. But even so, if every microsecond it takes to generate each answer adds 177 years on the overall task, then neither of our solutions are going to get through all of the 8 character passwords any time soon.