Skip to content

Instantly share code, notes, and snippets.

@brentchow
Last active August 29, 2015 14:15
Show Gist options
  • Save brentchow/98ba651233070a255c95 to your computer and use it in GitHub Desktop.
Save brentchow/98ba651233070a255c95 to your computer and use it in GitHub Desktop.
Trello JavaScript Test for Hiring New Developers - https://trello.com/jobs/developer

#The Problem# 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
}

For example, if we were trying to find the 7 letter string where hash(the_string) was 680131659347, the answer would be "leepadg".

// My Solution
function decrypt(h) {
var s = '',
letters = 'acdegilmnoprstuw';
for(i = 7; i > 0; i--) {
var p = h % 37,
l = letters.charAt(p),
s = l + s,
h = (h - p)/37;
};
return s;
};
<!-- Displaying how I disected the pattern -->
<html>
<body>
<script>
function hash(s) {
var h = 7;
var letters = 'acdegilmnoprstuw';
for (i = 0; i < s.length; i++) {
var newH = h*37;
var h = (h * 37 + letters.indexOf(s[i]));
console.log('Letter ' + s.charAt(i) + '\n h x 37 ' + newH + '\nAdd ' + letters.indexOf(s[i]) + '\nTotal ' + h);
};
return h;
};
var testString = 'leepadg'
console.log(hash(testString));
function decrypt(h) {
var s = '';
var letters = 'acdegilmnoprstuw';
for(i = 7; i > 0; i--) {
var p = h % 37;
var l = letters.charAt(p);
var s = l + s;
var h = (h - p)/37;
};
return s;
};
var testHash = '680131659347';
console.log(decrypt(testHash));
</script>
</body>
</html>
// Trello pseudo hash function (I translated into Javascript)
function hash(s) {
var h = 7;
var letters = 'acdegilmnoprstuw';
for (i = 0; i < s.length; i++) {
var h = (h * 37 + letters.indexOf(s[i]));
};
return h;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment