Created
April 23, 2015 17:32
-
-
Save HandyAndyShortStack/253dfcfe4f64fe75b81f to your computer and use it in GitHub Desktop.
random permutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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