mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-05 02:40:16 +00:00
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
|
"""
|
||
|
https://en.wikipedia.org/wiki/Karnaugh_map
|
||
|
https://www.allaboutcircuits.com/technical-articles/karnaugh-map-boolean-algebraic-simplification-technique
|
||
|
"""
|
||
|
|
||
|
|
||
|
def simplify_kmap(kmap: list[list[int]]) -> str:
|
||
|
"""
|
||
|
Simplify the Karnaugh map.
|
||
|
>>> simplify_kmap(kmap=[[0, 1], [1, 1]])
|
||
|
"A'B + AB' + AB"
|
||
|
>>> simplify_kmap(kmap=[[0, 0], [0, 0]])
|
||
|
''
|
||
|
>>> simplify_kmap(kmap=[[0, 1], [1, -1]])
|
||
|
"A'B + AB' + AB"
|
||
|
>>> simplify_kmap(kmap=[[0, 1], [1, 2]])
|
||
|
"A'B + AB' + AB"
|
||
|
>>> simplify_kmap(kmap=[[0, 1], [1, 1.1]])
|
||
|
"A'B + AB' + AB"
|
||
|
>>> simplify_kmap(kmap=[[0, 1], [1, 'a']])
|
||
|
"A'B + AB' + AB"
|
||
|
"""
|
||
|
simplified_f = []
|
||
|
for a, row in enumerate(kmap):
|
||
|
for b, item in enumerate(row):
|
||
|
if item:
|
||
|
term = ("A" if a else "A'") + ("B" if b else "B'")
|
||
|
simplified_f.append(term)
|
||
|
return " + ".join(simplified_f)
|
||
|
|
||
|
|
||
|
def main() -> None:
|
||
|
"""
|
||
|
Main function to create and simplify a K-Map.
|
||
|
|
||
|
>>> main()
|
||
|
[0, 1]
|
||
|
[1, 1]
|
||
|
Simplified Expression:
|
||
|
A'B + AB' + AB
|
||
|
"""
|
||
|
kmap = [[0, 1], [1, 1]]
|
||
|
|
||
|
# Manually generate the product of [0, 1] and [0, 1]
|
||
|
|
||
|
for row in kmap:
|
||
|
print(row)
|
||
|
|
||
|
print("Simplified Expression:")
|
||
|
print(simplify_kmap(kmap))
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|
||
|
print(f"{simplify_kmap(kmap=[[0, 1], [1, 1]]) = }")
|