-2

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?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
ageow
  • 21
  • 4
  • 3
    Do you know how to solve this problem _without_ python, i.e. with just pencil and paper? – John Gordon Dec 23 '21 at 19:37
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") Please be aware this site can help solve specific, technical problems, not open-ended requests. Please edit your question to show what you have tried so far. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") and ["How do I ask and answer homework questions?"](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions "How do I ask and answer homework questions?") pages for details on how to best help us help you. – itprorh66 Dec 23 '21 at 20:11
  • There are *lots* of possible expressions that are equivalent to `AB`, even restricting yourself to ones that must include all of some set of terms. – chepner Dec 23 '21 at 20:39
  • Do you do any parsing of the string currently? – md2perpe Dec 23 '21 at 21:01
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 01 '22 at 23:53

1 Answers1

0

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')))}")
md2perpe
  • 3,372
  • 2
  • 18
  • 22