From Robert Sedgewick's paper, Permutation Generation Methods (1977), we obtain the basic recursive algorithm on page 140:
To generate all permutations of P[1], • • •, P[N], we repeat N times the step: "first generate all permutations of P[1], •••, P[N-1], then exchange P[N] with one of the elements P[1], •••, P[N-1]."
A first JavaScript generator function can be derived like this:
Код: Выделить всё
function* permutations(n) {
if (n == 1) yield P;
else {
for (let i = 1; i
Подробнее здесь: [url]https://stackoverflow.com/questions/79666067/can-two-equivalent-javascript-generators-be-derived-from-each-other[/url]
Мобильная версия