Skip to content

Instantly share code, notes, and snippets.

@chadsteele
Last active December 1, 2018 02:40
Show Gist options
  • Save chadsteele/eaa4c4aeae377665ebc36577ec477c3a to your computer and use it in GitHub Desktop.
Save chadsteele/eaa4c4aeae377665ebc36577ec477c3a to your computer and use it in GitHub Desktop.
/*
Assignment...
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers.
e.g. [[1,2,[3]],4] -> [1,2,3,4].
Your solution should be a link to a gist on gist.github.com with your implementation.
When writing this code, you can use any language you're comfortable with. The code must be well tested and documented
if necessary, and in general please treat the quality of the code as if it was ready to ship to production.
Try to avoid using language defined methods like Ruby's Array#flatten.*
*/
/*
Solution....
by chadsteele.com
I'm assuming this simple function would be included in a larger library, but for now,
I'll limit the scope to "tools".
You can execute in a browser or running >node test.js in your console
I'm also not depending on any external libraries although a production solution should
also not using prototypes, etc. to protect the global namespace
*/
tools = {
flatten: (ary, result) => {
result = result || [];
if (!Array.isArray(ary)){
result.push(ary);
return result;
}
ary.map(i => {
tools.flatten(i,result);
});
return result;
},
isEqual: (a, b) => {
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
return true;
},
assert: (msg, expr) => {
if (expr) console.log('PASS ' + msg);
else console.log('FAIL ' + msg);
return expr;
},
selfTest: () =>{
var allPassed = true;
//test the example
allPassed &= tools.assert("[[1,2,[3]],4] -> [1,2,3,4]", tools.isEqual([1, 2, 3, 4], tools.flatten([[1, 2, [3]], 4])));
//add strings
allPassed &= tools.assert("['one',[1,2,[3,'three']],4,[[[['five']]]]] -> ['one',1, 2, 3,'three', 4,'five']", tools.isEqual(['one',1, 2, 3,'three', 4,'five'], tools.flatten(['one',[1,2,[3,'three']],4,[[[['five']]]]])));
// test crazy deep nesting with values, strings, and arrays
var crazy = "[1,[2,[3,[4,[5,[6,[7,[8,[9,['ten',[1,[2,[3,[4,[5,[6,[7,[8,[9,['twenty',[1,[2,[3,[4,[5,[6,[7,[8,[9,['thirty',[1,[2,[3,[4,[5,[6,[7,[8,[9,['forty',[1,[2,[3,[4,[5,[6,[7,[8,[9,['fifty'],1],2],3],4],5],6],7],8],9],'sixty'],1],2],3],4],5],6],7],8],9],70],1],2],3],4],5],6],7],8],9],80]]]]]]]]]]]]]]]]]]]]";
var flatcrazy = "[1,2,3,4,5,6,7,8,9,'ten',1,2,3,4,5,6,7,8,9,'twenty',1,2,3,4,5,6,7,8,9,'thirty',1,2,3,4,5,6,7,8,9,'forty',1,2,3,4,5,6,7,8,9,'fifty',1,2,3,4,5,6,7,8,9,'sixty',1,2,3,4,5,6,7,8,9,70,1,2,3,4,5,6,7,8,9,80]";
allPassed &= tools.assert(crazy + ' -> ' + flatcrazy, tools.isEqual(eval(flatcrazy), tools.flatten(eval(crazy))));
//test that it fails too
allPassed &= tools.assert('[1,2,3] <> ' + crazy, !tools.isEqual([1,2,3], tools.flatten(eval(crazy))));
//extra crazy
crazy = eval(crazy);
flatcrazy = eval(flatcrazy);
crazy = [crazy,[crazy, crazy], crazy, [[flatcrazy]]];
flatcrazy = flatcrazy.concat(flatcrazy,flatcrazy,flatcrazy,flatcrazy);
allPassed &= tools.assert('extra crazy', tools.isEqual(flatcrazy, tools.flatten(crazy)));
// test that objects, don't confuse it
allPassed &= tools.assert('{ one:[1], two:[2], three:[3]} <> [1,2,3]', !tools.isEqual({ one:[1], two:[2], three:[3]},[1,2,3]));
allPassed &= tools.assert('[{one:1,two:2,three:3}] <> [1,2,3]', !tools.isEqual([{one:1,two:2,three:3}],[1,2,3]));
allPassed &= tools.assert('[{one:1,two:2,three:3}] -> [{one:1,two:2,three:3}]', !tools.isEqual([{one:1,two:2,three:3}],[{one:1,two:2,three:3}]));
//test empty arrays
var empty = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]';
allPassed &= tools.assert('[] -> ' + empty, tools.isEqual([], tools.flatten(eval(empty))));
if (allPassed) console.log("Yay! All selftests passed!");
return allPassed;
}
}
tools.selfTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment