Problem with algorithm
Michael Bentley
michael at jedimindworks.com
Fri Apr 13 11:22:45 EDT 2007
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!")
More information about the Python-list
mailing list