Are two formulas equivalent?

Two formulas are equivalent when they agree in every row of the truth table. Your program checks that, and shows a row that separates them when they are not equivalent.

What to build

A program that compares two formulas in p, q and r.

Requirements

  • Read two lines, each a Python expression using the names p, q and r, and the operators and, or, not, == and !=.
  • Try the 8 rows in the order TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF (the order of p, q, r).
  • If the formulas agree in all rows print equivalent. Otherwise print different: followed by the first row where they differ, such as different: p=T q=F r=T.

Input

Two lines, one formula each.

Output

One line.

Constraints

Each formula has at most 60 characters and uses only p, q and r.

Example

Input:

not (p and q)
(not p) or (not q)

Output:

equivalent

Why it works

A truth table with 2 to the power 3 rows holds every case, so agreeing in all of them is exactly logical equivalence. The first differing row is a counterexample that proves the formulas are not equivalent.

Examples

Example 1 · Input
not (p and q)
(not p) or (not q)
Expected output
equivalent
Example 2 · Input
(not p) or q
(not q) or p
Expected output
different: p=T q=F r=T

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Loop over product([True, False], repeat=3) and evaluate both formulas with eval(formula, dict(p=..., q=..., r=...)).

Hint 2

Stop at the first row where the two values differ and print it.

Hint 3

Write the row as p=T q=F r=T, using T or F for each value.

Solution

Show one correct solution (try it yourself first)
from itertools import product

a = input()
b = input()
letter = lambda x: "T" if x else "F"
answer = "equivalent"
for p, q, r in product([True, False], repeat=3):
    env = {"p": p, "q": q, "r": r}
    if bool(eval(a, env)) != bool(eval(b, env)):
        answer = f"different: p={letter(p)} q={letter(q)} r={letter(r)}"
        break
print(answer)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Logical equivalence →