<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#3333ff">
<font size="+1">Michael,<br>
<br>
Thank you for your code and your commentary. The code tells me this is
really an ongoing learning process; almost as convoluted as linguistics
and certainly every bit as interesting.<br>
<br>
Your concept of brute force in this example is intriguing. It is as if
I have five cogs spinning, 'a' the slowest and 'e' flying as fast as
its spokes can engage, and every click of every wheel produces a viable
but possibly wrong response. However, each response is unique and in
the nth iteration where password is found, all previous iterations are
unique so there is certainly no need to carry the previous responses in
either a list or a dictionary. They do not duplicate since they cannot
repeat. Other than to include extraneous code, why include repetitive
checking at all?<br>
<br>
In spite of that, your code and your remarks are most appreciated.<br>
<br>
Robert<br>
<br>
</font>Michael Langford wrote:
<blockquote
 cite="mid:82b4f5810901080834j28a668cbq1a1e22bd8d79a7b6@mail.gmail.com"
 type="cite">
  <pre wrap="">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 <a class="moz-txt-link-rfc2396E" href="mailto:bermanrl@cfl.rr.com">&lt;bermanrl@cfl.rr.com&gt;</a> wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">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  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>

    </pre>
  </blockquote>
  <pre wrap=""><!---->


  </pre>
</blockquote>
</body>
</html>