-
Notifications
You must be signed in to change notification settings - Fork 20k
Add algo for BooleanGateslogic #5717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e5dae59
Add algo for BooleanGateslogic
VANSH3104 5dfa700
Add algo for BooleanGateslogic and test cases
VANSH3104 c7eacae
Add algo for BooleanGateslogic and test cases
VANSH3104 aa2daea
Add algo for BooleanGateslogic and test cases
VANSH3104 52efb3e
Merge branch 'master' into VANSH3104/5680
VANSH3104 a250a95
Add algo for BooleanGateslogic and test cases
VANSH3104 e083d42
Merge branch 'VANSH3104/5680' of https://github.com/VANSH3104/Java in…
VANSH3104 5023e8a
Add algo for BooleanGateslogic and test cases
VANSH3104 e0dc8f5
Add algo for BooleanGateslogic and test cases
VANSH3104 a70868a
Add algo for BooleanGateslogic and test cases
VANSH3104 880976b
Add algo for BooleanGateslogic and test cases
VANSH3104 7f0733b
Add algo for BooleanGateslogic and test cases
VANSH3104 e32089a
Add algo for BooleanGateslogic and test cases and spaces
VANSH3104 b374db8
Add algo for BooleanGateslogic and test cases and spaces
VANSH3104 2fc4858
Add algo for BooleanGateslogic and test cases and spaces
VANSH3104 6209077
Add algo for BooleanGateslogic and test cases and spaces and booleans
VANSH3104 1b24895
Merge branch 'master' into VANSH3104/5680
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.thealgorithms.bitmanipulation; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR) | ||
*/ | ||
public final class BooleanAlgebraGates { | ||
|
||
private BooleanAlgebraGates() { | ||
// Prevent instantiation | ||
} | ||
|
||
/** | ||
* Represents a Boolean gate that takes multiple inputs and returns a result. | ||
*/ | ||
interface BooleanGate { | ||
/** | ||
* Evaluates the gate with the given inputs. | ||
* | ||
* @param inputs The input values for the gate. | ||
* @return The result of the evaluation. | ||
*/ | ||
boolean evaluate(List<Boolean> inputs); | ||
} | ||
|
||
/** | ||
* AND Gate implementation. | ||
* Returns true if all inputs are true; otherwise, false. | ||
*/ | ||
static class ANDGate implements BooleanGate { | ||
@Override | ||
public boolean evaluate(List<Boolean> inputs) { | ||
for (boolean input : inputs) { | ||
if (!input) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* OR Gate implementation. | ||
* Returns true if at least one input is true; otherwise, false. | ||
*/ | ||
static class ORGate implements BooleanGate { | ||
@Override | ||
public boolean evaluate(List<Boolean> inputs) { | ||
for (boolean input : inputs) { | ||
if (input) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* NOT Gate implementation (Unary operation). | ||
* Negates a single input value. | ||
*/ | ||
static class NOTGate { | ||
/** | ||
* Evaluates the negation of the input. | ||
* | ||
* @param input The input value to be negated. | ||
* @return The negated value. | ||
*/ | ||
public boolean evaluate(boolean input) { | ||
return !input; | ||
} | ||
} | ||
|
||
/** | ||
* XOR Gate implementation. | ||
* Returns true if an odd number of inputs are true; otherwise, false. | ||
*/ | ||
static class XORGate implements BooleanGate { | ||
@Override | ||
public boolean evaluate(List<Boolean> inputs) { | ||
boolean result = false; | ||
for (boolean input : inputs) { | ||
result ^= input; | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
/** | ||
* NAND Gate implementation. | ||
* Returns true if at least one input is false; otherwise, false. | ||
*/ | ||
static class NANDGate implements BooleanGate { | ||
@Override | ||
public boolean evaluate(List<Boolean> inputs) { | ||
return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND | ||
} | ||
} | ||
|
||
/** | ||
* NOR Gate implementation. | ||
* Returns true if all inputs are false; otherwise, false. | ||
*/ | ||
static class NORGate implements BooleanGate { | ||
@Override | ||
public boolean evaluate(List<Boolean> inputs) { | ||
return !new ORGate().evaluate(inputs); // Equivalent to negation of OR | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.thealgorithms.bitmanipulation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ANDGate; | ||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.BooleanGate; | ||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NANDGate; | ||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NORGate; | ||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.NOTGate; | ||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.ORGate; | ||
import com.thealgorithms.bitmanipulation.BooleanAlgebraGates.XORGate; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class BooleanAlgebraGatesTest { | ||
|
||
@ParameterizedTest(name = "ANDGate Test Case {index}: inputs={0} -> expected={1}") | ||
@MethodSource("provideAndGateTestCases") | ||
void testANDGate(List<Boolean> inputs, boolean expected) { | ||
BooleanGate gate = new ANDGate(); | ||
assertEquals(expected, gate.evaluate(inputs)); | ||
} | ||
|
||
@ParameterizedTest(name = "ORGate Test Case {index}: inputs={0} -> expected={1}") | ||
@MethodSource("provideOrGateTestCases") | ||
void testORGate(List<Boolean> inputs, boolean expected) { | ||
BooleanGate gate = new ORGate(); | ||
assertEquals(expected, gate.evaluate(inputs)); | ||
} | ||
|
||
@ParameterizedTest(name = "NOTGate Test Case {index}: input={0} -> expected={1}") | ||
@CsvSource({"true, false", "false, true"}) | ||
void testNOTGate(boolean input, boolean expected) { | ||
NOTGate gate = new NOTGate(); | ||
assertEquals(expected, gate.evaluate(input)); | ||
} | ||
|
||
@ParameterizedTest(name = "XORGate Test Case {index}: inputs={0} -> expected={1}") | ||
@MethodSource("provideXorGateTestCases") | ||
void testXORGate(List<Boolean> inputs, boolean expected) { | ||
BooleanGate gate = new XORGate(); | ||
assertEquals(expected, gate.evaluate(inputs)); | ||
} | ||
|
||
@ParameterizedTest(name = "NANDGate Test Case {index}: inputs={0} -> expected={1}") | ||
@MethodSource("provideNandGateTestCases") | ||
void testNANDGate(List<Boolean> inputs, boolean expected) { | ||
BooleanGate gate = new NANDGate(); | ||
assertEquals(expected, gate.evaluate(inputs)); | ||
} | ||
|
||
@ParameterizedTest(name = "NORGate Test Case {index}: inputs={0} -> expected={1}") | ||
@MethodSource("provideNorGateTestCases") | ||
void testNORGate(List<Boolean> inputs, boolean expected) { | ||
BooleanGate gate = new NORGate(); | ||
assertEquals(expected, gate.evaluate(inputs)); | ||
} | ||
|
||
// Helper methods to provide test data for each gate | ||
|
||
static Stream<Object[]> provideAndGateTestCases() { | ||
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, | ||
new Object[] {Collections.emptyList(), Boolean.TRUE} // AND over no inputs is true | ||
); | ||
} | ||
|
||
static Stream<Object[]> provideOrGateTestCases() { | ||
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.TRUE}, | ||
new Object[] {Collections.emptyList(), Boolean.FALSE} // OR over no inputs is false | ||
); | ||
} | ||
|
||
static Stream<Object[]> provideXorGateTestCases() { | ||
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // XOR over odd true | ||
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // XOR over single true | ||
new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE), Boolean.FALSE}, // XOR over all false | ||
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE} // XOR over even true | ||
); | ||
} | ||
|
||
static Stream<Object[]> provideNandGateTestCases() { | ||
return Stream.of(new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NAND of all true is false | ||
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.FALSE), Boolean.TRUE}, // NAND with one false is true | ||
new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NAND of all false is true | ||
new Object[] {Collections.emptyList(), Boolean.FALSE} // NAND over no inputs is false (negation of AND) | ||
); | ||
} | ||
|
||
static Stream<Object[]> provideNorGateTestCases() { | ||
return Stream.of(new Object[] {Arrays.asList(Boolean.FALSE, Boolean.FALSE), Boolean.TRUE}, // NOR of all false is true | ||
new Object[] {Arrays.asList(Boolean.FALSE, Boolean.TRUE), Boolean.FALSE}, // NOR with one true is false | ||
new Object[] {Arrays.asList(Boolean.TRUE, Boolean.TRUE), Boolean.FALSE}, // NOR of all true is false | ||
new Object[] {Collections.emptyList(), Boolean.TRUE} // NOR over no inputs is true (negation of OR) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.