Skip to content

Instantly share code, notes, and snippets.

@RobertDaleSmith
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RobertDaleSmith/e89e2e027799115c23e0 to your computer and use it in GitHub Desktop.
Save RobertDaleSmith/e89e2e027799115c23e0 to your computer and use it in GitHub Desktop.
/* Trello Node.js Developer Challenge - Solution
*
* https://trello.com/jobs/developer
*
* 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".
*
*/
// Using for loop
function hash(s){h=7;for(var i in s)h=(h*37+"acdegilmnoprstuw".indexOf(s[i]));return h}
// Using recursion
function hash(s){return s?hash(s.slice(0,-1))*37+"acdegilmnoprstuw".indexOf(s.slice(-1)):7}
// Verify and output results.
var passed = false, expect = 910135625670694, result = hash("agoodgoda");
if(result == expect) passed = true;
console.log('\nResult: \t'+ result +'\nExpected: \t'+ expect +'\nPassed: \t'+ passed +'\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment