Check whether two Boolean expressions are equal for every input. The expressions use Python syntax: the variable names, and, or, not and brackets.
Input
A line with the variable names separated by spaces (1 to 5 names). Then two lines, each one expression.
Output
equivalent if they agree on every input. Otherwise different followed by the first assignment where they disagree, as name=value pairs separated by spaces. Assignments are tried in counting order: all 0 first, with the first variable as the most significant bit, so for A B the order is A=0 B=0, A=0 B=1, A=1 B=0, A=1 B=1.
Examples
A B not (A or B) (not A) and (not B)
equivalent
A B not (A and B) (not A) and (not B)
different A=0 B=1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use itertools.product([0, 1], repeat=n) to go through every assignment, in order.
Hint 2
Build a dictionary {name: value} and evaluate each expression with eval(expression, {}, values).
Hint 3
Compare the two results as booleans: bool(...) of each.
Solution
Show one correct solution (try it yourself first)
from itertools import product
names = input().split()
f = input()
g = input()
for bits in product([0, 1], repeat=len(names)):
values = dict(zip(names, bits))
if bool(eval(f, {}, values)) != bool(eval(g, {}, values)):
print("different", " ".join(name + "=" + str(bit) for name, bit in zip(names, bits)))
break
else:
print("equivalent")