BACKGROUND
Numbers are often used to measure quantities, which means that the units of
measurement are important for the correct interpretation of the numeric value.
For example, 10 meters per second is different than 10 miles per hour, even
though both values measure speed. When performing arithmetic on values, the
units can be manipulated algebraically (consider meters, seconds, miles, and
hours to be unknown variables that cannot be simplified). Addition and
subtraction are only possible if the powers of the units agree exactly. In
multiplication, the power of each unit in the result is equal to the sum of the
powers of the unit in the two operands. In division, the power of each unit in
the result is equal to the power in the numerator minus the power in the
denominator (with the usual interpretation of 0 or negative powers).
PROBLEM DESCRIPTION
Create a class UnitsAlgebra that contains a method evaluate. The method should
take three Strings representing (in order) the left hand value, the operation
to perform, and the right hand value. The method should return a String
representing the result of the operation or "INVALID" if the operation is not
possible.
Values will be specified as an integral number from 1 to 1000 (inclusive)
followed by a space, followed by between 0 and 5 (inclusive) units
declarations. A units declaration is composed of a single lower-case letter
('a'-'z') followed by a '^' followed by a number in the range of -9 to 9
inclusive with no leading zeros. "f^1", "a^0", and "z^-9" are proper units
declarations. There will be no spaces between units declarations. TopCoder
will verify that the values are properly constructed.
Units will always be specified in increasing alphabetical order, and the same
unit will not be specified twice.
"10 f^1", "99 m^1s^-1", and "67 " are properly constructed values. "42" is not
correct because it does not have a space following the value. "1 z^1y^1" is
not correct because the units are not specified in increasing alphabetical order.
The operator will be either +, -, *, or /. Division should round down to the
nearest integer. Note that because values are constrained to be positive,
division is always defined.
The result should be formatted in the same way as the input values, with the
exception that there may be more than 5 units declarations, the value may be
zero or negative, and the powers of the individual units may be greater than 9
or less than -9. If the operation is not possible (addition or subtraction of
values that do not have the same units), your method should return the String
"INVALID". You should not include units in the output whose power is 0.
DEFINITION
Class: UnitsAlgebra
Parameters: String, String, String
Returns: String
Method signature (be sure your method is public): String evaluate(String lhs,
String op, String rhs);
NOTES
Addition and subtraction are only possible if the powers of all units are
identical, and the powers of the units of the result should be the same as the
powers of the units of the operands.
Multiplication is always possible, and for each unit in the output the power
should be the sum of the powers of that unit in the two operands.
Division is always possible, and for each unit in the output the power should
be equal to the power of that unit in the left-hand operand minus the power of
that unit in the right-hand operand.
INPUT CONSTRAINTS
A properly constructed operand is composed of an integral value from 1 to 1000
(inclusive) with no leading zeros, followed by a space followed by at most 5 (0
to 5 inclusive) units declarations in strictly increasing order (alphabetic by
the letter in the units declaration). A units declaration is composed of a
single lower case letter (a-z inclusive) followed by a caret (^) followed by an
optional minus sign (-) followed by a single numeric digit (0-9 inclusive).
TopCoder will verify that both lhs and rhs are properly constructed operands
and that op is equal to one of "+", "-", "*", and "/".
EXAMPLES
lhs="32 f^1s^-2", op="*", and rhs="10 s^1" should return "320 f^1s^-1"
lhs="10 m^1s^-1", op="+", and rhs="10 f^1s^-1" should return "INVALID"
lhs="10 x^4", op="-", and rhs="10 x^4" should return "0 x^4"
lhs="100 x^3y^2z^1", op="/", and rhs="10 x^2y^2z^2" should return "10 x^1z^-1"
lhs="10 ", op="*", and rhs="15 " should return "150 "
lhs="99 p^9", op="/", and rhs="100 p^-9" should return "0 p^18"
lhs="2 x^0", op="+", and rhs="2 y^0" should return "4 "