Skip to main content
added 284 characters in body
Source Link
gervais.b
  • 2.1k
  • 11
  • 13

Do you have considered to use only one stack ?

You can wrap your numbers into a constant entry that returns the number. And create one operation entry for each valid operation. So that you can "just" reduce your stack until she has one item.

Stack<Function<Integer, Integer>> operations = new Stack<>();


public Integer resolve(final Integer x) {
    Integer right = x;
    while ( !operations.isEmpty() ) {
        right = operations.pop().apply(right);
    }
    return right;
}

Do you have considered to use only one stack ?

You can wrap your numbers into a constant entry that returns the number. And create one operation entry for each valid operation. So that you can "just" reduce your stack until she has one item.

Do you have considered to use only one stack ?

You can wrap your numbers into a constant entry that returns the number. And create one operation entry for each valid operation. So that you can "just" reduce your stack until she has one item.

Stack<Function<Integer, Integer>> operations = new Stack<>();


public Integer resolve(final Integer x) {
    Integer right = x;
    while ( !operations.isEmpty() ) {
        right = operations.pop().apply(right);
    }
    return right;
}
Source Link
gervais.b
  • 2.1k
  • 11
  • 13

Do you have considered to use only one stack ?

You can wrap your numbers into a constant entry that returns the number. And create one operation entry for each valid operation. So that you can "just" reduce your stack until she has one item.