fork(6) download
  1. import copy
  2. import sys
  3.  
  4. print("This program reads boolean equations and outputs the truth table.")
  5. print("You may use the operators ~, ∨, and ∧, or")
  6. print("their text equivalents, not, or, and and.")
  7. print("You may use parenthesis, braces, and brackets to group your equations,")
  8. print("and you must use single letters for your variable names, as 'a∨b'\n")
  9.  
  10. equation = input()
  11. equation_copy = equation
  12. equation_vars = []
  13. equation = " " + equation + " "
  14. equation = equation.replace("~", " not ")
  15. equation = equation.replace("∨", " or ")
  16. equation = equation.replace("∧", " and ")
  17. equation = equation.replace("[", "(")
  18. equation = equation.replace("]", ")")
  19. equation = equation.replace("{", "(")
  20. equation = equation.replace("}", ")")
  21.  
  22. for index in range(len(equation)):
  23. if equation[index] in " ()^":
  24. continue
  25. if equation[index + 1] in " ()" and equation[index - 1] in " ()" and equation[index] not in equation_vars:
  26. equation_vars.append(equation[index])
  27.  
  28. equation_vars.sort()
  29.  
  30. for equationIndex in equation_vars:
  31. sys.stdout.write(equationIndex + '\t')
  32.  
  33. print(equation_copy)
  34.  
  35. for equationIndex in range(pow(2,len(equation_vars))):
  36. vals = str(bin(equationIndex)).replace('0b','').zfill(len(equation_vars))
  37.  
  38. for value in vals:
  39. sys.stdout.write(value + '\t')
  40.  
  41. equation_vars_copy = copy.deepcopy(equation_vars)
  42. for index in range(len(equation_vars_copy)):
  43. equation_vars_copy[index] += " = " + vals[index]
  44. exec(equation_vars_copy[index])
  45.  
  46. truthTable = "result = equation"
  47. truthTable = truthTable.replace('equation', equation)
  48. exec(truthTable)
  49. if result:print(1)
  50. else:print(0)
  51.  
Success #stdin #stdout 0.03s 9696KB
stdin
A ∨ B
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