TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Polish

Problem statement, definition, constraints, and public examples.

Problem Statement

PROBLEM STATEMENT

In Polish notation, an expression consists of an operator followed by its
operands.  For example, consider the following expressions in standard notation:

5 * 2 + -3
5 * (2 + -3)

The Polish notation equivalents of these expressions, respectively, are: 

+ * 5 2 -3
* 5 + 2 -3

Implement a class, Polish, which contains a method, evaluate, that calculates
an expression which is given in valid Polish notation.

DEFINITION
Class name: Polish
Method name: evaluate
Parameters: String
Return type: int

Method signature: int evaluate(String expr); Be sure your method is public.

TopCoder will ensure the input is valid.  Inputs are valid if the following
criteria are met:
* The String is given in valid Polish notation.  An expression is in valid
Polish notation if:
   - there is exactly one less operator than the number of operands
   - the first expression is not an operand
   - the last two expressions are operands
* An expression will not be more than 21 characters.
* Operands will be between -100 and 100, inclusive.
* Operators can only be a plus ('+'), minus ('-') or multiplication sign ('*').

NOTES
* There will be at least one space between all operands and operators in the
input string.
* There may be leading and trailing spaces in the input string.
* Unary minus signs should be considered to be part of the integer values, not
operators, and will not be separated from the values they modify by a space. 

EXAMPLES

1. Given the expression "+ * 5 2 -3" as input, the integer value 7 will be
returned.

2. Given the expression "* 5 + 2 -3" as input, the integer value -5 will be
returned.

3. Given the expression "+ * - + 24 5 29 5 -7" as input, the integer value -7
will be returned.

Definition

Class:
Polish
Method:
evaluate
Parameters:
String
Returns:
int
Method signature:
int evaluate(String param0)
(be sure your method is public)

Constraints

    Examples

      No public sample cases were explicitly marked for this problem in the original data.

      Back to all problems