Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import java.util.Stack;
- public class SimplifyExpressionTest extends TestCase {
- private static String simplifyExpression(final String inputExpression) {
- final StringBuilder simplifiedExpression = new StringBuilder();
- // Implementation logic here:
- final Stack<Boolean> signStack = new Stack<>();
- boolean positiveSign = true;
- signStack.push(positiveSign);
- for (final char chr : inputExpression.toCharArray()) {
- // >>>---> Super Secret Implementation Logic Here. <---<<< //
- }
- return simplifiedExpression.toString();
- }
- public void test1() {
- assertSimpifiedExpressionEquals("-a-b-c", "-(a+b+c)");
- }
- public void test2() {
- assertSimpifiedExpressionEquals("a-b-c+d", "a-b-(c-d)");
- }
- public void test3() {
- assertSimpifiedExpressionEquals("a-b+c+d-e", "a-(b-(c+d)+e)");
- }
- public void test4() {
- assertSimpifiedExpressionEquals("a-b+c+d-e+f-g", "a-(b-(c+d)+(e-f)+g)");
- }
- public void tests() {
- assertSimpifiedExpressionEquals("a", "a");
- assertSimpifiedExpressionEquals("a", "+a");
- assertSimpifiedExpressionEquals("-a", "-a");
- assertSimpifiedExpressionEquals("a+b", "a+b");
- assertSimpifiedExpressionEquals("a-b", "a-b");
- assertSimpifiedExpressionEquals("-a-b", "-(a+b)");
- assertSimpifiedExpressionEquals("-a+b", "-(a-b)");
- assertSimpifiedExpressionEquals("-a", "-(a)");
- assertSimpifiedExpressionEquals("a", "-(-(a))");
- assertSimpifiedExpressionEquals("-a", "-(-(-(a)))");
- assertSimpifiedExpressionEquals("-a+b-c+d-e", "-(a-(b-(c-(d-e))))");
- assertSimpifiedExpressionEquals("-a+b-c+d", "-(a-(b)+(c-d))");
- }
- private static void assertSimpifiedExpressionEquals(final String expectedSimpifiedExpression, final String inputExpression) {
- assertEquals("For expression <" + inputExpression + ">;", expectedSimpifiedExpression, simplifyExpression(inputExpression));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement