mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Improve Quine–McCluskey algorithm (#4935)
* Syntax improvements (I hope) to boolean algebra * Reverted certain index variables to i * remove extra line on decimal_to_binary * Update quine_mc_cluskey.py Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:
parent
6680e435a7
commit
9af2eef9b3
@ -1,5 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Sequence
|
||||||
|
|
||||||
|
|
||||||
def compare_string(string1: str, string2: str) -> str:
|
def compare_string(string1: str, string2: str) -> str:
|
||||||
"""
|
"""
|
||||||
@ -9,17 +11,17 @@ def compare_string(string1: str, string2: str) -> str:
|
|||||||
>>> compare_string('0110','1101')
|
>>> compare_string('0110','1101')
|
||||||
'X'
|
'X'
|
||||||
"""
|
"""
|
||||||
l1 = list(string1)
|
list1 = list(string1)
|
||||||
l2 = list(string2)
|
list2 = list(string2)
|
||||||
count = 0
|
count = 0
|
||||||
for i in range(len(l1)):
|
for i in range(len(list1)):
|
||||||
if l1[i] != l2[i]:
|
if list1[i] != list2[i]:
|
||||||
count += 1
|
count += 1
|
||||||
l1[i] = "_"
|
list1[i] = "_"
|
||||||
if count > 1:
|
if count > 1:
|
||||||
return "X"
|
return "X"
|
||||||
else:
|
else:
|
||||||
return "".join(l1)
|
return "".join(list1)
|
||||||
|
|
||||||
|
|
||||||
def check(binary: list[str]) -> list[str]:
|
def check(binary: list[str]) -> list[str]:
|
||||||
@ -28,7 +30,7 @@ def check(binary: list[str]) -> list[str]:
|
|||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
pi = []
|
pi = []
|
||||||
while 1:
|
while True:
|
||||||
check1 = ["$"] * len(binary)
|
check1 = ["$"] * len(binary)
|
||||||
temp = []
|
temp = []
|
||||||
for i in range(len(binary)):
|
for i in range(len(binary)):
|
||||||
@ -46,19 +48,18 @@ def check(binary: list[str]) -> list[str]:
|
|||||||
binary = list(set(temp))
|
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])
|
>>> decimal_to_binary(3,[1.5])
|
||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
temp = []
|
temp = []
|
||||||
s = ""
|
for minterm in minterms:
|
||||||
for m in minterms:
|
string = ""
|
||||||
for i in range(no_of_variable):
|
for i in range(no_of_variable):
|
||||||
s = str(m % 2) + s
|
string = str(minterm % 2) + string
|
||||||
m //= 2
|
minterm //= 2
|
||||||
temp.append(s)
|
temp.append(string)
|
||||||
s = ""
|
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
|
|
||||||
@ -70,16 +71,13 @@ def is_for_table(string1: str, string2: str, count: int) -> bool:
|
|||||||
>>> is_for_table('01_','001',1)
|
>>> is_for_table('01_','001',1)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
l1 = list(string1)
|
list1 = list(string1)
|
||||||
l2 = list(string2)
|
list2 = list(string2)
|
||||||
count_n = 0
|
count_n = 0
|
||||||
for i in range(len(l1)):
|
for i in range(len(list1)):
|
||||||
if l1[i] != l2[i]:
|
if list1[i] != list2[i]:
|
||||||
count_n += 1
|
count_n += 1
|
||||||
if count_n == count:
|
return count_n == count
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
||||||
@ -108,7 +106,7 @@ def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
|||||||
for k in range(len(chart)):
|
for k in range(len(chart)):
|
||||||
chart[k][j] = 0
|
chart[k][j] = 0
|
||||||
temp.append(prime_implicants[i])
|
temp.append(prime_implicants[i])
|
||||||
while 1:
|
while True:
|
||||||
max_n = 0
|
max_n = 0
|
||||||
rem = -1
|
rem = -1
|
||||||
count_n = 0
|
count_n = 0
|
||||||
|
Loading…
Reference in New Issue
Block a user