Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is a weasel constructor.
- # Cf. The Blind Watchmaker, Richard Dawkins, Chapter 3
- from random import choice
- ##
- # Weasel constructor
- def weasel(target='METHINKS IT IS LIKE A WEASEL',
- characters='default', mode=0):
- """mode 0: single step selection; mode 1: cumulative selection"""
- if characters == 'default':
- characters = [chr(ascii_code) for ascii_code in range(65,91)] # 'A'-'Z'
- #characters += ' '
- working_string = ''
- tries = 0
- if mode == 0: # = single step selection mode
- while working_string != target:
- # run until target found (potentially a very long time)
- working_string = ''
- for character in range(len(target)):
- working_string += choice(characters)
- # fill working_string with random characters to len of target
- print working_string
- tries += 1
- print "Total tries:",tries
- return working_string, tries
- weasel(target='FUCK')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement