|
1 |
| - package com.thealgorithms.bitmanipulation; |
| 1 | +package com.thealgorithms.bitmanipulation; |
2 | 2 |
|
3 |
| - import java.util.List; |
| 3 | +import java.util.List; |
4 | 4 |
|
5 |
| - /** |
6 |
| - * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR) |
7 |
| - */ |
8 |
| - public final class BooleanAlgebraGates { |
9 |
| - |
10 |
| - private BooleanAlgebraGates() { |
11 |
| - // Private constructor to prevent instantiation |
12 |
| - } |
| 5 | +/** |
| 6 | + * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR). |
| 7 | + */ |
| 8 | +public final class BooleanAlgebraGates { |
13 | 9 |
|
14 |
| - /** |
15 |
| - * Interface representing a Boolean gate that takes multiple inputs and returns a result. |
16 |
| - */ |
17 |
| - interface BooleanGate { |
18 |
| - boolean evaluate(List<Boolean> inputs); |
19 |
| - } |
| 10 | + private BooleanAlgebraGates() { |
| 11 | + // Private constructor to prevent instantiation |
| 12 | + } |
20 | 13 |
|
21 |
| - /** |
22 |
| - * AND Gate implementation. |
23 |
| - * Returns true if all inputs are true, otherwise false. |
24 |
| - * |
25 |
| - * Test cases for AND Gate: |
26 |
| - * |
27 |
| - * AND([true, true]) should return true. |
28 |
| - * AND([true, false]) should return false. |
29 |
| - * AND([false, false]) should return false. |
30 |
| - * |
31 |
| - * Multiple inputs: |
32 |
| - * AND([true, true, true]) should return true. |
33 |
| - * AND([true, false, true]) should return false. |
34 |
| - * |
35 |
| - * Edge case (empty input list): |
36 |
| - * AND([]) can either return true (since the conjunction of no values |
37 |
| - * is true in some interpretations) or throw an exception depending |
38 |
| - * your design. |
39 |
| - */ |
| 14 | + /** |
| 15 | + * Interface representing a Boolean gate that takes multiple inputs and returns a result. |
| 16 | + */ |
| 17 | + interface BooleanGate { |
| 18 | + boolean evaluate(List<Boolean> inputs); |
| 19 | + } |
40 | 20 |
|
41 |
| - static class ANDGate implements BooleanGate { |
42 |
| - @Override |
43 |
| - public boolean evaluate(List<Boolean> inputs) { |
44 |
| - for (boolean input : inputs) { |
45 |
| - if (!input) { |
46 |
| - return false; |
47 |
| - } |
| 21 | + /** |
| 22 | + * AND Gate implementation. |
| 23 | + * Returns true if all inputs are true; otherwise, false. |
| 24 | + * |
| 25 | + * Test cases for AND Gate: |
| 26 | + * - AND([true, true]) should return true. |
| 27 | + * - AND([true, false]) should return false. |
| 28 | + * - AND([false, false]) should return false. |
| 29 | + * - AND([true, true, true]) should return true. |
| 30 | + * - AND([true, false, true]) should return false. |
| 31 | + * |
| 32 | + * Edge case (empty input list): |
| 33 | + * - AND([]) can either return true or throw an exception depending on your design. |
| 34 | + */ |
| 35 | + static class ANDGate implements BooleanGate { |
| 36 | + @Override |
| 37 | + public boolean evaluate(List<Boolean> inputs) { |
| 38 | + for (boolean input : inputs) { |
| 39 | + if (!input) { |
| 40 | + return false; |
48 | 41 | }
|
49 |
| - return true; |
50 | 42 | }
|
| 43 | + return true; |
51 | 44 | }
|
| 45 | + } |
52 | 46 |
|
53 |
| - /** |
54 |
| - * OR Gate implementation. |
55 |
| - * Returns true if at least one input is true, otherwise false. |
56 |
| - * |
57 |
| - * Test cases for OR Gate: |
58 |
| - * |
59 |
| - * OR([true, false]) should return true. |
60 |
| - * OR([false, false]) should return false. |
61 |
| - * |
62 |
| - * Multiple inputs: |
63 |
| - * OR([true, true, false]) should return true. |
64 |
| - * OR([false, false, false]) should return false. |
65 |
| - * |
66 |
| - * Edge case (empty input list): |
67 |
| - * OR([]) can either return false (since the disjunction of no values |
68 |
| - * is false in some interpretations) or throw an exception. |
69 |
| - */ |
70 |
| - static class ORGate implements BooleanGate { |
71 |
| - @Override |
72 |
| - public boolean evaluate(List<Boolean> inputs) { |
73 |
| - for (boolean input : inputs) { |
74 |
| - if (input) { |
75 |
| - return true; |
76 |
| - } |
| 47 | + /** |
| 48 | + * OR Gate implementation. |
| 49 | + * Returns true if at least one input is true; otherwise, false. |
| 50 | + * |
| 51 | + * Test cases for OR Gate: |
| 52 | + * - OR([true, false]) should return true. |
| 53 | + * - OR([false, false]) should return false. |
| 54 | + * - OR([true, true, false]) should return true. |
| 55 | + * - OR([false, false, false]) should return false. |
| 56 | + * |
| 57 | + * Edge case (empty input list): |
| 58 | + * - OR([]) can either return false or throw an exception. |
| 59 | + */ |
| 60 | + static class ORGate implements BooleanGate { |
| 61 | + @Override |
| 62 | + public boolean evaluate(List<Boolean> inputs) { |
| 63 | + for (boolean input : inputs) { |
| 64 | + if (input) { |
| 65 | + return true; |
77 | 66 | }
|
78 |
| - return false; |
79 | 67 | }
|
| 68 | + return false; |
80 | 69 | }
|
| 70 | + } |
81 | 71 |
|
| 72 | + /** |
| 73 | + * NOT Gate implementation (Unary operation). |
| 74 | + * Only accepts a single input and returns the negation. |
| 75 | + * |
| 76 | + * Test cases for NOT Gate: |
| 77 | + * - NOT(true) should return false. |
| 78 | + * - NOT(false) should return true. |
| 79 | + * |
| 80 | + * Edge case: |
| 81 | + * Not applicable, as NOT is a unary operation and requires a single input. |
| 82 | + */ |
| 83 | + static class NOTGate { |
82 | 84 | /**
|
83 |
| - * NOT Gate implementation (Unary operation). |
84 |
| - * Only accepts a single input and returns the negation. |
85 |
| - * |
86 |
| - * Test cases for NOT Gate: |
87 |
| - * |
88 |
| - * NOT(true) should return false. |
89 |
| - * NOT(false) should return true. |
90 |
| - * |
91 |
| - * Edge case: |
92 |
| - * Not applicable, as NOT is a unary operation and requires a single |
93 |
| - * input. |
| 85 | + * Evaluates the negation of a single input. |
| 86 | + * |
| 87 | + * @param input The input value to be negated. |
| 88 | + * @return The negation of the input. |
94 | 89 | */
|
95 |
| - static class NOTGate { |
96 |
| - /** |
97 |
| - * Evaluates the negation of a single input. |
98 |
| - * |
99 |
| - * @param input The input value to be negated. |
100 |
| - * @return The negation of the input. |
101 |
| - */ |
102 |
| - public boolean evaluate(boolean input) { |
103 |
| - return !input; |
104 |
| - } |
| 90 | + public boolean evaluate(boolean input) { |
| 91 | + return !input; |
105 | 92 | }
|
| 93 | + } |
106 | 94 |
|
107 |
| - /** |
108 |
| - * XOR Gate implementation. |
109 |
| - * Returns true if an odd number of inputs are true, otherwise false. |
110 |
| - * |
111 |
| - * Test cases for XOR Gate: |
112 |
| - * |
113 |
| - * XOR([true, false]) should return true. |
114 |
| - * XOR([true, true]) should return false. |
115 |
| - * XOR([false, false]) should return false. |
116 |
| - * |
117 |
| - * Multiple inputs: |
118 |
| - * XOR([true, true, true]) should return true. |
119 |
| - * XOR([true, false, true]) should return false (since the XOR of an |
120 |
| - * odd number of true inputs is true). |
121 |
| - * |
122 |
| - * Edge case: |
123 |
| - * XOR([]) can either return false (if no inputs are given) or throw an |
124 |
| - * exception. |
125 |
| - */ |
126 |
| - static class XORGate implements BooleanGate { |
127 |
| - @Override |
128 |
| - public boolean evaluate(List<Boolean> inputs) { |
129 |
| - boolean result = false; |
130 |
| - for (boolean input : inputs) { |
131 |
| - result ^= input; |
132 |
| - } |
133 |
| - return result; |
| 95 | + /** |
| 96 | + * XOR Gate implementation. |
| 97 | + * Returns true if an odd number of inputs are true; otherwise, false. |
| 98 | + * |
| 99 | + * Test cases for XOR Gate: |
| 100 | + * - XOR([true, false]) should return true. |
| 101 | + * - XOR([true, true]) should return false. |
| 102 | + * - XOR([false, false]) should return false. |
| 103 | + * - XOR([true, true, true]) should return true. |
| 104 | + * - XOR([true, false, true]) should return false. |
| 105 | + * |
| 106 | + * Edge case: |
| 107 | + * - XOR([]) can either return false or throw an exception. |
| 108 | + */ |
| 109 | + static class XORGate implements BooleanGate { |
| 110 | + @Override |
| 111 | + public boolean evaluate(List<Boolean> inputs) { |
| 112 | + boolean result = false; |
| 113 | + for (boolean input : inputs) { |
| 114 | + result ^= input; |
134 | 115 | }
|
| 116 | + return result; |
135 | 117 | }
|
| 118 | + } |
136 | 119 |
|
137 |
| - /** |
138 |
| - * NAND Gate implementation. |
139 |
| - * Returns true if at least one input is false, otherwise false. |
140 |
| - * |
141 |
| - * Test cases for NAND Gate: |
142 |
| - * |
143 |
| - * NAND([true, true]) should return false. |
144 |
| - * NAND([true, false]) should return true. |
145 |
| - * NAND([false, false]) should return true. |
146 |
| - * |
147 |
| - * Multiple inputs: |
148 |
| - * NAND([true, true, true]) should return false. |
149 |
| - * NAND([true, true, false]) should return true. |
150 |
| - * |
151 |
| - * Edge case: |
152 |
| - * NAND([]) can either return true or throw an exception depending on |
153 |
| - * your design. |
154 |
| - */ |
155 |
| - static class NANDGate implements BooleanGate { |
156 |
| - @Override |
157 |
| - public boolean evaluate(List<Boolean> inputs) { |
158 |
| - return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND |
159 |
| - } |
| 120 | + /** |
| 121 | + * NAND Gate implementation. |
| 122 | + * Returns true if at least one input is false; otherwise, false. |
| 123 | + * |
| 124 | + * Test cases for NAND Gate: |
| 125 | + * - NAND([true, true]) should return false. |
| 126 | + * - NAND([true, false]) should return true. |
| 127 | + * - NAND([false, false]) should return true. |
| 128 | + * - NAND([true, true, true]) should return false. |
| 129 | + * - NAND([true, true, false]) should return true. |
| 130 | + * |
| 131 | + * Edge case: |
| 132 | + * - NAND([]) can either return true or throw an exception. |
| 133 | + */ |
| 134 | + static class NANDGate implements BooleanGate { |
| 135 | + @Override |
| 136 | + public boolean evaluate(List<Boolean> inputs) { |
| 137 | + return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND |
160 | 138 | }
|
| 139 | + } |
161 | 140 |
|
162 |
| - /** |
163 |
| - * NOR Gate implementation. |
164 |
| - * Returns true if all inputs are false, otherwise false. |
165 |
| - * |
166 |
| - * Test cases for NOR Gate: |
167 |
| - * |
168 |
| - * NOR([true, false]) should return false. |
169 |
| - * NOR([false, false]) should return true. |
170 |
| - * |
171 |
| - * Multiple inputs: |
172 |
| - * NOR([false, false, false]) should return true. |
173 |
| - * NOR([true, true, false]) should return false. |
174 |
| - * |
175 |
| - * Edge case: |
176 |
| - * NOR([]) can either return true or throw an exception. |
177 |
| - */ |
178 |
| - static class NORGate implements BooleanGate { |
179 |
| - @Override |
180 |
| - public boolean evaluate(List<Boolean> inputs) { |
181 |
| - return !new ORGate().evaluate(inputs); // Equivalent to negation of OR |
182 |
| - } |
| 141 | + /** |
| 142 | + * NOR Gate implementation. |
| 143 | + * Returns true if all inputs are false; otherwise, false. |
| 144 | + * |
| 145 | + * Test cases for NOR Gate: |
| 146 | + * - NOR([true, false]) should return false. |
| 147 | + * - NOR([false, false]) should return true. |
| 148 | + * - NOR([false, false, false]) should return true. |
| 149 | + * - NOR([true, true, false]) should return false. |
| 150 | + * |
| 151 | + * Edge case: |
| 152 | + * - NOR([]) can either return true or throw an exception. |
| 153 | + */ |
| 154 | + static class NORGate implements BooleanGate { |
| 155 | + @Override |
| 156 | + public boolean evaluate(List<Boolean> inputs) { |
| 157 | + return !new ORGate().evaluate(inputs); // Equivalent to negation of OR |
183 | 158 | }
|
184 |
| - /** |
185 |
| - * Edge Cases and Special Scenarios: |
186 |
| - * |
187 |
| - * 1. Empty input list: |
188 |
| - * - Test handling of empty input lists for multi-input gates. |
189 |
| - * - Should throw an exception if the design assumes an empty list is * invalid. |
190 |
| - * |
191 |
| - * 2. Single input for multi-input gates: |
192 |
| - * - AND([true]) -> true |
193 |
| - * - OR([false]) -> false |
194 |
| - * - XOR([true]) -> true |
195 |
| - * - Test behavior with single input as a corner case. |
196 |
| - * |
197 |
| - * 3. Mixed inputs: |
198 |
| - * - AND([true, false, false, true]) -> false |
199 |
| - * - Similar tests for OR, XOR, and other gates. |
200 |
| - * |
201 |
| - * 4. Large input lists: |
202 |
| - * - AND([true, true, ..., true]) with 1,000 true values -> true. |
203 |
| - * - OR with mostly false and one true -> true. |
204 |
| - * |
205 |
| - * 5. Randomized tests: |
206 |
| - * - Generate random true/false input lists. |
207 |
| - * - Validate expected gate outputs, especially for XOR. |
208 |
| - * |
209 |
| - * 6. Invalid input handling: |
210 |
| - * - Test behavior when null is passed as input or within the list. |
211 |
| - * - Should either throw an exception or handle per defined behavior. |
212 |
| - */ |
213 |
| - |
214 | 159 | }
|
| 160 | + |
| 161 | + /** |
| 162 | + * Edge Cases and Special Scenarios: |
| 163 | + * |
| 164 | + * 1. Empty input list: |
| 165 | + * - Test handling of empty input lists for multi-input gates. |
| 166 | + * - Should throw an exception if the design assumes an empty list is invalid. |
| 167 | + * |
| 168 | + * 2. Single input for multi-input gates: |
| 169 | + * - AND([true]) -> true |
| 170 | + * - OR([false]) -> false |
| 171 | + * - XOR([true]) -> true |
| 172 | + * - Test behavior with single input as a corner case. |
| 173 | + * |
| 174 | + * 3. Mixed inputs: |
| 175 | + * - AND([true, false, false, true]) -> false |
| 176 | + * - Similar tests for OR, XOR, and other gates. |
| 177 | + * |
| 178 | + * 4. Large input lists: |
| 179 | + * - AND([true, true, ..., true]) with 1,000 true values -> true. |
| 180 | + * - OR with mostly false and one true -> true. |
| 181 | + * |
| 182 | + * 5. Randomized tests: |
| 183 | + * - Generate random true/false input lists. |
| 184 | + * - Validate expected gate outputs, especially for XOR. |
| 185 | + * |
| 186 | + * 6. Invalid input handling: |
| 187 | + * - Test behavior when null is passed as input or within the list. |
| 188 | + * - Should either throw an exception or handle per defined behavior. |
| 189 | + */ |
| 190 | +} |
0 commit comments