Skip to content

Instantly share code, notes, and snippets.

View gkucmierz's full-sized avatar
💻

Grzegorz Kućmierz gkucmierz

💻
View GitHub Profile
@gkucmierz
gkucmierz / deep_map.js
Created January 18, 2020 05:31
iterate over nested arrays similar to native map
// 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));
@gkucmierz
gkucmierz / kmp_search.js
Last active December 19, 2019 15:31
count all substring occurrences
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;
@gkucmierz
gkucmierz / play_sound_sine.js
Created December 19, 2019 12:05
play sine wave in browser
// 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();
// 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 => {
// 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) => {
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],
@gkucmierz
gkucmierz / conditional_ranges.js
Last active December 5, 2019 20:15
select data based on ranges description
// 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;
@gkucmierz
gkucmierz / big_int.js
Last active December 6, 2019 17:20
big int, Arbitrary-Length Integer Arithmetic
// 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);
@gkucmierz
gkucmierz / array_intersection.js
Last active December 6, 2019 16:13
array intersection
// 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;
@gkucmierz
gkucmierz / group_by.js
Created November 15, 2019 12:30
groupBy
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);