Created
December 12, 2015 20:29
-
-
Save jimjeffers/d92e5c82d003fc3367fd 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
/** | |
Solution to an interview question posted by trello. I came across the link | |
not actually looking for a job but I love puzzles. So I went ahead and solved | |
it :) | |
Decrypts a value hashed 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 | |
} | |
- Parameter h: The hashed result to decode. | |
- returns: The original string used to create the hash. | |
*/ | |
func decrypt(var h: Int) -> String { | |
let letters = "acdegilmnoprstuw" | |
var result = "" | |
while h > 7 { | |
let index = h % 37 | |
result += String(letters[letters.startIndex.advancedBy(index)]) | |
h = (h - index) / 37 | |
} | |
return String(result.characters.reverse()) | |
} | |
// decrypt(680131659347) -> "leepadg" | |
// decrypt(25377615533200) -> "nsanswer" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment