- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathquine_mc_cluskey.py
163 lines (136 loc) · 4.18 KB
/
quine_mc_cluskey.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
from __future__ importannotations
fromcollections.abcimportSequence
fromtypingimportLiteral
defcompare_string(string1: str, string2: str) ->str|Literal[False]:
"""
>>> compare_string('0010','0110')
'0_10'
>>> compare_string('0110','1101')
False
"""
list1=list(string1)
list2=list(string2)
count=0
foriinrange(len(list1)):
iflist1[i] !=list2[i]:
count+=1
list1[i] ="_"
ifcount>1:
returnFalse
else:
return"".join(list1)
defcheck(binary: list[str]) ->list[str]:
"""
>>> check(['0.00.01.5'])
['0.00.01.5']
"""
pi= []
whileTrue:
check1= ["$"] *len(binary)
temp= []
foriinrange(len(binary)):
forjinrange(i+1, len(binary)):
k=compare_string(binary[i], binary[j])
ifkisFalse:
check1[i] ="*"
check1[j] ="*"
temp.append("X")
foriinrange(len(binary)):
ifcheck1[i] =="$":
pi.append(binary[i])
iflen(temp) ==0:
returnpi
binary=list(set(temp))
defdecimal_to_binary(no_of_variable: int, minterms: Sequence[float]) ->list[str]:
"""
>>> decimal_to_binary(3,[1.5])
['0.00.01.5']
"""
temp= []
forminterminminterms:
string=""
for_inrange(no_of_variable):
string=str(minterm%2) +string
minterm//=2
temp.append(string)
returntemp
defis_for_table(string1: str, string2: str, count: int) ->bool:
"""
>>> is_for_table('__1','011',2)
True
>>> is_for_table('01_','001',1)
False
"""
list1=list(string1)
list2=list(string2)
count_n=sum(item1!=item2foritem1, item2inzip(list1, list2))
returncount_n==count
defselection(chart: list[list[int]], prime_implicants: list[str]) ->list[str]:
"""
>>> selection([[1]],['0.00.01.5'])
['0.00.01.5']
>>> selection([[1]],['0.00.01.5'])
['0.00.01.5']
"""
temp= []
select= [0] *len(chart)
foriinrange(len(chart[0])):
count=sum(row[i] ==1forrowinchart)
ifcount==1:
rem=max(jforj, rowinenumerate(chart) ifrow[i] ==1)
select[rem] =1
fori, iteminenumerate(select):
ifitem!=1:
continue
forjinrange(len(chart[0])):
ifchart[i][j] !=1:
continue
forrowinchart:
row[j] =0
temp.append(prime_implicants[i])
whileTrue:
counts= [chart[i].count(1) foriinrange(len(chart))]
max_n=max(counts)
rem=counts.index(max_n)
ifmax_n==0:
returntemp
temp.append(prime_implicants[rem])
forjinrange(len(chart[0])):
ifchart[rem][j] !=1:
continue
foriinrange(len(chart)):
chart[i][j] =0
defprime_implicant_chart(
prime_implicants: list[str], binary: list[str]
) ->list[list[int]]:
"""
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
[[1]]
"""
chart= [[0forxinrange(len(binary))] forxinrange(len(prime_implicants))]
foriinrange(len(prime_implicants)):
count=prime_implicants[i].count("_")
forjinrange(len(binary)):
ifis_for_table(prime_implicants[i], binary[j], count):
chart[i][j] =1
returnchart
defmain() ->None:
no_of_variable=int(input("Enter the no. of variables\n"))
minterms= [
float(x)
forxininput(
"Enter the decimal representation of Minterms 'Spaces Separated'\n"
).split()
]
binary=decimal_to_binary(no_of_variable, minterms)
prime_implicants=check(binary)
print("Prime Implicants are:")
print(prime_implicants)
chart=prime_implicant_chart(prime_implicants, binary)
essential_prime_implicants=selection(chart, prime_implicants)
print("Essential Prime Implicants are:")
print(essential_prime_implicants)
if__name__=="__main__":
importdoctest
doctest.testmod()
main()