Fix reverse polish stack argument order and add non-symmetric evaluation tests

Co-Authored-By: td1223
This commit is contained in:
Gleb Koval 2024-11-05 19:05:33 +00:00
parent 38e373bdfc
commit d463251ff1
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
3 changed files with 22 additions and 4 deletions

View File

@ -26,12 +26,12 @@ public class ReversePolishStack {
} }
IntOperator intOperator = stack.pop(); IntOperator intOperator = stack.pop();
Stack<Integer> args = new Stack<>(); List<Integer> args = new LinkedList<>();
while (args.size() < intOperator.getOperands()) { while (args.size() < intOperator.getOperands()) {
if (stack.isEmpty()) { if (stack.isEmpty()) {
throw new ArithmeticException("Not enough arguments to evaluate operator"); throw new ArithmeticException("Not enough arguments to evaluate operator");
} }
args.push(evaluateNext()); args.addFirst(evaluateNext());
} }
return intOperator.evaluate(args); return intOperator.evaluate(args);
} }

View File

@ -15,6 +15,12 @@ public class IntOperatorTest {
assertThat(intOperator.evaluate(List.of(1, 2)), is(3)); assertThat(intOperator.evaluate(List.of(1, 2)), is(3));
} }
@Test
public void intOperatorCanEvaluateNonSymmetric() {
IntOperator intOperator = new IntOperator("-", 2, args -> args.get(0) - args.get(1));
assertThat(intOperator.evaluate(List.of(1, 2)), is(-1));
}
@Test @Test
public void intOperatorThrowsOnInvalidArguments() { public void intOperatorThrowsOnInvalidArguments() {
IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1)); IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1));

View File

@ -30,6 +30,18 @@ public class ReversePolishStackTest {
assertThat(reversePolishStack.evaluate(), is(35)); assertThat(reversePolishStack.evaluate(), is(35));
} }
@Test
public void canEvaluateReversePolishStackNonSymmetric() {
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
reversePolishStack.push(new IntOperator("25", 0, args -> 25));
reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1)));
reversePolishStack.push(new IntOperator("9", 0, args -> 9));
reversePolishStack.push(new IntOperator("1", 0, args -> 1));
reversePolishStack.push(new IntOperator("-", 2, args -> args.get(0) - args.get(1)));
reversePolishStack.push(new IntOperator("*", 2, args -> args.get(0) * args.get(1)));
assertThat(reversePolishStack.evaluate(), is(280));
}
@Test @Test
public void evaluatePushesBack() { public void evaluatePushesBack() {
reversePolishStack.push(new IntOperator("10", 0, args -> 10)); reversePolishStack.push(new IntOperator("10", 0, args -> 10));
@ -57,8 +69,8 @@ public class ReversePolishStackTest {
reversePolishStack.push(new IntOperator("10", 0, args -> 10)); reversePolishStack.push(new IntOperator("10", 0, args -> 10));
reversePolishStack.push(new IntOperator("25", 0, args -> 25)); reversePolishStack.push(new IntOperator("25", 0, args -> 25));
reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1))); reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1)));
reversePolishStack.push(new IntOperator("1", 0, args -> 10)); reversePolishStack.push(new IntOperator("1", 0, args -> 1));
reversePolishStack.push(new IntOperator("9", 0, args -> 25)); reversePolishStack.push(new IntOperator("9", 0, args -> 9));
reversePolishStack.push(new IntOperator("-", 2, args -> args.get(0) - args.get(1))); reversePolishStack.push(new IntOperator("-", 2, args -> args.get(0) - args.get(1)));
reversePolishStack.push(new IntOperator("*", 2, args -> args.get(0) * args.get(1))); reversePolishStack.push(new IntOperator("*", 2, args -> args.get(0) * args.get(1)));
assertThat(reversePolishStack.toString(), is("10 25 + 1 9 - *")); assertThat(reversePolishStack.toString(), is("10 25 + 1 9 - *"));