Reduce the complexity of boolean_algebra/quine_mc_cluskey.py (#8604)

* Reduce the complexity of boolean_algebra/quine_mc_cluskey.py

* updating DIRECTORY.md

* Fix

* Fix review issues

* Fix

* Fix review issues

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Maxim Smolskiy 2023-08-16 02:10:22 +03:00 committed by GitHub
parent efaf526737
commit f66568e981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,10 +74,7 @@ def is_for_table(string1: str, string2: str, count: int) -> bool:
""" """
list1 = list(string1) list1 = list(string1)
list2 = list(string2) list2 = list(string2)
count_n = 0 count_n = sum(item1 != item2 for item1, item2 in zip(list1, list2))
for i in range(len(list1)):
if list1[i] != list2[i]:
count_n += 1
return count_n == count return count_n == count
@ -92,40 +89,34 @@ def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
temp = [] temp = []
select = [0] * len(chart) select = [0] * len(chart)
for i in range(len(chart[0])): for i in range(len(chart[0])):
count = 0 count = sum(row[i] == 1 for row in chart)
rem = -1
for j in range(len(chart)):
if chart[j][i] == 1:
count += 1
rem = j
if count == 1: if count == 1:
rem = max(j for j, row in enumerate(chart) if row[i] == 1)
select[rem] = 1 select[rem] = 1
for i in range(len(select)): for i, item in enumerate(select):
if select[i] == 1: if item != 1:
continue
for j in range(len(chart[0])): for j in range(len(chart[0])):
if chart[i][j] == 1: if chart[i][j] != 1:
for k in range(len(chart)): continue
chart[k][j] = 0 for row in chart:
row[j] = 0
temp.append(prime_implicants[i]) temp.append(prime_implicants[i])
while True: while True:
max_n = 0 counts = [chart[i].count(1) for i in range(len(chart))]
rem = -1 max_n = max(counts)
count_n = 0 rem = counts.index(max_n)
for i in range(len(chart)):
count_n = chart[i].count(1)
if count_n > max_n:
max_n = count_n
rem = i
if max_n == 0: if max_n == 0:
return temp return temp
temp.append(prime_implicants[rem]) temp.append(prime_implicants[rem])
for i in range(len(chart[0])): for j in range(len(chart[0])):
if chart[rem][i] == 1: if chart[rem][j] != 1:
for j in range(len(chart)): continue
chart[j][i] = 0 for i in range(len(chart)):
chart[i][j] = 0
def prime_implicant_chart( def prime_implicant_chart(