https://github.com/danvc/theorem
https://github.com/danvc/theorem
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/danvc/theorem
- Owner: danvc
- Created: 2019-12-19T17:37:43.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-02-11T01:17:56.000Z (over 4 years ago)
- Last Synced: 2025-02-02T15:55:50.785Z (over 1 year ago)
- Language: JavaScript
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# theorem
## The problem
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].
## Prerequisits for testing
Make sure that all dependencies for tests are available. Run:
```
yarn install
```
## How to test
```
yarn test
```
## The implementation
There are `2` implementations for this problem. One (`lazyFlatArray`) is using the very familiar method called `reduce` from the `Array` which reduces each elements to a initial value that is an empty `[]`.
The second (`fasterFlatArray`) method is a function that runs through the array which could be calling itself (recursively) to do a deeper flatten in the array.
The `fastFlatArray` is faster than the `reducer` method in almost 100%;
There is a `third` method called `createsNestedArray` which creates random nested arrays (that may contain numbers or, array of (numbers||array)
## The tests covering
The test cover the following scenarios:
- [x] pass theorem test using lazyFlatArray();
- [x] pass theorem test using fastFlatArray();
- [x] lazyFlatArray() pass over 1000 tests;
- [x] fasterFlatArray() pass over 1000 tests;
- [x] fasterFlatArray() is faster than lazyFlatArray() method;