2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2021-02-04 16:58:29 +00:00
|
|
|
|
2022-07-11 08:19:52 +00:00
|
|
|
from collections.abc import Sequence
|
2022-11-06 14:54:44 +00:00
|
|
|
from typing import Literal
|
2021-12-16 09:28:31 +00:00
|
|
|
|
2021-02-04 16:58:29 +00:00
|
|
|
|
2022-11-06 14:54:44 +00:00
|
|
|
def compare_string(string1: str, string2: str) -> str | Literal[False]:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> compare_string('0010','0110')
|
|
|
|
'0_10'
|
2019-11-16 07:05:00 +00:00
|
|
|
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> compare_string('0110','1101')
|
2022-11-06 14:54:44 +00:00
|
|
|
False
|
2019-11-21 14:21:40 +00:00
|
|
|
"""
|
2021-12-16 09:28:31 +00:00
|
|
|
list1 = list(string1)
|
|
|
|
list2 = list(string2)
|
2019-10-05 05:14:13 +00:00
|
|
|
count = 0
|
2021-12-16 09:28:31 +00:00
|
|
|
for i in range(len(list1)):
|
|
|
|
if list1[i] != list2[i]:
|
2019-10-05 05:14:13 +00:00
|
|
|
count += 1
|
2021-12-16 09:28:31 +00:00
|
|
|
list1[i] = "_"
|
2019-10-05 05:14:13 +00:00
|
|
|
if count > 1:
|
2022-11-06 14:54:44 +00:00
|
|
|
return False
|
2019-10-05 05:14:13 +00:00
|
|
|
else:
|
2021-12-16 09:28:31 +00:00
|
|
|
return "".join(list1)
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def check(binary: list[str]) -> list[str]:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> check(['0.00.01.5'])
|
|
|
|
['0.00.01.5']
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
pi = []
|
2021-12-16 09:28:31 +00:00
|
|
|
while True:
|
2019-10-05 05:14:13 +00:00
|
|
|
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])
|
2022-11-06 14:54:44 +00:00
|
|
|
if k is False:
|
2019-10-05 05:14:13 +00:00
|
|
|
check1[i] = "*"
|
|
|
|
check1[j] = "*"
|
2022-11-06 14:54:44 +00:00
|
|
|
temp.append("X")
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(len(binary)):
|
|
|
|
if check1[i] == "$":
|
|
|
|
pi.append(binary[i])
|
|
|
|
if len(temp) == 0:
|
|
|
|
return pi
|
|
|
|
binary = list(set(temp))
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-12-16 09:28:31 +00:00
|
|
|
def decimal_to_binary(no_of_variable: int, minterms: Sequence[float]) -> list[str]:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> decimal_to_binary(3,[1.5])
|
|
|
|
['0.00.01.5']
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
temp = []
|
2021-12-16 09:28:31 +00:00
|
|
|
for minterm in minterms:
|
|
|
|
string = ""
|
2022-10-13 16:03:06 +00:00
|
|
|
for _ in range(no_of_variable):
|
2021-12-16 09:28:31 +00:00
|
|
|
string = str(minterm % 2) + string
|
|
|
|
minterm //= 2
|
|
|
|
temp.append(string)
|
2019-10-05 05:14:13 +00:00
|
|
|
return temp
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-10-16 06:11:52 +00:00
|
|
|
def is_for_table(string1: str, string2: str, count: int) -> bool:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> is_for_table('__1','011',2)
|
|
|
|
True
|
2019-11-16 07:05:00 +00:00
|
|
|
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> is_for_table('01_','001',1)
|
|
|
|
False
|
|
|
|
"""
|
2021-12-16 09:28:31 +00:00
|
|
|
list1 = list(string1)
|
|
|
|
list2 = list(string2)
|
2023-08-15 23:10:22 +00:00
|
|
|
count_n = sum(item1 != item2 for item1, item2 in zip(list1, list2))
|
2021-12-16 09:28:31 +00:00
|
|
|
return count_n == count
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> selection([[1]],['0.00.01.5'])
|
|
|
|
['0.00.01.5']
|
2019-11-16 07:05:00 +00:00
|
|
|
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> selection([[1]],['0.00.01.5'])
|
|
|
|
['0.00.01.5']
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
temp = []
|
|
|
|
select = [0] * len(chart)
|
|
|
|
for i in range(len(chart[0])):
|
2023-08-15 23:10:22 +00:00
|
|
|
count = sum(row[i] == 1 for row in chart)
|
2019-10-05 05:14:13 +00:00
|
|
|
if count == 1:
|
2023-08-15 23:10:22 +00:00
|
|
|
rem = max(j for j, row in enumerate(chart) if row[i] == 1)
|
2019-10-05 05:14:13 +00:00
|
|
|
select[rem] = 1
|
2023-08-15 23:10:22 +00:00
|
|
|
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])
|
2021-12-16 09:28:31 +00:00
|
|
|
while True:
|
2023-08-15 23:10:22 +00:00
|
|
|
counts = [chart[i].count(1) for i in range(len(chart))]
|
|
|
|
max_n = max(counts)
|
|
|
|
rem = counts.index(max_n)
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
if max_n == 0:
|
|
|
|
return temp
|
|
|
|
|
|
|
|
temp.append(prime_implicants[rem])
|
|
|
|
|
2023-08-15 23:10:22 +00:00
|
|
|
for j in range(len(chart[0])):
|
|
|
|
if chart[rem][j] != 1:
|
|
|
|
continue
|
|
|
|
for i in range(len(chart)):
|
|
|
|
chart[i][j] = 0
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
|
2021-02-04 16:58:29 +00:00
|
|
|
def prime_implicant_chart(
|
2021-09-07 11:37:03 +00:00
|
|
|
prime_implicants: list[str], binary: list[str]
|
|
|
|
) -> list[list[int]]:
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-11-21 14:21:40 +00:00
|
|
|
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
|
|
|
|
[[1]]
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
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
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-10-22 09:45:19 +00:00
|
|
|
def main() -> None:
|
2019-10-05 05:14:13 +00:00
|
|
|
no_of_variable = int(input("Enter the no. of variables\n"))
|
|
|
|
minterms = [
|
2021-10-22 09:45:19 +00:00
|
|
|
float(x)
|
2019-10-05 05:14:13 +00:00
|
|
|
for x in input(
|
2020-09-14 12:40:27 +00:00
|
|
|
"Enter the decimal representation of Minterms 'Spaces Separated'\n"
|
2019-10-05 05:14:13 +00:00
|
|
|
).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()
|