Created
October 26, 2014 20:44
-
-
Save awforsythe/b7874dc1c36782accff1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
def get_snail_name(): | |
class Letters(object): | |
def __init__(self): | |
self._chars = list('nnaaiieeuh') + ['gh'] | |
def draw(self, n): | |
def draw_one(): | |
c = random.choice(self._chars) | |
self._chars.remove(c) | |
return c | |
return ''.join([draw_one() for i in range(n)]) | |
start = 's' | |
middle = Letters().draw(random.randint(2, 8)) | |
end = random.choice(['l', 'le', 'il']) | |
return (start + middle + end) | |
def find_spelling(s): | |
start = time.time() | |
tries = 1 | |
while get_snail_name() != s: | |
tries += 1 | |
elapsed = time.time() - start | |
print 'Found "%s" after %0.3f seconds and %d tries.' % (s, elapsed, tries) | |
return (elapsed, tries) | |
if __name__ == '__main__': | |
find_spelling('snail') | |
find_spelling('snale') | |
find_spelling('snaueghle') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment