Typescript References

April 11, 2021

https://itnext.io/permutations-combinations-algorithms-cheat-sheet-68c14879aba5

https://github.com/trekhleb/javascript-algorithms

Cartesian Product, Permutations

example: get all permutations of coords x,y,z for {0,1,2} +- 1

const xRange = [-1, 0, 1];
const yRange = [0, 1, 2];
const zRange = [1, 2, 3];

for (const x of xRange) {
  for (const y of yRange) {
    for (const z of zRange) {
      console.log([x, y, z]);
    }
  }
}

Manhatten Distance

let pointA = [1105, -1205, 1229];
let pointB = [-92, -2380, -20];

function manhattenDist(p1: number[], p2: number[]) {
  return p1.reduce((acc, v, idx) => {
    return (acc += Math.abs(v - pointB[idx]));
  }, 0);
}
// ...

Playground Link