Skip to content
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions boolean_algebra/karnaugh_map_simplification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# https://www.allaboutcircuits.com/technical-articles/karnaugh-map-boolean-algebraic-simplification-technique/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# https://www.allaboutcircuits.com/technical-articles/karnaugh-map-boolean-algebraic-simplification-technique/
"""
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 K-Map.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Simplify the K-Map.
Simplify the Karnaugh map.


>>> kmap = [[0, 1], [1, 1]]
>>> simplify_kmap(kmap)
"A'B + AB' + AB"
"""
simplified_f = []
for a in range(2):
for b in range(2):
if kmap[a][b]:
Copy link
Member

@cclauss cclauss Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for a in range(2):
for b in range(2):
if kmap[a][b]:
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)

simplified_expression = simplify_kmap(kmap)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
simplified_expression = simplify_kmap(kmap)
print(f"{simplify_kmap(kmap=[[0, 1], [1, 1]]) = }")
simplified_expression = simplify_kmap(kmap)

print("Simplified Expression:")
print(simplified_expression)


if __name__ == "__main__":
main()