Skip to content

Instantly share code, notes, and snippets.

@sdbruder
Created July 18, 2018 01:24
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 sdbruder/8ced5b240110dfd55b1dfacb186dc4a0 to your computer and use it in GitHub Desktop.
Save sdbruder/8ced5b240110dfd55b1dfacb186dc4a0 to your computer and use it in GitHub Desktop.
question
function strUnhash(h)
{
str = "";
letters = "acdfgilmnoprstuw";
while(h>23) {
c = h % 23;
h = (h-(h%23))/23; // or a simpler Math.floor(h / 23) as h is always positive;
str = letters[c] + str;
}
return str;
}
function strHash(s)
{
hash = 7;
letters = "acdfgilmnoprstuw";
for(i = 0; i < s.length; i++)
{
hash = (hash * 23 + letters.indexOf(s[i]));
}
return hash;
}
console.log(strHash("tortilla"));
console.log(strUnhash(593846452632));
console.assert("tortilla" == strUnhash(strHash("tortilla")));
console.log(strUnhash(292681030017238));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment