-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruth_table.py
More file actions
73 lines (57 loc) · 2.01 KB
/
truth_table.py
File metadata and controls
73 lines (57 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- encoding: utf-8 -*-
# @Author: RZH
"""
A simple program to print the truth table of a given formula
Guides:
- input variables as uppercase letter (A, B, C, ..., Z)
- input connectives as following:
- `~`: not
- `&`: and
- `|`: or
- `^`: xor
- `>`: implication
- `=`: double implication
- brackets (`(` and `)`) are supported
- spaces will be omitted
If the given formula is invalid, an exception will be raised
"""
from re import findall
class Statement(int):
def __and__(self, other):
return Statement(int(self) & int(other))
def __or__(self, other):
return Statement(int(self) | int(other))
def __xor__(self, other):
return Statement(int(self) ^ int(other))
def __gt__(self, other): # `>` means `implication` now
return Statement(not self or other)
def __invert__(self):
return Statement(1 - self)
def __eq__(self, other):
return Statement(not (self - other))
class Formula(object):
def __init__(self, formula):
self.formula = formula.replace('=', '==')
# we will need `==` instead of `=` to call the `__eq__` function
def truth_table(self):
"""
print the truth table
:return: None
"""
variables = sorted(set(findall(r'[A-Z]', self.formula)))
print('%s Ans' % ' '.join(variables))
n = len(variables)
for i in range(2 ** n):
values: str = bin(i)[2:].rjust(n, '0')
statements: dict = dict(
zip(variables, map(Statement, map(int, values)))
)
statements['0'] = Statement(0)
statements['1'] = Statement(1)
# the values of statements will be stored in the dict `statements`
result: Statement = eval(self.formula, statements)
print('%s %s' % (values.replace('', ' ').strip(), result))
return
if __name__ == '__main__':
f = Formula(input())
f.truth_table()