Problem with algorithm

Paul McGuire ptmcg at austin.rr.com
Fri Apr 13 11:41:31 EDT 2007


On Apr 13, 10:22 am, Michael Bentley <mich... at jedimindworks.com>
wrote:
> On Apr 13, 2007, at 9:19 AM, Paul McGuire wrote:
>
> > If you just expand the length to five million* or so, one of those
> > strings will contain all the works of Shakespeare.
>
> Not likely, even with a tiny sampling of the works of Shakespeare:
>
> # :-)
>
> import string
> import random
>
> def main(bardText, maxTries=5000000):
>      tries = 0
>      while tries < maxTries:
>          tries += 1
>          attempt = []
>          for letter in bardText.lower():
>              if random.choice(
>                  string.lowercase[:26]
>                  + string.punctuation
>                  + ' '
>                  ) == letter:
>                  attempt.append(letter)
>              else:
>                  break
>          if len(attempt) >= 4:
>              print '%d: %s' % (
>                  tries,
>                  ''.join(attempt)
>              )
>
> if __name__ == "__main__":
>      main("Alas, poor Yorick!")

5000000 << infinity

Keep tryin'!

Also, the OP's technique was not doing random string permutations, but
generating an exhaustive list of all possible sequences from aaa... to
zzz... .  So I think the works of Shakespeare are *bound* to be in
there somewhere.

For proof, here's an extract from my sample code from running this
exhaustive program with length=14:

...
ALASPOORYORICG
ALASPOORYORICH
ALASPOORYORICI
ALASPOORYORICJ
ALASPOORYORICK
ALASPOORYORICL
ALASPOORYORICM
ALASPOORYORICN
ALASPOORYORICO
...

-- Paul
:) (too late for April 1, unfortunately)




More information about the Python-list mailing list