Last active
August 29, 2015 14:01
-
-
Save rdkls/852352bc7838249229ea to your computer and use it in GitHub Desktop.
My python solution to the problem on the Fog Creek job app page http://www.fogcreek.com/Jobs/Dev/
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
#!/usr/bin/python | |
the_letters = 'acdegilmnoprstuw' | |
target_hash = 910897038977002 | |
target_word_length = 9 | |
def possible_strings(given_hash, string_so_far=''): | |
try: | |
given_hash = float(given_hash) | |
except ValueError: | |
raise Exception('Hash must be a number') | |
if len(string_so_far) >= target_word_length: | |
return string_so_far | |
for l in the_letters: | |
pre_division = given_hash - the_letters.index(l) | |
if not pre_division % 37: | |
p = possible_strings(pre_division / 37, l + string_so_far) | |
if p: | |
return p | |
def hash(s): | |
"""actually unused, but this is how the hash is calculated""" | |
h = 7 | |
i = 0 | |
for l in s: | |
h = h*37 + the_letters.index(s[i]) | |
i += 1 | |
return h | |
def brute_force(): | |
import itertools | |
import sys | |
for s in itertools.permutations(letters, 9): | |
s = ''.join(s) | |
h = hash(s) | |
print '%s = %s' % (s,h) | |
if 910897038977002 == hash(s): | |
print s | |
sys.exit(0) | |
if __name__ == '__main__': | |
print possible_strings(target_hash) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment