[Tutor] Suggestions for more efficient and optimized coding technique,
kent37 at tds.net
Thu Jan 8 18:28:20 CET 2009
On Thu, Jan 8, 2009 at 10:49 AM, Robert Berman <bermanrl at cfl.rr.com> wrote:
> One of the challenges on the challenge you web page appropriately titled
> 'Brute force' reads as follows:
> "The password you have to guess is 'loner' . Try all combinations of
> lowercase letters until you guess it. Try not to loop much for example,
> save all used combinations in an array so you don't repeat."
This is a strange requirement. If you want to try all combinations of
lowercase letters, the simplest way to do that is with nested loops.
The loops will generate all combinations without repeating, so there
is no need to save the used combinations.
> Since the challenge revolves around the use of randomized retrieval, I'm not
> too sure how to optimize the process. The authors concept of using arrays
> seem a bit superfluous as I think it takes longer to add an item to a
> dictionary and retrieve an item from a dictionary than it does to do an if
> compare of two 5 character strings. So, I left that code out of the program
> entirely. If that was wrong, or there is a better way to avoid duplication,
> please point me in the right direction.
To avoid duplication you should use a set to hold the passwords
already tried. It is probably faster to just compare, but if you are
supposed to imagine a true brute-force password attack, the set test
would be faster than a failed login.
> I think, perhaps, I could make it a tad more efficient if I changed
> 'alphabet' from a string to a list as I remember reading that lists are
> significantly faster to manipulate than are strings. Is that true and is it
> a viable change.
I don't think it will make any difference. The place where lists are
preferred is when you are concatenating strings in a loop.
More information about the Tutor