Boolean algebra how to complete missing expressions

269 Views Asked by At

For example, AB + AB' = A and ABC + ABC' = AB. When the user enters AB, how can I find the missing letter and give it the form it was before it was simplified? In fact, what we need to do is multiply the missing letter and the missing(not) letter with the current expression. For example, A * (B + B').

Other examples

  • ABC + A'C' (B is missing) => (A'C' * (B + B')) + ABC

  • ABCD + AD'(B and C are missing) => ((AD' * (B+B')) * (C + C')) + ABCD

We assume that the user enters the letters in order. The output should be accordingly. First "A" then "B" then "C" then "D" and up to D at most. How can we solve this problem with python?

1

There are 1 best solutions below

0
md2perpe On

Here's some code that might help:

def split_into_terms(formula: str) -> list[str]:
    return [ t.strip() for t in formula.split('+') ]

def variables_in_term(term: str) -> set[str]:
    return set( c for c in term if c != "'" )

def missing_variables_in_term(term: str, all_variables: set[str]) -> set[str]:
    return all_variables.difference(variables_in_term(term))



if __name__ == '__main__':
    for term in split_into_terms("ABCD + AD'"):
        print(f"Missing variables in term {term}: {''.join(missing_variables_in_term(term, set('ABCD')))}")