-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruth.py
More file actions
executable file
·27 lines (21 loc) · 819 Bytes
/
truth.py
File metadata and controls
executable file
·27 lines (21 loc) · 819 Bytes
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
#!/usr/bin/python
import re
in_string = raw_input("Enter boolean expression: ")
l_string = in_string
# find the variables (eliminate duplicates)
list = sorted(list(set(re.findall(r"\w+", in_string))))
num_rows = 2 ** len(list)
# replace boolean variables with array indices
for i in range(len(list)):
# use negative lookahead and lookbehind to ensure exact string is replaced
# e.g. abba & dabba case
l_string = re.sub("(?<!\\w)" +list[i]+"(?!\\w)", "l["+str(i)+"]", l_string)
print list[i] + " |",
print "out"
l = [False] * len(list)
for i in range(num_rows):
# change variable values in compliance with counting order
for j in range(len(l)):
if ((i) % (2 ** (len(l)-1-j)) == 0) and i != 0:
l[j] = not l[j]
print "".join([str(int(l[k])) + " | " for k in range(len(l))])+ str(int(eval(l_string)) & 1)