Skip to content

Improve Quine–McCluskey algorithm #4935

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
merged 4 commits into from
Dec 16, 2021
Merged
Changes from all 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
46 changes: 22 additions & 24 deletions boolean_algebra/quine_mc_cluskey.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import Sequence


def compare_string(string1: str, string2: str) -> str:
"""
Expand All @@ -9,17 +11,17 @@ def compare_string(string1: str, string2: str) -> str:
>>> compare_string('0110','1101')
'X'
"""
l1 = list(string1)
l2 = list(string2)
list1 = list(string1)
list2 = list(string2)
count = 0
for i in range(len(l1)):
if l1[i] != l2[i]:
for i in range(len(list1)):
if list1[i] != list2[i]:
count += 1
l1[i] = "_"
list1[i] = "_"
if count > 1:
return "X"
else:
return "".join(l1)
return "".join(list1)


def check(binary: list[str]) -> list[str]:
Expand All @@ -28,7 +30,7 @@ def check(binary: list[str]) -> list[str]:
['0.00.01.5']
"""
pi = []
while 1:
while True:
check1 = ["$"] * len(binary)
temp = []
for i in range(len(binary)):
Expand All @@ -46,19 +48,18 @@ def check(binary: list[str]) -> list[str]:
binary = list(set(temp))


def decimal_to_binary(no_of_variable: int, minterms: list[float]) -> list[str]:
def decimal_to_binary(no_of_variable: int, minterms: Sequence[float]) -> list[str]:
"""
>>> decimal_to_binary(3,[1.5])
['0.00.01.5']
"""
temp = []
s = ""
for m in minterms:
for minterm in minterms:
string = ""
for i in range(no_of_variable):
s = str(m % 2) + s
m //= 2
temp.append(s)
s = ""
string = str(minterm % 2) + string
minterm //= 2
temp.append(string)
return temp


Expand All @@ -70,16 +71,13 @@ def is_for_table(string1: str, string2: str, count: int) -> bool:
>>> is_for_table('01_','001',1)
False
"""
l1 = list(string1)
l2 = list(string2)
list1 = list(string1)
list2 = list(string2)
count_n = 0
for i in range(len(l1)):
if l1[i] != l2[i]:
for i in range(len(list1)):
if list1[i] != list2[i]:
count_n += 1
if count_n == count:
return True
else:
return False
return count_n == count


def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
Expand Down Expand Up @@ -108,7 +106,7 @@ def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
for k in range(len(chart)):
chart[k][j] = 0
temp.append(prime_implicants[i])
while 1:
while True:
max_n = 0
rem = -1
count_n = 0
Expand Down Expand Up @@ -146,7 +144,7 @@ def prime_implicant_chart(
return chart


def main():
def main() -> None:
no_of_variable = int(input("Enter the no. of variables\n"))
minterms = [
int(x)
Expand Down