Skip to content

Commit c7a1331

Browse files
Khushi-Shuklapre-commit-ci[bot]cclauss
authoredOct 29, 2023
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>
1 parent 760d9be commit c7a1331

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
https://en.wikipedia.org/wiki/Karnaugh_map
3+
https://www.allaboutcircuits.com/technical-articles/karnaugh-map-boolean-algebraic-simplification-technique
4+
"""
5+
6+
7+
def simplify_kmap(kmap: list[list[int]]) -> str:
8+
"""
9+
Simplify the Karnaugh map.
10+
>>> simplify_kmap(kmap=[[0, 1], [1, 1]])
11+
"A'B + AB' + AB"
12+
>>> simplify_kmap(kmap=[[0, 0], [0, 0]])
13+
''
14+
>>> simplify_kmap(kmap=[[0, 1], [1, -1]])
15+
"A'B + AB' + AB"
16+
>>> simplify_kmap(kmap=[[0, 1], [1, 2]])
17+
"A'B + AB' + AB"
18+
>>> simplify_kmap(kmap=[[0, 1], [1, 1.1]])
19+
"A'B + AB' + AB"
20+
>>> simplify_kmap(kmap=[[0, 1], [1, 'a']])
21+
"A'B + AB' + AB"
22+
"""
23+
simplified_f = []
24+
for a, row in enumerate(kmap):
25+
for b, item in enumerate(row):
26+
if item:
27+
term = ("A" if a else "A'") + ("B" if b else "B'")
28+
simplified_f.append(term)
29+
return " + ".join(simplified_f)
30+
31+
32+
def main() -> None:
33+
"""
34+
Main function to create and simplify a K-Map.
35+
36+
>>> main()
37+
[0, 1]
38+
[1, 1]
39+
Simplified Expression:
40+
A'B + AB' + AB
41+
"""
42+
kmap = [[0, 1], [1, 1]]
43+
44+
# Manually generate the product of [0, 1] and [0, 1]
45+
46+
for row in kmap:
47+
print(row)
48+
49+
print("Simplified Expression:")
50+
print(simplify_kmap(kmap))
51+
52+
53+
if __name__ == "__main__":
54+
main()
55+
print(f"{simplify_kmap(kmap=[[0, 1], [1, 1]]) = }")

0 commit comments

Comments
 (0)
Please sign in to comment.