How do i iterate through all possible X boolean values? i.e i have 2 booleans, how do i make a table with all truths and falses?
like:
- A 0 0 1 1
- B 0 1 0 1
Advertisement
Answer
Try to iterate over the 2 ** number of variables
integers. The following example for 4 variables:
JavaScript
x
9
1
variables = ['A', 'B', 'C', 'D']
2
lv = len(variables)
3
4
print(''.join(variables))
5
print('=' * lv)
6
for i in range(2**lv):
7
print(f"{i:0{lv}b}")
8
9
will produce the following:
JavaScript
1
19
19
1
ABCD
2
====
3
0000
4
0001
5
0010
6
0011
7
0100
8
0101
9
0110
10
0111
11
1000
12
1001
13
1010
14
1011
15
1100
16
1101
17
1110
18
1111
19