Creating a truth table for any expression in Python

前端 未结 3 726
青春惊慌失措
青春惊慌失措 2021-02-09 17:42

I am attempting to create a program that when run will ask for the boolean expression, the variables and then create a truth table for whatever is entered. I need to use a class

相关标签:
3条回答
  • 2021-02-09 18:12

    You probably want to do something like this:

    from itertools import product
    for p in product((True, False), repeat=len(variables)):
        # Map variable in variables to value in p
        # Apply boolean operators to variables that now have values
        # add result of each application to column in truth table
        pass
    

    But the inside of the for loop is the hardest part, so good luck.

    This is an example of what you would be iterating over in the case of three variables:

    >>> list(product((True, False), repeat=3))
    [(True, True, True), (True, True, False), (True, False, True), (True, False, False), (False, True, True), (False, True, False), (False, False, True), (False, False, False)]
    
    0 讨论(0)
  • 2021-02-09 18:19

    You could simply define any boolean function right in python.

    consider the following example:

    def f(w,x,y,z):
        return (x and y) and (w or z)
    

    I've wrote a snippet that takes any function f, and returns its truth table:

    import pandas as pd
    from itertools import product
    
    def truth_table(f):
        values = [list(x) + [f(*x)] for x in product([False,True], repeat=f.func_code.co_argcount)]
        return pd.DataFrame(values,columns=(list(f.func_code.co_varnames) + [f.func_name]))
    

    Using this will yield (in a nicely formatted html if you're using IPython Notebook):

    truth_table(f)
    
        w       x       y       z       f
    0   False   False   False   False   False
    1   False   False   False   True    False
    2   False   False   True    False   False
    3   False   False   True    True    False
    4   False   True    False   False   False
    5   False   True    False   True    False
    6   False   True    True    False   False
    7   False   True    True    True    True
    8   True    False   False   False   False
    9   True    False   False   True    False
    10  True    False   True    False   False
    11  True    False   True    True    False
    12  True    True    False   False   False
    13  True    True    False   True    False
    14  True    True    True    False   True
    15  True    True    True    True    True
    

    Cheers.

    0 讨论(0)
  • 2021-02-09 18:25

    I have a library that does exactly what you want! Check out the github repo or find it here on pypi.

    The readme describes how everything works, but here's a quick example:

    from truths import Truths
    print Truths(['a', 'b', 'x', 'd'], ['(a and b)', 'a and b or x', 'a and (b or x) or d'])
    +---+---+---+---+-----------+--------------+---------------------+
    | a | b | x | d | (a and b) | a and b or x | a and (b or x) or d |
    +---+---+---+---+-----------+--------------+---------------------+
    | 0 | 0 | 0 | 0 |     0     |      0       |          0          |
    | 0 | 0 | 0 | 1 |     0     |      0       |          1          |
    | 0 | 0 | 1 | 0 |     0     |      1       |          0          |
    | 0 | 0 | 1 | 1 |     0     |      1       |          1          |
    | 0 | 1 | 0 | 0 |     0     |      0       |          0          |
    | 0 | 1 | 0 | 1 |     0     |      0       |          1          |
    | 0 | 1 | 1 | 0 |     0     |      1       |          0          |
    | 0 | 1 | 1 | 1 |     0     |      1       |          1          |
    | 1 | 0 | 0 | 0 |     0     |      0       |          0          |
    | 1 | 0 | 0 | 1 |     0     |      0       |          1          |
    | 1 | 0 | 1 | 0 |     0     |      1       |          1          |
    | 1 | 0 | 1 | 1 |     0     |      1       |          1          |
    | 1 | 1 | 0 | 0 |     1     |      1       |          1          |
    | 1 | 1 | 0 | 1 |     1     |      1       |          1          |
    | 1 | 1 | 1 | 0 |     1     |      1       |          1          |
    | 1 | 1 | 1 | 1 |     1     |      1       |          1          |
    +---+---+---+---+-----------+--------------+---------------------+
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题