mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 23:15:17 +08:00
Implement Parentheses Generator (#5096)
* chore: add `ParenthesesGenerator` to `DIRECTORY.md` * feat: implement Parentheses Generator * ref: change `ParenthesesGenerator`s method to `static` * ref: use parametrized tests * ref: handling exception when `n < 0` * chore: update docstrings * ref: make `ParenthesesGenerator` to be a proper utility * chore(docs): add private constructor docstring * ref(tests): move bad name suggestions * style: remove reduntant comments --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -0,0 +1,33 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class ParenthesesGeneratorTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource("regularInputStream")
|
||||
void regularInputTests(int input, List<String> expected) {
|
||||
assertEquals(expected, ParenthesesGenerator.generateParentheses(input));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("negativeInputStream")
|
||||
void throwsForNegativeInputTests(int input) {
|
||||
assertThrows(IllegalArgumentException.class, () -> ParenthesesGenerator.generateParentheses(input));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> regularInputStream() {
|
||||
return Stream.of(Arguments.of(0, List.of("")), Arguments.of(1, List.of("()")), Arguments.of(2, List.of("(())", "()()")), Arguments.of(3, List.of("((()))", "(()())", "(())()", "()(())", "()()()")),
|
||||
Arguments.of(4, List.of("(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()")));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> negativeInputStream() {
|
||||
return Stream.of(Arguments.of(-1), Arguments.of(-5), Arguments.of(-10));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user