Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursive solution in Clear category for Split Pairs by ibonyun
import assert from "assert";
function splitPairs(text: string): string[] {
switch (text.length) {
case 0:
return [];
case 1:
return [text + '_'];
case 2:
return [text];
default:
return [text.slice(0, 2), ...splitPairs(text.slice(2))];
}
}
console.log('Example:');
console.log(splitPairs('abcd'));
// These "asserts" are used for self-checking
assert.deepEqual(splitPairs('abcd'), ['ab', 'cd']);
assert.deepEqual(splitPairs('abc'), ['ab', 'c_']);
assert.deepEqual(splitPairs('abcdf'), ['ab', 'cd', 'f_']);
assert.deepEqual(splitPairs('a'), ['a_']);
assert.deepEqual(splitPairs(''), []);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
July 2, 2021
Comments: