TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

StringChecker

Problem statement, definition, constraints, and public examples.

Problem Statement

In this problem you are to determine if a String represents a number, and return an error message if it doesn't. For the purposes of this problem, a String represents a number if and only if it conforms to the following:
A number must be an optional '-', followed by a sequence of zero or more digits, followed by an optional '.' followed by another sequence of zero or more digits, followed by an optional exponent.
If there is an exponent, there must be at least 1 digit before it. If there is no exponent, there must be at least 1 digit in the String.
The exponent must be represented as an 'e' or an 'E', followed by an optional '+' or '-' (not both) followed by a sequence of one or more digits.

If the given String is not a number, then you are to return an error message explaining why it is not a number, as follows. If the String is not a number for multiple reasons, you are to return the applicable error message that appears first on this list:
If the String contains any other characters other than digits ('0'-'9'), 'e', 'E', '.', '+' and '-' you are to return "INVALID CHARACTERS".
If the String contains more than one '.' you are to return "MULTIPLE DECIMAL POINTS".
If the String contains more than one 'e', more than one 'E', or both an 'e' and an 'E', return "INVALID EXPONENT".
If the String contains a '+' or a '-' where it should not be (a '+' should only come immediately after an 'e' or an 'E', and a '-' should only come after an 'e' or an 'E', or at the very beginning of the String), return "INVALID SIGN".
If the String is invalid for any other reason return "INVALID".
If the String is a valid number return "".

Definition

Class:
StringChecker
Method:
isNumber
Parameters:
String
Returns:
String
Method signature:
String isNumber(String num)
(be sure your method is public)

Constraints

  • num contains between 0 and 50 characters, inclusive.
  • Each character of num is a letter ('a'-'z' or 'A'-'Z'), a digit ('0'-'9'), a '+', a '-', a '.', or a space (' ').

Examples

  1. "100.45E-8"
    Returns: ""
  2. "100.e8"
    Returns: ""
  3. "-.45E0"
    Returns: ""
  4. "100.45E-"
    Returns: "INVALID"
  5. "23"
    Returns: ""
  6. ".E1"
    Returns: "INVALID"
  7. "45.32.32"
    Returns: "MULTIPLE DECIMAL POINTS"
  8. "this is not a number"
    Returns: "INVALID CHARACTERS"
  9. "5E1e10"
    Returns: "INVALID EXPONENT"
  10. "43.-32"
    Returns: "INVALID SIGN"
Back to all problems