Skip to content

Reduce the complexity of boolean_algebra/quine_mc_cluskey.py #8604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@
* [Archimedes Principle](physics/archimedes_principle.py)
* [Casimir Effect](physics/casimir_effect.py)
* [Centripetal Force](physics/centripetal_force.py)
* [Grahams Law](physics/grahams_law.py)
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Hubble Parameter](physics/hubble_parameter.py)
* [Ideal Gas Law](physics/ideal_gas_law.py)
Expand Down
46 changes: 20 additions & 26 deletions boolean_algebra/quine_mc_cluskey.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def is_for_table(string1: str, string2: str, count: int) -> bool:
list1 = list(string1)
list2 = list(string2)
count_n = 0
for i in range(len(list1)):
if list1[i] != list2[i]:
for item1, item2 in zip(list1, list2):
if item1 != item2:
count_n += 1
return count_n == count

Expand All @@ -92,40 +92,34 @@ def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
temp = []
select = [0] * len(chart)
for i in range(len(chart[0])):
count = 0
rem = -1
for j in range(len(chart)):
if chart[j][i] == 1:
count += 1
rem = j
count = sum(row[i] == 1 for row in chart)
if count == 1:
rem = max(j for j, row in enumerate(chart) if row[i] == 1)
select[rem] = 1
for i in range(len(select)):
if select[i] == 1:
for j in range(len(chart[0])):
if chart[i][j] == 1:
for k in range(len(chart)):
chart[k][j] = 0
temp.append(prime_implicants[i])
for i, item in enumerate(select):
if item != 1:
continue
for j in range(len(chart[0])):
if chart[i][j] != 1:
continue
for row in chart:
row[j] = 0
temp.append(prime_implicants[i])
while True:
max_n = 0
rem = -1
count_n = 0
for i in range(len(chart)):
count_n = chart[i].count(1)
if count_n > max_n:
max_n = count_n
rem = i
counts = [chart[i].count(1) for i in range(len(chart))]
max_n = max(counts)
rem = counts.index(max_n)

if max_n == 0:
return temp

temp.append(prime_implicants[rem])

for i in range(len(chart[0])):
if chart[rem][i] == 1:
for j in range(len(chart)):
chart[j][i] = 0
if chart[rem][i] != 1:
continue
for j in range(len(chart)):
chart[j][i] = 0


def prime_implicant_chart(
Expand Down