Skip to content

refactor: InfixToPostfix #5401

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 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/main/java/com/thealgorithms/stacks/InfixToPostfix.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.thealgorithms.stacks;

import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class InfixToPostfix {
private InfixToPostfix() {
}

public static void main(String[] args) throws Exception {
assert "32+".equals(infix2PostFix("3+2"));
assert "123++".equals(infix2PostFix("1+(2+3)"));
assert "34+5*6-".equals(infix2PostFix("(3+4)*5-6"));
}

public static String infix2PostFix(String infixExpression) throws Exception {
if (!BalancedBrackets.isBalanced(infixExpression)) {
if (!BalancedBrackets.isBalanced(filterBrackets(infixExpression))) {
throw new Exception("invalid expression");
}
StringBuilder output = new StringBuilder();
Expand Down Expand Up @@ -55,4 +51,10 @@ private static int precedence(char operator) {
return -1;
}
}

private static String filterBrackets(String input) {
Pattern pattern = Pattern.compile("[^(){}\\[\\]<>]");
Matcher matcher = pattern.matcher(input);
return matcher.replaceAll("");
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/thealgorithms/stacks/InfixToPostfixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.thealgorithms.stacks;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

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;

class InfixToPostfixTest {

@ParameterizedTest
@MethodSource("provideValidExpressions")
void testValidExpressions(String infix, String expectedPostfix) throws Exception {
assertEquals(expectedPostfix, InfixToPostfix.infix2PostFix(infix));
}

private static Stream<Arguments> provideValidExpressions() {
return Stream.of(Arguments.of("3+2", "32+"), Arguments.of("1+(2+3)", "123++"), Arguments.of("(3+4)*5-6", "34+5*6-"));
}

@ParameterizedTest
@MethodSource("provideInvalidExpressions")
void testInvalidExpressions(String infix, String expectedMessage) {
Exception exception = assertThrows(Exception.class, () -> InfixToPostfix.infix2PostFix(infix));
assertEquals(expectedMessage, exception.getMessage());
}

private static Stream<Arguments> provideInvalidExpressions() {
return Stream.of(Arguments.of("((a+b)*c-d", "invalid expression"));
}
}