v1
9/17/2017 by przemek -10
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
function permuteReganKoopmans(list) {
    if (list.length == 1) { return [list] }
    let permutations = []
    let subpermutations = permuteReganKoopmans(list.slice(1, list.length))
    for (index in subpermutations) {
        let sublist = subpermutations[index]
        for (let pos = 0; pos < sublist.length+1; pos++) {
             permutations.push(sublist.slice(0, pos)
                         .concat([list[0]])
                         .concat(sublist.slice(pos, sublist.length)));
        }
     }
     return permutations;
}

const permutePrzemek = arr => { 
	const permutations = [];

	const swap = (a, b) => {
		const tmp = arr[a];
		arr[a] = arr[b];
		arr[b] = tmp;
	};

	const generate = n => {
		if (n === 1) {
			return permutations[permutations.length] = arr;
		}
		for (let i = 0; i < n; i++) {
			generate(n - 1);
			swap(n % 2 === 0 ? i : 0, n - 1);
		}
	};

	generate(arr.length);
	return permutations;
}
delete caserun single casemove downdrag and drop case


ready



permuteReganKoopmans([1,2,3,4]);
delete caserun single casemove updrag and drop case


ready



permutePrzemek([1,2,3,4]);
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN