I tried to build a truth table generator for practice. Here is what I wrote:

from itertools import  product, tee

def variables_truth(num=2):
    #Return a list of True and False according to the num of Variables 
    #Such as (T,T,F,F)(T,F,T,F) with num = 2

    booleans = {True : 1, False : 0} 
    data = list(product(booleans.keys(), repeat=num))
    return [[elem[i] for elem in lst] for i, lst in enumerate(tee(data, len(data[0])))]

def AND(a, b):
    return [p and q for p, q in zip(a, b)]
    
def OR(a, b):
    return [p or q for p, q in zip(a, b)]

def NOT(a):
    return [not p for p in a]

def EQUIV(a, b):
    return [p is q for p, q in zip(a, b)]
       
def IMPLIES(a, b):
    return [(not p) or q for p, q in zip(a, b)]

So that I could do something like:

X,Y = variables_truth()
IMPLIES(X,AND(X,NOT(Y)))

Which should output:

[False, True, True, True]

How can I return the arguments of the main function so that I can print every single list of truths (of course without writing the arguments again)?

In the example above I'd like to have:

NOT(Y)
[False, True, False, True]

AND(X,NOT(Y))
[False, True, False, False]

If it's impossible, please can you explain me how you would do that?

0

There are 0 answers