Public
Edited
Jun 14, 2024
Insert cell
Insert cell
function permuteT1() {
let permutations = []
for (let i = 0; i < 3; i++) {
permutations.push([i])
}

return permutations;
}
Insert cell
permuteT1()
Insert cell
function permuteT2() {
let permutations = [];

for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i == j) continue;
permutations.push([i, j]);
}
}

return permutations;
}
Insert cell
permuteT2()
Insert cell
function permuteT3() {
const permutations = [];

for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j == i) continue;
for (let k = 0; k < 3; k++) {
if (k == i || k == j) continue;
permutations.push([i, j, k]);
}
}
}

return permutations;
}
Insert cell
permuteT3()
Insert cell
function permute(n, x) {
const permutations = [];

function pick(current) {
if (current.length == x) {
permutations.push(current);
return;
}

for (let i = 0; i < n; i++) {
if (current.includes(i)) continue;
pick([...current, i]);
}
}

pick([]);
return permutations;
}
Insert cell
permute(1,1)
Insert cell
permute(2,2)
Insert cell
permute(3,2)
Insert cell
permute(3,3)
Insert cell
function permuteS(n, x) {
const permutations = [];
let current = new Array(n);
const selection = j => current.slice(0, j);

function pick(j) {
if (j === x) {
permutations.push(selection(x));
return;
}

for (let i = 0; i < n; i++) {
if (selection(j).includes(i)) continue;
current[j] = i;
pick(j+1);
}
}

pick(0);
return permutations;
}
Insert cell
permuteS(3,2)
Insert cell
permuteS(3,3)
Insert cell
function permuteR(n, x) {
const logs = []
let current = new Array(n).fill(-1);
const selection = j => current.slice(0, j);

function pick(j) {
if (j === x) {
logs.push(selection(x));
return;
}

while (current[j] < n - 1) {
current[j]++;
if (selection(j).includes(current[j])) continue;
pick(j+1);
}

current[j] = -1;
}

pick(0)
return logs
}
Insert cell
permuteR(3,2)
Insert cell
permuteR(3,3)
Insert cell
function permuteB(n, x) {
const logs = []
function pick(current) {
if (current.length == x) {
logs.push(current);
return;
}

for (let i = 0; i < n; i++) {
// if (current.includes(i)) continue;
pick([...current, i]);
}
}

pick([]);
return logs
}
Insert cell
permuteB(3,2)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more