Skip to content

Instantly share code, notes, and snippets.

View gkucmierz's full-sized avatar
💻

Grzegorz Kućmierz gkucmierz

💻
View GitHub Profile
@gkucmierz
gkucmierz / array_to_bst.js
Last active April 23, 2020 22:34
convert array to BST - binary search tree
const arrayToBst = arr => {
const [LEFT, VALUE, RIGHT] = [0, 1, 2];
const node = [null, null, null]
const bst = node.slice();
arr.map(el => {
let tmp = bst;
while (tmp[VALUE] !== null) {
const branch = el < tmp[VALUE] ? LEFT : RIGHT;
if (tmp[branch] === null) {
@gkucmierz
gkucmierz / 10000_primes.js
Created April 23, 2020 17:33
count primes - erastotenes sieve
const sieve = [];
const MAX = 10008;
for (let i = 2; i < MAX; ++i) {
for (let j = i * 2; j < MAX; j += i) {
sieve[j] = 1;
}
}
function countPairs(arr) {
// console.log('regular:');
let cnt = 0;
for (let i = 0; i < arr.length; ++i) {
for (let j = i + 1; j < arr.length; ++j) {
const and = arr[i] & arr[j];
const log = Math.log(and) / Math.log(2);
@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;