Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Last active January 1, 2016 23:19
Show Gist options
  • Save mizuhara/8215634 to your computer and use it in GitHub Desktop.
Save mizuhara/8215634 to your computer and use it in GitHub Desktop.
exports.solve = function(src) {
'use strict';
var radix = [],
n = parseInt(src, 10);
for(var i = 2; i < n; ++i) {
if( isPalindrome( makeRadixArray(n, i) ) ) {
radix.push(i);
}
}
return radix.length === 0 ? '-' : radix.join();
};
function makeRadixArray(src, radix) {
'use strict';
var radixArray = [],
n = parseInt(src, 10);
while(radix <= n) {
radixArray.push(n % radix);
n = parseInt(n / radix, 10);
}
radixArray.push(n);
return radixArray;
}
function isPalindrome(text) {
'use strict';
return text.join() === text.reverse().join();
}
var yhpg = require('./yhpg');
describe("palindrome.solve", function() {
var inputs = [
/*0*/ [ '17301', '5,38,100,218,236,5766,17300' ],
/*1*/ [ '2', '-' ],
/*2*/ [ '1', '-' ],
/*3*/ [ '3', '2' ],
/*4*/ [ '4', '3' ],
/*5*/ [ '5', '2,4' ],
/*6*/ [ '6', '5' ],
/*7*/ [ '10', '3,4,9' ],
/*8*/ [ '101', '10,100' ],
/*9*/ [ '1001', '10,25,76,90,142,1000' ],
/*10*/ [ '10001', '10,24,30,42,80,100,136,10000' ],
/*11*/ [ '1212', '22,100,201,302,403,605,1211' ],
/*12*/ [ '123412', '62,100,205,215,30852,61705,123411' ],
/*13*/ [ '5179', '5178' ],
/*14*/ [ '4919', '4918' ],
/*15*/ [ '5791', '5790' ],
/*16*/ [ '5498', '2748,5497' ],
/*17*/ [ '453', '150,452' ],
/*18*/ [ '134', '66,133' ],
/*19*/ [ '8489', '27,652,8488' ],
/*20*/ [ '1234', '22,616,1233' ],
/*21*/ [ '5497', '41,238,5496' ],
/*22*/ [ '4763', '19,35,432,4762' ],
/*23*/ [ '3974', '17,27,1986,3973' ],
/*24*/ [ '3521', '44,55,502,3520' ],
/*25*/ [ '5513', '20,38,53,148,5512' ],
/*26*/ [ '8042', '23,29,60,4020,8041' ],
/*27*/ [ '7442', '37,60,121,3720,7441' ],
/*28*/ [ '4857', '25,1618,4856' ],
/*29*/ [ '22843', '49,69,91,141,430,22842' ],
/*30*/ [ '194823', '84,121,21646,64940,194822' ],
/*31*/ [ '435697', '160,169,235,626,1822,435696' ],
/*32*/ [ '142', '3,7,70,141' ],
/*33*/ [ '886', '5,14,442,885' ],
/*34*/ [ '3102', '7,65,93,140,281,516,1033,1550,3101' ],
/*35*/ [ '17326', '11,28,99,105,8662,17325' ],
/*36*/ [ '32982', '13,72,238,477,716,1433,5496,10993,16490,32981' ],
/*37*/ [ '36', '5,8,11,17,35' ],
/*38*/ [ '37', '6,36' ],
/*39*/ [ '251', '8,250' ],
/*40*/ [ '252', '5,10,17,20,27,35,41,62,83,125,251' ],
/*41*/ [ '253', '12,14,22,252' ],
/*42*/ [ '6643', '2,3,9,81,90,510,948,6642' ],
/*43*/ [ '5040', '71,79,83,89,104,111,119,125,139,143,167,179,209,239,251,279,314,335,359,419,503,559,629,719,839,1007,1259,1679,2519,5039' ],
/*44*/ [ '9240', '23,38,62,104,109,119,131,139,153,164,167,209,219,230,263,279,307,329,384,419,439,461,615,659,769,839,923,1154,1319,1539,1847,2309,3079,4619,9239' ],
];
for(var i = 0; i < inputs.length; ++i) {
(function(src, expected) {
it("should return " + expected + " (" + i + ").", function() {
var actual = yhpg.solve(src);
expect(actual).toBe(expected);
});
})(inputs[i][0], inputs[i][1]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment