This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// similar to map, but works with nested arrays | |
const deepMap = (arr, fn, idxs = [], startLen = 0) => { | |
const res = []; | |
for (let i = 0; i < arr.length; ++i) { | |
const len = startLen + res.length; | |
if (Array.isArray(arr[i])) { | |
res.push(...deepMap(arr[i], fn, [...idxs, i], len)); | |
} else { | |
res.push(fn(arr[i], idxs, len)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const KMPSearch = (() => { | |
const computeLPSArray = pat => { | |
let len = 0; | |
let i = 1; | |
const lps = [0]; | |
while (i < pat.length) { | |
if (pat[i] === pat[len]) { | |
++len; | |
lps[i] = len; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// play sine wave in browser | |
const sound = (() => { | |
return { | |
play: (duration = 1e3) => { | |
const context = new AudioContext(); | |
const o = context.createOscillator(); | |
o.type = 'sine'; | |
o.connect(context.destination); | |
o.start(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// union find | |
// quick-union with path compression | |
const unionFind = n => { | |
const arr = new Uint32Array(n); | |
for (let i = 0; i < n; ++i) { | |
arr[i] = i; | |
} | |
const root = p => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sort first array using another array | |
// - both arrays are same length | |
// - sort is stable | |
function sortUsing(a1, a2) { | |
const a = []; | |
for (let i = 0; i < a1.length; ++i) { | |
a[i] = [a1[i], a2[i], i]; | |
} | |
return a.sort((a, b) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const print2dGrid = (grid, pad = 2) => { | |
const p = ' '.repeat(pad); | |
console.log(['', ...grid.map(r => r.map(n => (p+n).substr(-pad)).join` `), ''].join`\n`); | |
}; | |
const square = [ | |
[1, 2, 3, 6, 2, 8, 1], | |
[4, 8, 2, 4, 3, 1, 9], | |
[1, 5, 3, 7, 9, 3, 1], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// select data based on ranges description | |
// min and max are optional | |
// ranges are testet from bottom to top and vice versa | |
const conditionalRange = (n, ranges, def) => { | |
for (let i = 0; i < ranges.length; ++i) { | |
const r = ranges[i]; | |
if ('max' in r) { | |
if ('min' in r) { | |
if (r.min <= n && n <= r.max) return r.data; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// big int | |
const BigInt = function BigInt(n = '0') { | |
const str = (n.join && n.join`` || n + '').replace(/^0+/, '') || '0'; | |
const sign = str[0] === '-' && +n !== 0; | |
const arr = [...str.replace(/^\-/, '')].map(n => +n); | |
const calcDbl = bi => { | |
return [1, 2, 3].reduce(([dbl, acc]) => { | |
acc = acc.add(acc); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// array intersection that correctly handles also duplicates | |
const intersection = (a1, a2) => { | |
const cnt = new Map(); | |
a2.map(el => cnt.set(el, cnt.has(el) ? cnt.get(el) + 1 : 1)); | |
return a1.filter(el => cnt.has(el)).filter(el => { | |
const left = cnt.get(el); | |
if (0 < left) { | |
cnt.set(el, left - 1); | |
return true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function groupBy(arr, fn) { | |
fn = fn || ((o) => o); | |
let g = []; | |
let v = []; | |
for (let i = 0; i < arr.length; ++i) { | |
let val = fn(arr[i]); | |
let idx = g.indexOf(val); | |
if (idx === -1) { | |
g.push(val); |