获取所有数组的排列[1,1,1,1,1,0,0,0,0]

住友

我正在尝试创建一个脚本,该脚本将生成二进制开关的所有各种排列,其中应该有51和4 0并且数组应为size 9

我尝试了以下代码。排列的条件是:1.数组集应该是唯一的。2.1彼此相邻不超过3个

const row = [1, 1, 1, 1, 1, 0, 0, 0, 0];
const list = [];
const fullList = [];

// To make sure that no more than 3 `1` are next to each other
const isRowValid = (row) => {
  let isValid = true;
  for(let i = 0; i+2 < row.length; i++) {
    if(row[i] === 1 && row[i+1] === 1 && row[i+2] === 1) {
      isValid = false;
      break;
    }
  }
  return isValid;
}


const combinations = (row, baseIndex, currentIndex, iterationLevel, list) => {
  if(currentIndex > row.length - iterationLevel) {
      baseIndex++;
      currentIndex = 0;
  }

  if(baseIndex + iterationLevel > row.length) {
    baseIndex = 0;
    iterationLevel++;
  }

  if(iterationLevel === 5) {
    return;
  }

  let rowCopy = [...row]
  if(baseIndex > currentIndex ) {
    let first = [...row.slice(0, currentIndex)];
    let second = [...row.slice(currentIndex)];
    let value = second.splice(baseIndex - currentIndex, iterationLevel);
    rowCopy =  [...first, ...value, ...second]
  } else if(baseIndex < currentIndex) {
    let first = [...row.slice(0, currentIndex + iterationLevel)];
    let second = [...row.slice(currentIndex + iterationLevel)];
    let value = first.splice(baseIndex, iterationLevel);
    rowCopy = [...first, ...value, ...second];
  }
  if(isRowValid(rowCopy)) {
      list.push(rowCopy);
  }
  console.log(rowCopy);
  combinations(row, baseIndex, currentIndex + 1, iterationLevel, list);
}

combinations(row, 0, 0, 1, list);
list.forEach(l => combinations(l, 0, 0, 1, fullList));

// To remove duplicates
for(let i = 0; i < fullList.length; i++) {
  const base = fullList[i]
  for(let j = i + 1; j < fullList.length; j++) {
    const isSame = fullList[j].every((l, m) => base[m] === l);
    if(isSame) {
      fullList[j] = [];
    }
  }
}

let filtered = fullList.filter(l => l.length !== 0);
console.log(filtered.length);

filtered.slice(0, 100).map(i => console.log(i));
console.log(fullList.length);

JS斌

al

如果我正确理解的话,您的意思是排列而不是组合,其中每个排列中“打开”的顺序开关不应超过3个。

每当您必须生成排列或组合时,都可以使用递归回溯算法。

这个想法很简单,在每个步骤中,您都遵循可能的选择,直到满足基本条件为止(例如,由于,完成了排列perm.length === switchCount)。采取步骤时,您会在问题的状态上反映出该选择,而当递归调用返回时,您将撤消这些影响。

为了确定在每个步骤中可以做出哪些选择,我们需要跟踪问题的状态。在这里,我们只需要知道还剩下多少个打开/关闭开关,以及到目前为止我们有多少个顺序打开开关(seqOn)。

const perms = permute(5, 4);

console.log(perms.length);
console.log(perms);

function permute(on, off) {
  const switchCount = on + off;
  const perm = [], perms = [];

  p(on, off, 0);

  return perms;

  function p(on, off, seqOn) {
      if (perm.length === switchCount) {
          perms.push([...perm]);
          return;
      }

      if (on && seqOn < 3) {
          perm.push(1);
          p(on - 1, off, seqOn + 1);
          perm.pop();
      }

      if (off) {
          perm.push(0);
          p(on, off - 1, 0);
          perm.pop();
      }
  }
}

如果我们有许多要枚举的排列,我们也可以使用生成器来节省内存。在这里,我得到了perm保存O(n)时间副本的相同数组。只要您不需要保留副本,只需枚举开关就可以了。

for (const perm of permute(5, 4)) {
    console.log(perm);
}

function* permute(on, off) {
    const switchCount = on + off;
    const perm = [];

    yield* p(on, off, 0);

    function* p(on, off, seqOn) {
        if (perm.length === switchCount) {
            yield perm;
            return;
        }

        if (on && seqOn < 3) {
            perm.push(1);
            yield* p(on - 1, off, seqOn + 1);
            perm.pop();
        }

        if (off) {
            perm.push(0);
            yield* p(on, off - 1, 0);
            perm.pop();
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

检查数组元素中的所有数字是否为0或1 [c ++]

[“ $ {1:0:1}” ='-']的含义

如何使用a [0]-a [0] a [1]-a [0] a [1] a [2]输出数组

有什么办法可以循环通过这些数字:-1 0、1 0、0 -1、0 1

确保数组的所有值都编码为1和-1,而不是1和0

数组访问args [0] [1]-'0'

在R中将(0,1,0,0,1,1,1)转换为(0,0,0,1,0,1,2)

如何从1而不是0开始排列键?

Python-0 ** 0 == 1?

(0 * 1 *)*等于(0 | 1)*吗?

在 Python 中,如何按顺序生成一串 0 和 1 的所有排列?

将0/1添加到数据帧行的每一列的所有排列

如何递归地找到变量0和1的所有排列(不使用itertools或random)?

如何在JAVA中获得0和1位的所有可能排列

如何在pandas数据帧列python中将所有数字==加1到0

R:根据临界值对所有数字变量(1:0)进行分类

使用循环生成 0 到 1 的所有数字

CSS转换。如何从“ matrix(0,1,-1,0,0,0“)获取旋转度值?

为什么为 dx[dir 选择值 {1, 1, 0, -1, -1, -1, 0, 1} 和 {0, 1, 1, 1, 0, -1, -1, -1} ] 和 dy[dir]?

(假-NOT(0))等于1?

cout << 1 && 0;的输出

关系0 ... 1在Laravel

验证0或1的参数

[$的含义?== 1] &&返回0

提取包含0 | 0,0 | 1,1 | 0和1 | 1的文件

用于生成 [((0,0),0), ((0,1),0), ((1,0),0), ((1,1),0)] 的代码实际上给出了 [0 , 0, 0, 1, 1, 0, 1, 1],如何解决?

使用request.getRemoteAddr()返回0:0:0:0:0:0:0:0:1

zookeeper无法打开localhost / 0:0:0:0:0:0:0:0:1:2181的套接字

如果所有行均为0,NA和1,则替换所有0