Created
April 8, 2018 00:02
-
-
Save joenoon/9a38a55ec23586a7614e94a0f838ea96 to your computer and use it in GitHub Desktop.
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
const LETTERS = 'acdfgilmnoprstuw'; | |
const HASH = 7; | |
function hash(s) { | |
let hash = HASH; | |
for (let i = 0; i < s.length; i++) { | |
hash = (hash * 23 + LETTERS.indexOf(s[i])); | |
} | |
return hash; | |
} | |
function unhash(num) { | |
const letters = []; | |
while (num > 7) { | |
letters.push(LETTERS[num % 23]); | |
num = parseInt(num / 23, 10); | |
} | |
return letters.reverse().join(''); | |
} | |
const test1_num = hash('tortilla'); | |
const test1_str = unhash(test1_num); | |
console.assert('tortilla' === test1_str, {test1_num, test1_str}); | |
const test2_num = 292681030017238; | |
const test2_str = unhash(test2_num); | |
console.log(`num ${test2_num} unhashed is ${test2_str}`); | |
// crowdgifts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment