Skip to content

Instantly share code, notes, and snippets.

@jaimeagudo
Created November 12, 2014 00:35
Show Gist options
  • Save jaimeagudo/9fbe5a3d684aa8552e42 to your computer and use it in GitHub Desktop.
Save jaimeagudo/9fbe5a3d684aa8552e42 to your computer and use it in GitHub Desktop.
Top Secret for now ;)
// Author: Jaime Agudo <jaime.agudo.lopez@gmail.com>
//
// Github: https://github.com/jaimeagudo/
//
// License: MIT
'use strict';
/*
Write JavaScript (or CoffeeScript) code to find a 9 letter string of characters that contains only letters from
acdegilmnoprstuw
such that the hash(the_string) is
956446786872726
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
}p
//for 680131659347, the answer would be "leepadg"
*/
var hash = function(s){
var h = 7;
var legalLetters = "acdegilmnoprstuw";
var i;
if(s && s.length){
for(i = 0; i < s.length; i++) {
h = (h * 37 + legalLetters.indexOf(s[i]));
}
}
return h;
};
var stringToNumArray =function(s){
var legalLetters = "acdegilmnoprstuw";
var i, a=[];
for(i = 0; i < s.length; i++)
a.push(legalLetters.indexOf(s[i]));
return a;
};
var numArrayToString =function(a){
var legalLetters = "acdegilmnoprstuw";
var i, s=[];
for(i = 0; i < a.length; i++)
s.push(legalLetters[a[i]]);
return s.join('');
};
var hashNumbers = function(a){
var h = 7;
var i;
if(a && a.length)
for(i = 0; i < a.length; i++)
h = (h * 37 + a[i]);
return h;
};
//Just to test a correct translation
var hash2=function(s){
return hashNumbers(stringToNumArray(s));
};
var reverseHash=function(n){
var maxLength= "acdegilmnoprstuw".length;
var i;
var a=[];
var h=n;
while(h != 7){
for(i=0; i < maxLength; i++){
if((h - i) % 37 === 0){
a.push(i);
//just in case floating point calculations do weird things
h=Math.floor((h - i) / 37);
break;
}
}
}
return numArrayToString(a.reverse());
};
numArrayToString(stringToNumArray("leepadg"));
//console.log(hash("leepadg"));
//console.log(hash2("leepadg"));
var l=680131659347;
var leepadg="leepadg";
reverseHash(l);//leepadg
var x=956446786872726
var the_string=reverseHash(x); //trellises
console.log(the_string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment