Skip to content

Create karnaugh_map_simplification.py #11056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 29, 2023
Merged
Changes from 1 commit
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
Next Next commit
Create karnaugh_map_simplification.py
  • Loading branch information
Khushi-Shukla authored Oct 28, 2023
commit a72eabcc12b66ac7e7ed7649ec7bfc137ba6b796
57 changes: 57 additions & 0 deletions boolean_algebra/karnaugh_map_simplification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#https://www.allaboutcircuits.com/technical-articles/karnaugh-map-boolean-algebraic-simplification-technique/

def F(A: int, B: int) -> int:
"""
Define the function F(A, B).

>>> F(0, 0)
0
>>> F(1, 1)
1
"""
return A and (not B) or (A and B) or (A and B)

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]:
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 for _ in range(2)] for _ in range(2)]

# Manually generate the product of [0, 1] and [0, 1]
for A in [0, 1]:
for B in [0, 1]:
kmap[A][B] = F(A, B)

for row in kmap:
print(row)

simplified_expression = simplify_kmap(kmap)
print("\nSimplified Expression:")
print(simplified_expression)

if __name__ == "__main__":
main()