mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
4d0c830d2c
* ci(pre-commit): Add ``flake8-builtins`` additional dependency to ``pre-commit`` (#7104) * refactor: Fix ``flake8-builtins`` (#7104) * fix(lru_cache): Fix naming conventions in docstrings (#7104) * ci(pre-commit): Order additional dependencies alphabetically (#7104) * fix(lfu_cache): Correct function name in docstring (#7104) * Update strings/snake_case_to_camel_pascal_case.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/stacks/next_greater_element.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update digital_image_processing/index_calculation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update graphs/prim.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update hashes/djb2.py Co-authored-by: Christian Clauss <cclauss@me.com> * refactor: Rename `_builtin` to `builtin_` ( #7104) * fix: Rename all instances (#7104) * refactor: Update variable names (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ci: Create ``tox.ini`` and ignore ``A003`` (#7123) * revert: Remove function name changes (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename tox.ini to .flake8 * Update data_structures/heap/heap.py Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> * refactor: Rename `next_` to `next_item` (#7104) * ci(pre-commit): Add `flake8` plugin `flake8-bugbear` (#7127) * refactor: Follow `flake8-bugbear` plugin (#7127) * fix: Correct `knapsack` code (#7127) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
172 lines
4.3 KiB
Python
172 lines
4.3 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Sequence
|
|
|
|
|
|
def compare_string(string1: str, string2: str) -> str:
|
|
"""
|
|
>>> compare_string('0010','0110')
|
|
'0_10'
|
|
|
|
>>> compare_string('0110','1101')
|
|
'X'
|
|
"""
|
|
list1 = list(string1)
|
|
list2 = list(string2)
|
|
count = 0
|
|
for i in range(len(list1)):
|
|
if list1[i] != list2[i]:
|
|
count += 1
|
|
list1[i] = "_"
|
|
if count > 1:
|
|
return "X"
|
|
else:
|
|
return "".join(list1)
|
|
|
|
|
|
def check(binary: list[str]) -> list[str]:
|
|
"""
|
|
>>> check(['0.00.01.5'])
|
|
['0.00.01.5']
|
|
"""
|
|
pi = []
|
|
while True:
|
|
check1 = ["$"] * len(binary)
|
|
temp = []
|
|
for i in range(len(binary)):
|
|
for j in range(i + 1, len(binary)):
|
|
k = compare_string(binary[i], binary[j])
|
|
if k != "X":
|
|
check1[i] = "*"
|
|
check1[j] = "*"
|
|
temp.append(k)
|
|
for i in range(len(binary)):
|
|
if check1[i] == "$":
|
|
pi.append(binary[i])
|
|
if len(temp) == 0:
|
|
return pi
|
|
binary = list(set(temp))
|
|
|
|
|
|
def decimal_to_binary(no_of_variable: int, minterms: Sequence[float]) -> list[str]:
|
|
"""
|
|
>>> decimal_to_binary(3,[1.5])
|
|
['0.00.01.5']
|
|
"""
|
|
temp = []
|
|
for minterm in minterms:
|
|
string = ""
|
|
for _ in range(no_of_variable):
|
|
string = str(minterm % 2) + string
|
|
minterm //= 2
|
|
temp.append(string)
|
|
return temp
|
|
|
|
|
|
def is_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 = 0
|
|
for i in range(len(list1)):
|
|
if list1[i] != list2[i]:
|
|
count_n += 1
|
|
return count_n == count
|
|
|
|
|
|
def selection(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)
|
|
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
|
|
if count == 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])
|
|
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
|
|
|
|
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
|
|
|
|
|
|
def prime_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 = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))]
|
|
for i in range(len(prime_implicants)):
|
|
count = prime_implicants[i].count("_")
|
|
for j in range(len(binary)):
|
|
if is_for_table(prime_implicants[i], binary[j], count):
|
|
chart[i][j] = 1
|
|
|
|
return chart
|
|
|
|
|
|
def main() -> None:
|
|
no_of_variable = int(input("Enter the no. of variables\n"))
|
|
minterms = [
|
|
float(x)
|
|
for x in input(
|
|
"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__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
main()
|