Skip to content

Commit bf87f5f

Browse files
authored
Merge branch 'master' into test/RegexMatchingTest
2 parents 61dc6e9 + 4347f5b commit bf87f5f

File tree

4 files changed

+68
-37
lines changed

4 files changed

+68
-37
lines changed

src/main/java/com/thealgorithms/stacks/InfixToPostfix.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.thealgorithms.stacks;
22

33
import java.util.Stack;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
46

57
public final class InfixToPostfix {
68
private InfixToPostfix() {
79
}
810

9-
public static void main(String[] args) throws Exception {
10-
assert "32+".equals(infix2PostFix("3+2"));
11-
assert "123++".equals(infix2PostFix("1+(2+3)"));
12-
assert "34+5*6-".equals(infix2PostFix("(3+4)*5-6"));
13-
}
14-
1511
public static String infix2PostFix(String infixExpression) throws Exception {
16-
if (!BalancedBrackets.isBalanced(infixExpression)) {
12+
if (!BalancedBrackets.isBalanced(filterBrackets(infixExpression))) {
1713
throw new Exception("invalid expression");
1814
}
1915
StringBuilder output = new StringBuilder();
@@ -55,4 +51,10 @@ private static int precedence(char operator) {
5551
return -1;
5652
}
5753
}
54+
55+
private static String filterBrackets(String input) {
56+
Pattern pattern = Pattern.compile("[^(){}\\[\\]<>]");
57+
Matcher matcher = pattern.matcher(input);
58+
return matcher.replaceAll("");
59+
}
5860
}

src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
import java.util.function.BiFunction;
66

77
/**
8-
* @brief Utility class evaluating postix expressions, cf. https://en.wikipedia.org/wiki/Reverse_Polish_notation
9-
* @details The computation is done using Integers.
8+
* Utility class for evaluating postfix expressions using integer arithmetic.
9+
* <p>
10+
* Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators follow their operands.
11+
* This class provides a method to evaluate expressions written in postfix notation.
12+
* </p>
13+
* <p>
14+
* For more information on postfix notation, refer to
15+
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation (RPN) on Wikipedia</a>.
16+
* </p>
1017
*/
1118
public final class StackPostfixNotation {
1219
private StackPostfixNotation() {
@@ -55,7 +62,7 @@ private static void consumeExpression(Stack<Integer> s, final String exp) {
5562
* @exception IllegalArgumentException exp is not a valid postix expression.
5663
*/
5764
public static int postfixEvaluate(final String exp) {
58-
Stack<Integer> s = new Stack<Integer>();
65+
Stack<Integer> s = new Stack<>();
5966
consumeExpression(s, exp);
6067
if (s.size() != 1) {
6168
throw new IllegalArgumentException("exp is not a proper postfix expression.");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
class InfixToPostfixTest {
12+
13+
@ParameterizedTest
14+
@MethodSource("provideValidExpressions")
15+
void testValidExpressions(String infix, String expectedPostfix) throws Exception {
16+
assertEquals(expectedPostfix, InfixToPostfix.infix2PostFix(infix));
17+
}
18+
19+
private static Stream<Arguments> provideValidExpressions() {
20+
return Stream.of(Arguments.of("3+2", "32+"), Arguments.of("1+(2+3)", "123++"), Arguments.of("(3+4)*5-6", "34+5*6-"));
21+
}
22+
23+
@ParameterizedTest
24+
@MethodSource("provideInvalidExpressions")
25+
void testInvalidExpressions(String infix, String expectedMessage) {
26+
Exception exception = assertThrows(Exception.class, () -> InfixToPostfix.infix2PostFix(infix));
27+
assertEquals(expectedMessage, exception.getMessage());
28+
}
29+
30+
private static Stream<Arguments> provideInvalidExpressions() {
31+
return Stream.of(Arguments.of("((a+b)*c-d", "invalid expression"));
32+
}
33+
}
Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
package com.thealgorithms.stacks;
22

3-
import static java.util.Map.entry;
43
import static org.junit.jupiter.api.Assertions.assertEquals;
54
import static org.junit.jupiter.api.Assertions.assertThrows;
65

7-
import java.util.Map;
8-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
910

1011
public class StackPostfixNotationTest {
11-
@Test
12-
public void testEvaluate() {
13-
final Map<String, Integer> testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("-5 -2 -", -3), entry("5 2 + 3 *", 21), entry("-5", -5));
14-
for (final var tc : testCases.entrySet()) {
15-
assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey()));
16-
}
17-
}
18-
19-
@Test
20-
public void testIfEvaluateThrowsExceptionForEmptyInput() {
21-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(""));
22-
}
2312

24-
@Test
25-
public void testIfEvaluateThrowsExceptionForInproperInput() {
26-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3"));
13+
@ParameterizedTest
14+
@MethodSource("provideValidTestCases")
15+
void testEvaluate(String expression, int expected) {
16+
assertEquals(expected, StackPostfixNotation.postfixEvaluate(expression));
2717
}
2818

29-
@Test
30-
public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
31-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
19+
static Stream<Arguments> provideValidTestCases() {
20+
return Stream.of(Arguments.of("1 1 +", 2), Arguments.of("2 3 *", 6), Arguments.of("6 2 /", 3), Arguments.of("-5 -2 -", -3), Arguments.of("5 2 + 3 *", 21), Arguments.of("-5", -5));
3221
}
3322

34-
@Test
35-
public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsA() {
36-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("+"));
23+
@ParameterizedTest
24+
@MethodSource("provideInvalidTestCases")
25+
void testEvaluateThrowsException(String expression) {
26+
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(expression));
3727
}
3828

39-
@Test
40-
public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsB() {
41-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("2 +"));
29+
static Stream<Arguments> provideInvalidTestCases() {
30+
return Stream.of(Arguments.of(""), Arguments.of("3 3 3"), Arguments.of("3 3 !"), Arguments.of("+"), Arguments.of("2 +"));
4231
}
4332
}

0 commit comments

Comments
 (0)