1.0.9 ā€¢ Published 4 years ago

spiral-array v1.0.9

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Traverse a 2d array from the centre in a spiral

(+ other useful functions)

šŸ‘€ Displaying the data

const array = generateArray(3);
printArrayTable(array)
Traversing 9 cells
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”
ā”‚ (index) ā”‚ 0 ā”‚ 1 ā”‚ 2 ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”¤
ā”‚    0    ā”‚ 1 ā”‚ 2 ā”‚ 3 ā”‚
ā”‚    1    ā”‚ 4 ā”‚ 5 ā”‚ 6 ā”‚
ā”‚    2    ā”‚ 7 ā”‚ 8 ā”‚ 9 ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”˜

šŸŽÆ Finding the centre of an array

const centre = locateCentre(array)
centre { contents: 5, coordinates: [ 1, 1 ] }

šŸ§­ Traversing the array

const results = spiral(array);
results {
  contents: [
    5, 6, 3, 2, 1,
    4, 7, 8, 9
  ],
  coordinates: [
    [ 1, 1 ], [ 1, 2 ],
    [ 0, 2 ], [ 0, 1 ],
    [ 0, 0 ], [ 1, 0 ],
    [ 2, 0 ], [ 2, 1 ],
    [ 2, 2 ]
  ]
}

šŸ” Find the next cell

const cell = nextCell(array, [0,2], 'l');
cell {
  contents: [2],
  coordinates: [ [ 0, 1 ] ]
}

šŸ’» API

type

There are two types of response that can be returned from some functions.

  1. Contents of the cell
  2. Coordinates of the cell

If type is not specified, both contents and coordinates will be returned.

You can import these functions into your project:

module.exports = {
    spiral: (array, type) => traverseArray(array, type),
    generateArray: (size) => generateArray(size),
    printArrayTable: (array) => console.table(array),
    locateCentre: (array, type) => locateCentre(array, type),
    nextCell: (array, currentCell, direction, type) => nextCell(array, currentCell, direction, type)
};

ā„¹ļø Help

Node.js heap out of memory

When dealing with very large arrays (around 1000 x 1000 or higher) more memory can be allocated to your node process. See: https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago