Skip to content

Instantly share code, notes, and snippets.

@alej0varas
Created September 3, 2014 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alej0varas/db91bf565a2b7f46cf07 to your computer and use it in GitHub Desktop.
Save alej0varas/db91bf565a2b7f46cf07 to your computer and use it in GitHub Desktop.
My Solution to Trello challenge
""" Trello job challeng """
from itertools import product
import sys
import unittest
letters = "acdegilmnoprstuw"
def pyhash(s):
"""
Int64 hash (String s) {
Int64 h = 7
String letters = "acdegilmnoprstuw"
for(Int32 i = 0; i < s.length; i++) {
h = (h * 37 + letters.indexOf(s[i]))
}
return h
}
"""
h = 7
for i in range(len(s)):
h = (h * 37 + letters.index(s[i]))
return h
def bruteforce_pyhash_inverse(expected_result, length):
for s in product(letters, repeat=length):
s = ''.join(s)
result = pyhash(s)
if expected_result == result:
return result
def pyhash_inverse(expected_result):
"""
h = 7
for i in range(len(s)):
h = (h * 37) + letters.index(s[i])
"""
s = []
value = expected_result
c = 0
while value > 7:
if value % 37 == 0:
value = value // 37
s.append(letters[c])
c = 0
else:
c += 1
value = value - 1
s = ''.join(s[::-1])
return s
class pyhashTestCase(unittest.TestCase):
expected_result_7 = 680131659347
test_s_7 = 'leepadg'
test_l_7 = len(test_s_7)
expected_result_3 = 354610
test_s_3 = 'acd'
test_l_3 = len(test_s_3)
def test_pyhash(self):
result = pyhash(self.test_s_7)
self.assertEqual(result, self.expected_result_7)
def test_bruteforce_pyhash_inverse(self):
result = bruteforce_pyhash_inverse(self.expected_result_3,
self.test_l_3)
self.assertEqual(result, self.expected_result_3)
def test_pyhash_inverse(self):
result = pyhash_inverse(self.expected_result_7)
self.assertEqual(len(result), self.test_l_7)
self.assertEqual(result, self.test_s_7)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python trello_job.py <hash>")
exit()
value = int(sys.argv[1])
result = pyhash_inverse(value)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment