Skip to content

Instantly share code, notes, and snippets.

@samdark
Last active May 12, 2020 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save samdark/3ea03c52a4fa1ffa78ea to your computer and use it in GitHub Desktop.
Save samdark/3ea03c52a4fa1ffa78ea to your computer and use it in GitHub Desktop.
function unhash(hash) {
var parts = [];
var letters = 'acdegilmnoprstuw';
while (hash !== 7) {
var index = hash % 37;
hash = (hash - index) / 37;
parts.unshift(letters[index]);
}
return parts.join('');
}
function hash(str) {
var h = 7;
var letters = 'acdegilmnoprstuw';
for (var i = 0, len = str.length; i < len; i++) {
h = (h * 37 + letters.indexOf(str[i]))
}
return h;
}
/*
Write code to find a 7 letter string of characters that contains only letters from
acdegilmnoprstuw
such that the hash(the_string) is
675202166929
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