-
Notifications
You must be signed in to change notification settings - Fork 0
/
quine.py
171 lines (138 loc) · 4.09 KB
/
quine.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# ------------------------------------------------------------------------------------------------
inputs = ['A'] # Put the values that F=1 in the Terms List
terms = [1] # the terms equal to 1
d = [] # Dont care list
op = 'F' # the output name
# ------------------------------------------------------------------------------------------------
class term:
def __init__(self, x, l):
self.prime = True
self.m = [x]
self.val = bin(x)[2:]
while (len(self.val) < l):
self.val = '0' + self.val
self.ones = self.val.count('1')
def hd(self, x):
hd, pos = 0, 0
for i in range(len(x.val)):
if (self.val[i] != x.val[i]):
hd += 1
pos = i
if (hd > 1):
return -1
if (hd == 1 and (x.val.find('-') == self.val.find('-'))):
return pos
else:
return -1
def msIn(self, x):
ret = []
for i in self.m:
if (i in x):
ret += [i]
return ret, len(ret)
def __str__(self):
return self.val
def __eq__(self, x):
return self.val == x.val
def __lt__(self, x):
return self.m < x.m
# ===============================================================================
def setTerms(terms, inputs):
fo = (len(terms), len(inputs))
for i in range(fo[0]):
terms.append(term(terms[i], fo[1]))
return terms[int(len(terms) / 2):]
def combineTerms(x): # O(n^2 +n) :(
ret = []
for i in x:
for j in x:
buf = i.hd(j)
if ((buf != -1) and (j.ones - i.ones == 1)):
i.prime = False
j.prime = False
fo = list(i.val)
v = term(1, 4)
v.m = []
# for k in i.m: v.m.append(k)
v.m += i.m
# for k in j.m: v.m.append(k)
v.m += j.m
fo[buf] = '-'
v.val = ''.join(fo)
v.ones = v.val.count('1')
ret.append(v)
for i in x:
if i.prime == True:
ret.append(i)
return ret
def lettersFromBinary(x):
ret = ''
for i in range(len(x)):
if (x[i] == '0'):
# ret+='~'+inputs[i]+'.'
ret += inputs[i] + '`' + '.'
elif (x[i] == '1'):
ret += inputs[i] + '.'
return ret[:len(ret) - 1]
def result(x):
buf = ''
for i in x:
fo = lettersFromBinary(i.val)
if (fo != ''):
buf += fo + ' + '
return buf[:len(buf) - 3]
def sizeImpl(x):
while (True):
buf = combineTerms(x)
if (x == buf):
break
x = buf
return x
def getGroups(x):
buf = list(x)
for i in range(len(x)):
if (x.count(x[i]) == 2) and x[i].val != '':
x[i].val = ''
buf.remove(x[i])
return buf
def primeTable(x):
ms = {}
ret = []
for i in x:
for k in i.m:
try:
ms[k].append(i)
except:
ms[k] = [i]
for i in ms:
if (len(ms[i]) == 1 and i not in d):
for j in ms[i]:
if (j not in ret):
ret.append(j)
for i in ret:
for j in i.m: ms.pop(j, None)
for i in d:
ms.pop(i, None)
while (len(ms) != 0):
currentLength, currentGroups, prime = 0, 0, 0
for i in ms:
for j in ms[i]:
nextGroups, nextLength = j.msIn(ms.keys())
if (nextLength > currentLength):
currentLength = nextLength
currentGroups = nextGroups
prime = j
ret.append(prime)
for i in currentGroups:
ms.pop(i, None)
# print ms
return ret
def boolReduce(x):
x = primeTable(getGroups(sizeImpl(setTerms(x, inputs))))
print('The reduced Boolean equation is \n', result(x))
# classes and helper functions
# =============================================================================
terms += d
print(' ')
print('RESULT FOR ', op)
boolReduce(terms)