Skip to content

Instantly share code, notes, and snippets.

View fronterior's full-sized avatar
:electron:

Low Front fronterior

:electron:
View GitHub Profile
/*
[[5, 3, 8, 1, 2, 4, 9, 7, 6, 5, 3, 0]]
[[3, 1, 2, 4, 3, 0], 5, [8, 9, 7, 6, 5]]
[[1, 2, 0], 3, [4, 3], 5, [7, 6, 5], 8, [9]]
[[0], 1, [2], 3, [3], 4, 5, [5, 6], 7, 8, 9]
[0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9]
*/
function quickSort(array: number[]) {
const stack: (number[]|number)[][] = [[array]];
let target: (number[]|number)[];
function mergeSort(array) {
const stack = [...array].map(n => [n]);
while (1) {
const nextStackItem = [];
const left = stack.shift();
const right = stack.shift();
let leftIndex = 0, rightIndex = 0;
if (left.length === array.length) {
function asyncMap<T>(promiseFns: (() => Promise<T>)[], max: number) {
const result: T[] = [];
let count = 0;
let cursor = 0;
return new Promise(res => {
function run() {
while (count < max && cursor < promiseFns.length) {
count++;
@fronterior
fronterior / hanoi-dfs-iterative.js
Last active April 22, 2022 13:48
The iterative solution for Tower of Hanoi.
const PLAN_TYPE = Symbol('plan');
const MOVE_TYPE = Symbol('move');
const pillarMap = {
1: 2, // 0 + 1
3: 0, // 1 + 2
2: 1, // 2 + 0
};
function getWaypoint(start, end) {
function* numberOfCases(array, count) {
const stack = [[[], array]];
let target;
while (target = stack.shift()) {
const [c, rest] = target;
if (c.length === count) yield c;
else stack.push(...rest.map((item, i) => [[...c, item], rest.filter((_, j) => j !== i)]));
}
}
const ffi = require('ffi-napi');
const user32 = new ffi.Library('user32', {
FindWindowA: ['ulong', ['string', 'string']],
FindWindowExA: ['ulong', ['ulong', 'ulong', 'string', 'ulong']],
GetDesktopWindow: ['ulong', []],
SetWindowLongPtrA: ['ulong', ['int', 'int', 'int']],
SetWindowPos: [
'bool',
['ulong', 'ulong', 'int', 'int', 'int', 'int', 'uint'],
function* combination(array, n) {
if (n === 1) {
for (const item of array) yield [item];
return;
}
for (let i = 0; i < array.length - n + 1; i++) {
const stack = [[[array[i]], array.slice(i + 1)]];
while (stack.length) {
const [comb, rest] = stack.shift();
// https://stackoverflow.com/questions/836777/how-to-detect-browsers-protocol-handlers
// https://jsfiddle.net/stefansundin/0uca4h2o/
function checkRegisteredProtocol(protocol: string) {
const input = document.createElement('input');
input.style.position = 'absolute';
input.style.opacity = '0';
document.body.appendChild(input);
let isSupported = false;
input.focus();
input.onblur = () => isSupported = true;
function* getDivisor(n) {
let temp = 0;
for (let i = 1; i <= n; i++) {
if (n % i) continue;
if (temp === i) break;
yield i;
temp = n / i;
yield temp;
}
}
function* numberOfCases(...args) {
const stack = [[[], args]];
while (stack.length) {
const target = stack.shift();
if (target) {
const [result, rest] = target;
for (let i = 0; i < rest.length; i++) {
let r;
yield r = [...result, rest[i]];
stack.push([r, rest.filter((_, index) => index !== i)]);