Create karnaugh_map_simplification.py (#11056)

* Create karnaugh_map_simplification.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update karnaugh_map_simplification.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update karnaugh_map_simplification.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update karnaugh_map_simplification.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update karnaugh_map_simplification.py

* Update boolean_algebra/karnaugh_map_simplification.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update karnaugh_map_simplification.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update karnaugh_map_simplification.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update karnaugh_map_simplification.py

* Update karnaugh_map_simplification.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Khushi Shukla 2023-10-30 02:50:57 +05:30 committed by GitHub
parent 760d9bedc1
commit c7a1331b34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,55 @@
"""
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]]) = }")