Implement flatten
, a function that removes (flattens out) nested structure.
Examples:
[1, 2, [3, 4]] --> [1, 2, 3, 4] [[1, 2], [3, 4]] --> [1, 2, 3, 4]
Use any language and data structure/representation that makes sense. Go for the basic behavior first, but see bonus implementation notes for ideas.
These are of varying difficulty. Depending on your experience/skill, try some of these variants.
- Handle arbitrarily deep nesting (e.g.
[[[[1], [2, 3]]]] -> [1, 2, 3]
) - Allow an extra argument that sets the number of levels to flatten
- Support any combination of iterables as input
- Implement a unit test suite
- Write a version with no loop constructs
- Write a version in a pure functional style
- Write a version with no recursive calls
- Write a version you’d deploy into production. Explain your choices for the production version.
- Implement a property-based testing suite (using e.g. jsverify)