Skip to content

Instantly share code, notes, and snippets.

View fronterior's full-sized avatar
:electron:

fronterior fronterior

:electron:
View GitHub Profile
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();
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* 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)]));
}
}
@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 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++;
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) {
/*
[[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 quickSortinPlace(array: number[]) {
const stack: [number, number][] = [[0, array.length - 1]];
let target: [number, number];
let temp: number;
while (target = stack.shift()) {
const [firstIndex, lastIndex] = target;
const middleIndex = Math.floor((firstIndex + lastIndex) / 2);
const pivot = array[middleIndex];
const heap = Array.from(Array(100), () => Math.floor(Math.random() * 100));
const main = () => {
for (let i = 1; i < heap.length; i++) run(heap, i);
};
const run = (array, target) => {
const root = Math.floor((target - 1) / 2);
if (array[root] < array[target]) {
let temp = array[root];
array[root] = array[target];
@fronterior
fronterior / crypto-stream.js
Created June 16, 2022 02:33 — forked from chris-rock/crypto-stream.js
Encrypt and decrypt streams
// Part of https://github.com/chris-rock/node-crypto-examples
// Nodejs encryption of buffers
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
var fs = require('fs');
var zlib = require('zlib');