Skip to content

Instantly share code, notes, and snippets.

@rizemon
Last active May 14, 2021 16:32
Show Gist options
  • Save rizemon/5363d9d0fe549dd6e1f8fce62963305b to your computer and use it in GitHub Desktop.
Save rizemon/5363d9d0fe549dd6e1f8fce62963305b to your computer and use it in GitHub Desktop.
Reorders literals for easier comparison with output
from rich.console import Console
from rich.table import Table
import sys
from itertools import product
from math import ceil
def main():
if len(sys.argv) != 3:
print("Command: python3 column.py <literals> <minterms>")
print("e.g python3 column.py A,D,B,C 4,7,3,1")
sys.exit(1)
table = Table(show_lines=True)
var_list = sys.argv[1].split(",")
sop_list = [int(_) for _ in sys.argv[2].split(",")]
binary_format = "".join("{" + _ + "}" for _ in sorted(var_list))
size = ceil(len(var_list) / 2)
for name in " ".join(var_list).split(" ")[:size]:
table.add_column(name, style="cyan")
for name in " ".join(var_list).split(" ")[size:]:
table.add_column(name, style="green")
table.add_column("F", style="yellow")
for i in list(product([0,1], repeat=len(var_list))):
exec(f"{','.join(var_list)} = {i}", globals())
pattern = eval(f"f'{binary_format}'")
truth = int(pattern,2)
row = [str(r) for r in list(i) + [1 if truth in sop_list else 0]]
table.add_row( *row )
console = Console()
console.print(table)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment