Last active
February 28, 2018 09:48
-
-
Save Codesleuth/c30c988c1831a58a08fe to your computer and use it in GitHub Desktop.
Trello Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
hash: function (s) { | |
var h = 7; | |
var letters = "acdegilmnoprstuw"; | |
for (var i = 0; i < s.length; i++) { | |
h = h * 37 + letters.indexOf(s[i]); | |
} | |
return h; | |
}, | |
findAddedIndex: function (number) { | |
for (var i = 15; i > -1; i--) { | |
var thisNumber = number - i; | |
if ((thisNumber % 37) == 0) | |
return i; | |
} | |
throw new Error('Index not found for ' + number); | |
}, | |
unhash: function (hash) { | |
var val = hash; | |
var letters = "acdegilmnoprstuw"; | |
var result = []; | |
while (val > 7) { | |
var addedValue = this.findAddedIndex(val); | |
result.splice(0, 0, letters[addedValue]); | |
val = (val - addedValue) / 37; | |
} | |
return result.join(''); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'); | |
var hasher = require('./hasher'); | |
describe('Hasher', function () { | |
it('should return 680131659347 for string "leepadg"', function () { | |
var actualHash = hasher.hash('leepadg'); | |
assert.strictEqual(actualHash, 680131659347); | |
}); | |
}); | |
describe('Added Index finder', function () { | |
it('should give index 4 for value 680131659347', function () { | |
var addedIndex = hasher.findAddedIndex(680131659347); | |
assert.strictEqual(addedIndex, 4); | |
}); | |
it('should give index 2 for value 18381936739', function () { | |
var addedIndex = hasher.findAddedIndex(18381936739); | |
assert.strictEqual(addedIndex, 2); | |
}); | |
it('should give index 0 for value 496809101', function () { | |
var addedIndex = hasher.findAddedIndex(496809101); | |
assert.strictEqual(addedIndex, 0); | |
}); | |
}); | |
describe('Unhasher', function () { | |
it('should return "leepadg" for hash 680131659347', function () { | |
var actualString = hasher.unhash(680131659347); | |
assert.strictEqual(actualString, 'leepadg'); | |
}); | |
it('should return "leepadg" for hash 956446786872726', function () { | |
var actualString = hasher.unhash(956446786872726); | |
assert.strictEqual(actualString, 'trellises'); | |
}); | |
it('should return "glacier" for hash 675202166929', function () { | |
var actualString = hasher.unhash(675202166929); | |
assert.strictEqual(actualString, 'glacier'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment