My question is simple: \"how to build a dynamic growing truth table in python in an elegant way?\"
for n=3
for p in False, True:
for q in False, Tru
Easy math aproach:
a = lambda x: [x//4%2,x//2%2,x%2]
for i in range(8):
print(a(i))
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]
EDIT:
A more general format wold be:
def truth_table(n):
for i in range(2**n):
line = [i//2**j%2 for j in reversed(range(n))]
print(line)
This will only print the result as:
>>> truth_table(3)
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]