Skip to main content
Post Reopened by chrslg, ncica, Kelly Bundy
deleted 33 characters in body
Source Link
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    // Recursion base case
    if (n == 1) yield P;
    else for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) swap(n, heap(n, i));
    }
  }
}

permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;

  // Need to log the initial permutation explicitly.
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
  }
}
   
permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    // Recursion base case
    if (n == 1) yield P;
    else for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) swap(n, heap(n, i));
    }
  }
}

permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
  }
}
   
permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
    
  for (p of permutations(N)) console.log(count++, p.slice(1));

  function* permutations(n) {
    // Recursion base case
    if (n == 1) yield P;
    else for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) swap(n, heap(n, i));
    }
  }
}

permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;

  // Need to log the initial permutation explicitly.
  console.log(count++, P.slice(1));
    
  for (p of permutations(N)) console.log(count++, p.slice(1));

  function* permutations(n) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
  }
}
   
permute(4);
deleted 33 characters in body
Source Link
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    if (n > 1) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
    }
  }
}
   
 
permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    if (n > 1) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
    }
  }
}
   
 
permute(4);
function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
  }
}
   
permute(4);
deleted 2 characters in body
Source Link

The source code is re-edited to be runnable as self-contained code snippets, ebeneven though in the literature it is common to see algorithms with "globals".

function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    if (n > 1) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
    }
  }
}
   

permute(4);

The source code is re-edited to be runnable as self-contained code snippets, eben though in the literature it is common to see algorithms with "globals".

function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    if (n > 1) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
    }
  }
}

permute(4);

The source code is re-edited to be runnable as self-contained code snippets, even though in the literature it is common to see algorithms with "globals".

function permute(N) {
  function swap(x, y) { [ P[x], P[y] ] = [ P[y], P[x] ] }
  function heap(n, i) { return (n % 2) || i }

  var P = []; for (let i = 1; i <= N; i++) P[i] = i;
  var count = 1;
  console.log(count++, ":", P.slice(1));
    
  for (p of permutations(N)) console.log(count++, ":", p.slice(1));

  function* permutations(n) {
    if (n > 1) {
      for (let i = 1; i <= n; i++) {
        yield* permutations(n - 1);
        if (i < n) { swap(n, heap(n, i)); yield P }
      }
    }
  }
}
   

permute(4);
Complete re-edit to make it focused. Will delete the re-asked duplicate: https://stackoverflow.com/q/79666543/8703562
Source Link
Loading
edited tags
Link
Loading
Post Closed as "Opinion-based" by Pointy, user16540390, Yogi
added 1 character in body
Source Link
Loading
Source Link
Loading