An implication p → q comes with three relatives. Your program evaluates all four for given values of p and q.
What to build
A program that prints the value of p → q, its converse, its inverse and its contrapositive for several pairs of truth values.
Requirements
- Read the number of cases t, then t lines with two letters, each T or F, giving p and q.
- For each case print four letters T or F, separated by spaces: p → q, the converse q → p, the inverse ¬p → ¬q and the contrapositive ¬q → ¬p, in that order.
Input
The first line has t. Each of the next t lines has the values of p and q, such as T F.
Output
One line of four letters for each case.
Constraints
1 ≤ t ≤ 20.
Example
Input:
2
T F
F T
Output:
F T T F
T F F T
Why it works
An implication is false only when its first part is true and its second part is false. The contrapositive always has the same value as the original, and the inverse always has the same value as the converse, which is why the first and last letters, and the second and third, can differ in pairs.
Examples
2 T F F T
F T T F T F F T
1 T T
T T T T
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The implication is (not p) or q. Write a small function implies(a, b) and reuse it for the other statements.
Hint 2
The converse is implies(q, p), the inverse is implies(not p, not q), the contrapositive is implies(not q, not p).
Hint 3
Print T or F, separated by single spaces.
Solution
Show one correct solution (try it yourself first)
def implies(a, b):
return (not a) or b
def letter(x):
return "T" if x else "F"
t = int(input())
for _ in range(t):
a, b = input().split()
p, q = a == "T", b == "T"
print(letter(implies(p, q)), letter(implies(q, p)), letter(implies(not p, not q)), letter(implies(not q, not p)))
▶ Open this solution in the compiler, then press Visualize to watch it run step by step