最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Generating all possible truefalse combinations - Stack Overflow

programmeradmin6浏览0评论

I want to create an array of all possible binations of three variables that can either be true or false (i.e. 8 possible binations).

I am trying to create the cube in the top left corner at this image

So the output should be something like

points = [
  // first square
  {
    id: '000',
    truths: [false, false, false]
    position: [0, 0]
  },
  {
    id: '100',
    truths: [true, false, false]
    position: [5, 0]
  },
  {
    id: '010',
    truths: [false, true, false]
    position: [0, 5]
  },
  {
    id: '110',
    truths: [true, true, false]
    position: [5, 5]
  },
  // second square
  {
    id: '001',
    truths: [false, false, true]
    position: [2.5, 2.5]
  },
  {
    id: '101',
    truths: [true, false, true]
    position: [7.5, 2.5]
  },
  {
    id: '011',
    truths: [false, true, true]
    position: [2.5, 7.5]
  },
  {
    id: '111',
    truths: [true, true, true]
    position: [7.5, 7.5]
  },
];

lines = [
  { from: '000', to: '100' },
  { from: '000', to: '010' },
  { from: '000', to: '001' },

  { from: '100', to: '101' },
  { from: '100', to: '110' },

  { from: '001', to: '101' },
  { from: '001', to: '011' },

  { from: '101', to: '001' },
  { from: '101', to: '111' },

  ...
]

I don't know how to go through all possible truth values and create these points.

One approach could be to use a for loop

for (var i=0; i<Math.pow(2, 3); i++) {
  ...
}

but it doesn't help me assigning the possible truth values.

I want to create an array of all possible binations of three variables that can either be true or false (i.e. 8 possible binations).

I am trying to create the cube in the top left corner at this image

So the output should be something like

points = [
  // first square
  {
    id: '000',
    truths: [false, false, false]
    position: [0, 0]
  },
  {
    id: '100',
    truths: [true, false, false]
    position: [5, 0]
  },
  {
    id: '010',
    truths: [false, true, false]
    position: [0, 5]
  },
  {
    id: '110',
    truths: [true, true, false]
    position: [5, 5]
  },
  // second square
  {
    id: '001',
    truths: [false, false, true]
    position: [2.5, 2.5]
  },
  {
    id: '101',
    truths: [true, false, true]
    position: [7.5, 2.5]
  },
  {
    id: '011',
    truths: [false, true, true]
    position: [2.5, 7.5]
  },
  {
    id: '111',
    truths: [true, true, true]
    position: [7.5, 7.5]
  },
];

lines = [
  { from: '000', to: '100' },
  { from: '000', to: '010' },
  { from: '000', to: '001' },

  { from: '100', to: '101' },
  { from: '100', to: '110' },

  { from: '001', to: '101' },
  { from: '001', to: '011' },

  { from: '101', to: '001' },
  { from: '101', to: '111' },

  ...
]

I don't know how to go through all possible truth values and create these points.

One approach could be to use a for loop

for (var i=0; i<Math.pow(2, 3); i++) {
  ...
}

but it doesn't help me assigning the possible truth values.

Share Improve this question asked Sep 27, 2016 at 21:51 mortensenmortensen 1,2372 gold badges14 silver badges23 bronze badges 3
  • 1 There are 2^n possible values. If you don't want to use nested for loops (you really shouldn't) then extract the bits of the integers 0...2^n. The n values in truths will be the bits of the integer. – plasmacel Commented Sep 27, 2016 at 21:56
  • I just don't get if your order is 0,4,2,3,1,5,7,8 how a binary approach will help you. Why don't you use just numbers. – Redu Commented Sep 27, 2016 at 22:11
  • @Redu I don't get what are you talking about. The order doesn't matter. All integers from 0 to 8 will represent 3 bits, which correspond to the truths array in the OP's analogy. 2^n integers = 2^n truths arrays. In binary, numbers can be thought as "arrays" of bits: 0=[0,0,0], 1=[0,0,1], 2=[0,1,0], 3=[0,1,1], 4=[1,0,0] , 5=[1,0,1], 6=[1,1,0], 7=[1,1,1]. – plasmacel Commented Sep 27, 2016 at 22:35
Add a ment  | 

5 Answers 5

Reset to default 10

Everything in a puter is already binary. You don't need any fancy Math.pow or similar.

for (let i = 0; i < 1 << 3; i++) {
  console.log([!!(i & (1<<2)), !!(i & (1<<1)), !!(i & 1)]);
}

While this looks nice and short, i am actually not a fan of !! or magic numbers. I always fall for these tricks when writing snippets though. Therefore will attempt to give a slightly cleaner version:

const AMOUNT_OF_VARIABLES = 3;

for (let i = 0; i < (1 << AMOUNT_OF_VARIABLES); i++) {
  let boolArr = [];
  
  //Increasing or decreasing depending on which direction
  //you want your array to represent the binary number
  for (let j = AMOUNT_OF_VARIABLES - 1; j >= 0; j--) {
    boolArr.push(Boolean(i & (1 << j)));
  }
  
  console.log(boolArr);
}

It's easy, just convert all integer from 0 through 2**n-1 to binary:

var n = 3,
    m = 1 << n;
for (var i = 0; i < m; i++) {
    var s = i.toString(2); // convert to binary
    s = new Array(n + 1 - s.length).join('0') + s; // pad with zeroes
    console.log(s);
}

The above code is general; you can change n to the number bits that you want.

There are pow(2, n) possible values.

In the binary number system, numbers can be simply thought as "arrays" of bits: 0=[0,0,0], 1=[0,0,1], 2=[0,1,0], 3=[0,1,1], 4=[1,0,0], 5=[1,0,1], 6=[1,1,0], 7=[1,1,1].

Following this idea, the simplest approach is to extract the bits of the integers [0, pow(2, n) - 1]. Here is the code which is a straightforward implementation of the above idea:

function test()
{
   var n = 3;
   var k = (1 << n); // bit trick for pow(2, n)

   var truths = [];

   for (var i = 0; i < k; ++i)
   {
      truths[i] = [];

      for (var j = 0; j < n; ++j)
      {
         var value = (i >> j) & 1; // extract the j-th bit of i
         truths[i][j] = value;
      }

      console.log(truths[i]);
   }
}

const boolCombo = size => {
  const buf = Array(1 << size)
  for (let i = buf.length; i--;) {
    buf[ i ] = Array(size)
    for (let j = size; j--;)
      buf[ i ][ j ] = +!!(i & 1 << j)
  }
  return buf
}

console.log(boolCombo(3))

#include <stdio.h>

#include <math.h>

int main()

{

int n, c, k,arr[100],m,i;

scanf("%d", &n);

m=pow(2,n);

for(i=0;i<m;i++)

arr[i]=i;

for(i=0;i<m;i++)

{

for (c = n-1; c >= 0; c--)

{

k = arr[i] >> c;

if (k & 1)

printf("true ");

else

printf("false ");

}

printf("\n");

}

return 0;

}

发布评论

评论列表(0)

  1. 暂无评论