Skip to content

Instantly share code, notes, and snippets.

@awforsythe
Created October 26, 2014 20:44
Show Gist options
  • Save awforsythe/b7874dc1c36782accff1 to your computer and use it in GitHub Desktop.
Save awforsythe/b7874dc1c36782accff1 to your computer and use it in GitHub Desktop.
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