TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Register

Problem statement, definition, constraints, and public examples.

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

  1. "10+20="
    Returns: 30
    input is valid and the sum is 30
  2. "100="
    Returns: 100
    input is valid and the sum is 100
  3. "9999999999999999999999"
    Returns: -1
    No equals sign
  4. "1+2+3+4+5+6+7+8+9+10="
    Returns: 55
    input is valid with sum 55
  5. "=1+2+3+4="
    Returns: -1
    Too many equals signs
  6. "1++2+3+4="
    Returns: -1
    Too many pluses
  7. "0001+0002+0003="
    Returns: 6
    Watch out for leading zeros
  8. "100+100+="
    Returns: -1
Back to all problems