Last active
April 9, 2017 17:24
-
-
Save alexislagante/32ba4ac5f35f58227e01 to your computer and use it in GitHub Desktop.
This is a solution for the coding challenge included in this job posting from Fog Creek Software: http://www.fogcreek.com/jobs/dev/
This file contains 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
function crack (hash, n) { | |
var letters = "acdegilmnoprstuw"; | |
var cracked = ''; | |
while (n>0) { | |
var mod = hash % 37; | |
hash = (hash-mod)/37; | |
if (mod < 0 || mod >= letters.length) | |
throw "invalid hash!"; | |
cracked = letters[mod] + cracked; | |
n--; | |
} | |
if (hash != 7) | |
throw "invalid hash!"; | |
return cracked; | |
} | |
// leepadg | |
console.log(crack(680131659347, 7)); | |
// promenade | |
console.log(crack(945924806726376, 9)); | |
try { | |
// invalid hash | |
console.log(crack(945924806726389, 9)); | |
} catch (e) { | |
console.log(e); | |
} | |
try { | |
// invalid hash (n should be 7) | |
console.log(crack(680131659347, 10)); | |
} catch (e) { | |
console.log(e); | |
} | |
try { | |
// invalid hash (n should be 9) | |
console.log(crack(945924806726376, 10)); | |
} catch (e) { | |
console.log(e); | |
} |
This file contains 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
Find a 9 letter string of characters that contains only letters from | |
acdegilmnoprstuw | |
such that the hash(the_string) is | |
945924806726376 | |
if hash is defined by the following pseudo-code: | |
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 | |
} | |
For example, if we were trying to find the 7 letter string where hash(the_string) was 680131659347, the answer would be "leepadg". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment