-
Notifications
You must be signed in to change notification settings - Fork 921
/
P46.java
37 lines (32 loc) · 1.5 KB
/
P46.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.shekhargulati.ninetynine_problems._03_logic_and_codes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
/**
* 3.01 (**) Truth tables for logical expressions.
* <p>
* Define predicates and/2, or/2, nand/2, nor/2, xor/2, impl/2 and equ/2 (for logical equivalence) which succeed or fail according to the result of their respective operations; e.g. and(A,B) will succeed, if and only if both A and B succeed. Note that A and B can be Prolog goals (not only the constants true and fail).
* <p>
* A logical expression in two variables can then be written in prefix notation, as in the following example: and(or(A,B),nand(A,B)).
* Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables.
*/
public class P46 {
public static String table(BiPredicate<Boolean, Boolean> f) {
List<String> resultBuilder = new ArrayList<>();
resultBuilder.add("A B result");
for (boolean a : Arrays.asList(true, false)) {
for (boolean b : Arrays.asList(true, false)) {
resultBuilder.add(String.format("%-10s %-10s %s", a, b, f.test(a, b)));
}
}
return resultBuilder.stream().collect(Collectors.joining("\n"));
}
public static boolean and(boolean a, boolean b) {
return a && b;
}
public static boolean or(boolean a, boolean b) {
return a || b;
}
}