Skip to content

Instantly share code, notes, and snippets.

@made2591
Created July 19, 2019 14:25
Show Gist options
  • Save made2591/92ea60b2f2bfb12ae6ad03de314b286a to your computer and use it in GitHub Desktop.
Save made2591/92ea60b2f2bfb12ae6ad03de314b286a to your computer and use it in GitHub Desktop.
Flattener

Flattener

Flattener is a utility to flatten an array of arbitrarily nested arrays of integers into a flat array of integers.

Getting Started

To run the project, just create a folder and put the file flattener.js, test.js and package.json inside.

Prerequisites

Installing

To install

npm install

To run the code, open the node shell in the project folder (node) and run

const flattener = require("./flattener")
flattener([[1, 2, [3]], 4])

Running the tests

To run test

npm test

Regarding the tests

The tests cover multiple scenario: empty cases, undefined input, multiple nested structure.

flattener_recursive

There's a not exported recursive implementation of the flattener inside the package. This version may be more readable and shorter but can go out of memory for deeply nested arrays, so I preferred to submit the iterative version of it.

Author

// --- Directions
// Write some code, that will flatten an array of arbitrarily
// nested arrays of integers into a flat array of integers.
// --- Examples
// flattener([[1,2,[3]],4]) --> [1,2,3,4]
function flattener(array) {
if (!Array.isArray(array)) throw new Error('The input value is not an array');
var flattened = []
if (array.length === 0) return flattened
var stack = []
stack.push(...array)
while (stack.length != 0) {
var elems = stack.shift()
if (Array.isArray(elems)) {
stack.unshift(...elems)
} else {
flattened.push(elems)
}
}
return flattened;
}
// See README.md for more information
function flattener_recursive(array) {
if (Array.isArray(array)) {
return array.reduce(function (flattened, current) {
return flattened.concat(flattener_recursive(current));
}, []);
} else {
return array;
}
}
module.exports = flattener;
{
"name": "flattener",
"version": "0.1.0",
"description": "Flattener by made2591",
"main": "flattener.js",
"scripts": {
"test": "jest"
},
"keywords": [
"flattener",
"test",
"interview",
"conversio",
"excercise",
"code"
],
"author": "Matteo Madeddu <matteo.madeddu@gmail.com> (https://madeddu.xyz/)",
"license": "MIT",
"devDependencies": {},
"dependencies": {
"jest": "^24.8.0"
}
}
const flattener = require('./flattener.js');
// function exist and it's correctly exported
test('flattener function exists', () => {
expect(typeof flattener).toEqual('function');
});
// empty cases works
test('"[]" is the flattened form of "[]"', () => {
expect(flattener([])).toEqual([])
});
test('"[[], [[]]]" is the flattened form of "[]"', () => {
expect(flattener([[], [[]]])).toEqual([])
});
// easy case works
test('"[1, 2, 3]" is the flattened form of "[1, 2, 3]"', () => {
expect(flattener([1, 2, 3])).toEqual([1, 2, 3])
});
// provided case works
test('"[1,2,3,4]" is the flattened form of "[[1,2,[3]],4]"', () => {
expect(flattener([[1, 2, [3]], 4])).toEqual([1, 2, 3, 4])
});
// test more complex cases
test('"[[[[[[[[2, 5, 6, 4, 6]]]]]]]]" is the flattened form of "[2, 5, 6, 4, 6]"', () => {
expect(flattener([[[[[[[[2, 5, 6, 4, 6]]]]]]]])).toEqual([2, 5, 6, 4, 6])
});
test('"[2, 5, 6, 4, 6 , 6, 3, 2, 5, 8]" is the flattened form of "[[2, 5, 6], [[4,6]], [6,3, [2, 5]], 8]"', () => {
expect(flattener([[2, 5, 6], [[4, 6]], [6, 3, [2, 5]], 8])).toEqual([2, 5, 6, 4, 6, 6, 3, 2, 5, 8])
});
// undefined throws error
test('"undefined" input throws an error', () => {
expect(() => { flattener(undefined) }).toThrow()
});
// string input throws error
test('string input throws an error', () => {
expect(() => { flattener("Hello") }).toThrow()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment