Skip to content

Instantly share code, notes, and snippets.

@ndedic
Last active November 29, 2015 14:41
Show Gist options
  • Save ndedic/2c79fbf4d28241835f84 to your computer and use it in GitHub Desktop.
Save ndedic/2c79fbf4d28241835f84 to your computer and use it in GitHub Desktop.
Trello dev challenge, for the fun of it
var TrelloSolver = (function solver() {
var ALLOWED_LETTERS = 'acdegilmnoprstuw';
function hash(str) {
var h = 7;
for ( var i = 0, iLimit = str.length; i < iLimit; i++ ) {
h = ( h * 37 + validate(ALLOWED_LETTERS.indexOf(str[i])));
}
return h;
}
function validate(idx) {
if (idx === -1) {
throw new Error('The word should only contain letters in ' + ALLOWED_LETTERS);
}
return idx;
}
return {
hash : hash
};
})();
function test() {
var solutionMapping = {
'leepadg': 680131659347,
'egilmnopr' : 920662602668674,
'a' : 259,
'wut': 375637,
'-':''
};
function assertTrue(arg1, arg2) {
return arg1 === arg2;
}
for (var s in solutionMapping) {
try {
var calculated = TrelloSolver.hash(s);
console.log('Calculated hash for ' + s + ' is ' + calculated + ' which is ' + assertTrue( calculated, solutionMapping[s] ));
} catch (e) {
console.log(e);
}
}
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment