Skip to content

Instantly share code, notes, and snippets.

@HandyAndyShortStack
Created April 23, 2015 17:32
Show Gist options
  • Save HandyAndyShortStack/253dfcfe4f64fe75b81f to your computer and use it in GitHub Desktop.
Save HandyAndyShortStack/253dfcfe4f64fe75b81f to your computer and use it in GitHub Desktop.
random permutation
function removeAt(arr, num, index = 0, accum = []) {
let [car, ...cdr] = arr;
index += 1;
if (arr.length < 1) {
return accum;
} else if (index !== num) {
accum.push(car);
}
return removeAt(cdr, num, index, accum);
}
export var func = removeAt;
import {func as removeAt} from './solution_20.js'
function rndSelect(arr, num, accum = []) {
let [car, ...cdr] = arr;
if (accum.length === num) {
return accum;
}
let i = Math.floor(Math.random() * arr.length);
accum.push(arr[i]);
return rndSelect(removeAt(arr, i + 1), num, accum);
}
export var func = rndSelect;
import {func as rndSelect} from './solution_23.js'
function rndPermu(arr) {
return rndSelect(arr, arr.length);
}
export var func = rndPermu;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment