Problem Statement
*** WARNING: Do not leave the competition room while you are competing. If you have a question, use "admins: question" in the chat area. ***
This problem will involve writing the logic for a cash register. You will be given a
String input that represents the data entered into the cash register. If properly formatted,
input will look like:
- <Amount>+<Amount>+ ... +<Amount>=
- or
- <Amount>=
where <Amount> represents some non-negative integer. In other words,
input should be a list of non-negative integers delimited by single plus signs ('+') and terminated by a single equals sign ('=').
input should not have any characters after equals sign. If
input is formatted incorrectly, you will return -1. If
input is formatted correctly, you will return the sum of the <Amount>s. Note, the <Amount>s may have leadings zeros.
Definition
- Class:
- Register
- Method:
- compute
- Parameters:
- String
- Returns:
- int
- Method signature:
- int compute(String input)
- (be sure your method is public)
Constraints
- input will contain between 2 and 50 characters inclusive.
- Each character in input will be either a digit ('0'-'9'), a plus sign ('+') or an equals sign ('=').
- The returned value will be between -1 and 1000000 inclusive.
Examples
"10+20="
Returns: 30
input is valid and the sum is 30
"100="
Returns: 100
input is valid and the sum is 100
"9999999999999999999999"
Returns: -1
No equals sign
"1+2+3+4+5+6+7+8+9+10="
Returns: 55
input is valid with sum 55
"=1+2+3+4="
Returns: -1
Too many equals signs
"1++2+3+4="
Returns: -1
Too many pluses
"0001+0002+0003="
Returns: 6
Watch out for leading zeros
This problem statement is the exclusive and proprietary property of Topcoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of Topcoder, Inc. is strictly prohibited. (c)2024, Topcoder, Inc. All rights reserved.
Back to all problems