|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | + |
| 4 | +def compare_string(string1: str, string2: str) -> str: |
| 5 | + """ |
| 6 | + >>> compare_string('0010','0110') |
| 7 | + '0_10' |
| 8 | + >>> compare_string('0110','1101') |
| 9 | + 'X' |
| 10 | + """ |
| 11 | + l1 = list(string1) |
| 12 | + l2 = list(string2) |
| 13 | + count = 0 |
| 14 | + for i in range(len(l1)): |
| 15 | + if l1[i] != l2[i]: |
| 16 | + count += 1 |
| 17 | + l1[i] = "_" |
| 18 | + if count > 1: |
| 19 | + return "X" |
| 20 | + else: |
| 21 | + return "".join(l1) |
| 22 | + |
| 23 | + |
| 24 | +def check(binary: list[str]) -> list[str]: |
| 25 | + """ |
| 26 | + >>> check(['0.00.01.5']) |
| 27 | + ['0.00.01.5'] |
| 28 | + """ |
| 29 | + pi = [] |
| 30 | + while 1: |
| 31 | + check1 = ["$"] * len(binary) |
| 32 | + temp = [] |
| 33 | + for i in range(len(binary)): |
| 34 | + for j in range(i + 1, len(binary)): |
| 35 | + k = compare_string(binary[i], binary[j]) |
| 36 | + if k != "X": |
| 37 | + check1[i] = "*" |
| 38 | + check1[j] = "*" |
| 39 | + temp.append(k) |
| 40 | + for i in range(len(binary)): |
| 41 | + if check1[i] == "$": |
| 42 | + pi.append(binary[i]) |
| 43 | + if len(temp) == 0: |
| 44 | + return pi |
| 45 | + binary = list(set(temp)) |
| 46 | + |
| 47 | + |
| 48 | +def decimal_to_binary(no_of_variable: int, minterms: list[float]) -> list[str]: |
| 49 | + """ |
| 50 | + >>> decimal_to_binary(3,[1.5]) |
| 51 | + ['0.00.01.5'] |
| 52 | + """ |
| 53 | + temp = [] |
| 54 | + s = "" |
| 55 | + for m in minterms: |
| 56 | + for i in range(no_of_variable): |
| 57 | + s = str(m % 2) + s |
| 58 | + m //= 2 |
| 59 | + temp.append(s) |
| 60 | + s = "" |
| 61 | + return temp |
| 62 | + |
| 63 | + |
| 64 | +def is_for_table(string1: str, string2: str, count: int) -> bool: |
| 65 | + """ |
| 66 | + >>> is_for_table('__1','011',2) |
| 67 | + True |
| 68 | + >>> is_for_table('01_','001',1) |
| 69 | + False |
| 70 | + """ |
| 71 | + l1 = list(string1) |
| 72 | + l2 = list(string2) |
| 73 | + count_n = 0 |
| 74 | + for i in range(len(l1)): |
| 75 | + if l1[i] != l2[i]: |
| 76 | + count_n += 1 |
| 77 | + if count_n == count: |
| 78 | + return True |
| 79 | + else: |
| 80 | + return False |
| 81 | + |
| 82 | + |
| 83 | +def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]: |
| 84 | + """ |
| 85 | + >>> selection([[1]],['0.00.01.5']) |
| 86 | + ['0.00.01.5'] |
| 87 | + >>> selection([[1]],['0.00.01.5']) |
| 88 | + ['0.00.01.5'] |
| 89 | + """ |
| 90 | + temp = [] |
| 91 | + select = [0] * len(chart) |
| 92 | + for i in range(len(chart[0])): |
| 93 | + count = 0 |
| 94 | + rem = -1 |
| 95 | + for j in range(len(chart)): |
| 96 | + if chart[j][i] == 1: |
| 97 | + count += 1 |
| 98 | + rem = j |
| 99 | + if count == 1: |
| 100 | + select[rem] = 1 |
| 101 | + for i in range(len(select)): |
| 102 | + if select[i] == 1: |
| 103 | + for j in range(len(chart[0])): |
| 104 | + if chart[i][j] == 1: |
| 105 | + for k in range(len(chart)): |
| 106 | + chart[k][j] = 0 |
| 107 | + temp.append(prime_implicants[i]) |
| 108 | + while 1: |
| 109 | + max_n = 0 |
| 110 | + rem = -1 |
| 111 | + count_n = 0 |
| 112 | + for i in range(len(chart)): |
| 113 | + count_n = chart[i].count(1) |
| 114 | + if count_n > max_n: |
| 115 | + max_n = count_n |
| 116 | + rem = i |
| 117 | + |
| 118 | + if max_n == 0: |
| 119 | + return temp |
| 120 | + |
| 121 | + temp.append(prime_implicants[rem]) |
| 122 | + |
| 123 | + for i in range(len(chart[0])): |
| 124 | + if chart[rem][i] == 1: |
| 125 | + for j in range(len(chart)): |
| 126 | + chart[j][i] = 0 |
| 127 | + |
| 128 | + |
| 129 | +def prime_implicant_chart( |
| 130 | + prime_implicants: list[str], binary: list[str] |
| 131 | +) -> list[list[int]]: |
| 132 | + """ |
| 133 | + >>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5']) |
| 134 | + [[1]] |
| 135 | + """ |
| 136 | + chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] |
| 137 | + for i in range(len(prime_implicants)): |
| 138 | + count = prime_implicants[i].count("_") |
| 139 | + for j in range(len(binary)): |
| 140 | + if is_for_table(prime_implicants[i], binary[j], count): |
| 141 | + chart[i][j] = 1 |
| 142 | + |
| 143 | + return chart |
| 144 | + |
| 145 | + |
| 146 | +def main(): |
| 147 | + no_of_variable = int(input("Enter the no. of variables\n")) |
| 148 | + minterms = [ |
| 149 | + int(x) |
| 150 | + for x in input( |
| 151 | + "Enter the decimal representation of Minterms 'Spaces Separated'\n" |
| 152 | + ).split() |
| 153 | + ] |
| 154 | + binary = decimal_to_binary(no_of_variable, minterms) |
| 155 | + |
| 156 | + prime_implicants = check(binary) |
| 157 | + print("Prime Implicants are:") |
| 158 | + print(prime_implicants) |
| 159 | + chart = prime_implicant_chart(prime_implicants, binary) |
| 160 | + |
| 161 | + essential_prime_implicants = selection(chart, prime_implicants) |
| 162 | + print("Essential Prime Implicants are:") |
| 163 | + print(essential_prime_implicants) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + import doctest |
| 168 | + |
| 169 | + doctest.testmod() |
| 170 | + main() |
0 commit comments