import copy
import sys
print ( "This program reads boolean equations and outputs the truth table." )
print ( "You may use the operators ~, ∨, and ∧, or" )
print ( "their text equivalents, not, or, and and." )
print ( "You may use parenthesis, braces, and brackets to group your equations," )
print ( "and you must use single letters for your variable names, as 'a∨b'\n " )
equation = input ( )
equation_copy = equation
equation_vars = [ ]
equation = " " + equation + " "
equation = equation.replace ( "~" , " not " )
equation = equation.replace ( "∨" , " or " )
equation = equation.replace ( "∧" , " and " )
equation = equation.replace ( "[" , "(" )
equation = equation.replace ( "]" , ")" )
equation = equation.replace ( "{" , "(" )
equation = equation.replace ( "}" , ")" )
for index in range ( len ( equation) ) :
if equation[ index] in " ()^" :
continue
if equation[ index + 1 ] in " ()" and equation[ index - 1 ] in " ()" and equation[ index] not in equation_vars:
equation_vars.append ( equation[ index] )
equation_vars.sort ( )
for equationIndex in equation_vars:
sys .stdout .write ( equationIndex + '\t ' )
print ( equation_copy)
for equationIndex in range ( pow ( 2 , len ( equation_vars) ) ) :
vals = str ( bin( equationIndex) ) .replace ( '0b' , '' ) .zfill ( len ( equation_vars) )
for value in vals:
sys .stdout .write ( value + '\t ' )
equation_vars_copy = copy .deepcopy ( equation_vars)
for index in range ( len ( equation_vars_copy) ) :
equation_vars_copy[ index] += " = " + vals[ index]
exec ( equation_vars_copy[ index] )
truthTable = "result = equation"
truthTable = truthTable.replace ( 'equation' , equation)
exec ( truthTable)
if result:print ( 1 )
else :print ( 0 )
aW1wb3J0IGNvcHkKaW1wb3J0IHN5cwoKcHJpbnQoIlRoaXMgcHJvZ3JhbSByZWFkcyBib29sZWFuIGVxdWF0aW9ucyBhbmQgb3V0cHV0cyB0aGUgdHJ1dGggdGFibGUuIikKcHJpbnQoIllvdSBtYXkgdXNlIHRoZSBvcGVyYXRvcnMgfiwg4oioLCBhbmQg4oinLCBvciIpCnByaW50KCJ0aGVpciB0ZXh0IGVxdWl2YWxlbnRzLCBub3QsIG9yLCBhbmQgYW5kLiIpCnByaW50KCJZb3UgbWF5IHVzZSBwY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XRlKGVxdWF0aW9uSW5kZXggKyAnXHQnKQoKcHJpbnQoZXF1YXRpb25fY29weSkKCmZvciBlcXVhdGlvbkluZGV4IGluIHJhbmdlKHBvdygyLGxlbihlcXVhdGlvbl92YXJzKSkpOgoJdmFscyA9IHN0cihiaW4oZXF1YXRpb25JbmRleCkpLnJlcGxhY2UoJzBiJywnJykuemZpbGwobGVuKGVxdWF0aW9uX3ZhcnMpKQoKCWZvciB2YWx1ZSBpbiB2YWxzOgoJCXN5cy5zdGRvdXQud3JpdGUodmFsdWUgKyAnXHQnKQoKCWVxdWF0aW9uX3ZhcnNfY29weSA9IGNvcHkuZGVlcGNvcHkoZXF1YXRpb25fdmFycykKCWZvciBpbmRleCBpbiByYW5nZShsZW4oZXF1YXRpb25fdmFyc19jb3B5KSk6CgkJZXF1YXRpb25fdmFyc19jb3B5W2luZGV4XSArPSAiID0gIiArIHZhbHNbaW5kZXhdCgkJZXhlYyhlcXVhdGlvbl92YXJzX2NvcHlbaW5kZXhdKQoJCQkJCgl0cnV0aFRhYmxlID0gInJlc3VsdCA9IGVxdWF0aW9uIgoJdHJ1dGhUYWJsZSA9IHRydXRoVGFibGUucmVwbGFjZSgnZXF1YXRpb24nLCBlcXVhdGlvbikKCWV4ZWModHJ1dGhUYWJsZSkKCWlmIHJlc3VsdDpwcmludCgxKQoJZWxzZTpwcmludCgwKQo=
stdout
This program reads boolean equations and outputs the truth table.
You may use the operators ~, ∨, and ∧, or
their text equivalents, not, or, and and.
You may use parenthesis, braces, and brackets to group your equations,
and you must use single letters for your variable names, as 'a∨b'
A B A ∨ B
0 0 0
0 1 1
1 0 1
1 1 1