Here is your algorithm made more pythonic. Notice the use of default parameters, doc strings, not abbreviated variable names, unix C style capitolization (very subjective, but often the one found in python libs), the avoidance of control variables when possible, the use of ascii_lowercase instead of your own string and the not calling functions main that aren't __main__.
#!/usr/bin/env python import string import random def rand_string(length = 5): """returns a random string of numbers""" return ''.join(random.sample(string.ascii_lowercase,length)) def try_password(match="loner"): """randomly tries 5 letter long passwords until the user finds the correct one""" tries = 0 while True: tries += 1 if rand_string() == match: print 'Password found in ' + str(tries) + ' tries.' return if __name__ == '__main__': try_password() Note: I do not think you're doing the problem the way the author intended. As it wants you to try all combinations, and it's about brute force, not finding it quickly, you most likely should start at "aaaaa" and move to "zzzzz", and cache the results in a list like they ask you too. At the very least, you should be saving these combinations you are generating randomly in a list (as it asks you to) and not testing the password if the generated string was already in the list (as in a real application, that is the time consuming or dangerous operation). --Michael PS: If you wish to assure direct replies reach me On Thu, Jan 8, 2009 at 10:49 AM, Robert Berman <berma...@cfl.rr.com> wrote: > Hi, > > 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." > > My code is as follows: > > #!/usr/bin/env python > > import random > > def GetString(): > alphabet='abcdefghijklmnopqrstuvwxyz' > return ''.join(random.sample(alphabet,5)) > def main(): > password='loner' > errknt = 0 > control = True > while control is True: > if GetString() != password: > errknt +=1 > else: > print 'Password found in ',errknt,' tries.' > control = False > > if __name__ == '__main__': main() > The code does work. I am looking for suggestions to learn more about both > efficiency and optimization of code. > > 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. > > 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 realize my code looks like modified C++ structured code. I am trying to > become more Python concise but I think that is a matter of writing more and > more python code. > > All suggestions, ideas, critiques are most welcome. > > Thank you, > > Robert > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Michael Langford Phone: 404-386-0495 Consulting: http://www.RowdyLabs.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor