Statistics

Problem Statement for "MathDebug"

Problem Statement

You are writing the embedded software to make sure a calculator functions properly. This is a special calculator that allows simple arithmetic with very large integers. The following rules are in place for PROPER USAGE (quotes used for clarity):

1) A valid calculation consists of <NUMBER><OPERATOR><NUMBER><EQUALS> where:

  • <NUMBER> is a string of one or more digits('0'..'9')
  • <OPERATOR> is one of '+','-','*','/'
  • <EQUALS> is '='

2) The calculator can perform a sequence of valid calculations in a row.

  • So 98+01234=234*1230=000/000= is valid where
  • 98+01234= is the first calculation,
  • 234*1230= is the second and
  • 000/000= is the last.

Embedded logic is needed to determine what the next key-press should be. Your function will return what key-press the calculator can accept next according to the following schema (quotes used for clarity):

Next key-press                                : Return
--------------------------------------------------------
Digit ('0'..'9')                             : 1
Operator ('+','-','*','/')                   : 2
Digit or Operator ('0'..'9','+','-','*','/') : 3
Equals ('=')                                 : 4
Digit or Equals ('0'..'9','=')               : 5

For example, if you have:
"47"
The next key-press could either be a Digit or an Operator so you would return 3.

If the input given doesn't follow the above laws of PROPER USAGE you should return 0. This means that invalid key-presses occurred previously and your calculator is no longer in a functioning state. For example, if you have:
"53++6=12+"
The input has 2 consecutive Operator key-presses("++") so the entire input violates PROPER USAGE and you will return 0.

Create a class MathDebug that contains the method getNext, which takes a String, input, and returns an int that represents valid next key-presses or 0 if there is an error in the input.

Definition

Class:
MathDebug
Method:
getNext
Parameters:
String
Returns:
int
Method signature:
int getNext(String input)
(be sure your method is public)

Constraints

  • input will only contain characters within the following string: "0123456789+-*/=" (quotes are for clarity).
  • input will have length between 0 and 50 characters, inclusive.

Examples

  1. "*"

    Returns: 0

  2. "54+6564=465/546=565"

    Returns: 3

  3. ""

    Returns: 1

  4. "123"

    Returns: 3

  5. "123+456"

    Returns: 5

  6. "1+"

    Returns: 1

  7. "0-0-"

    Returns: 0

  8. "0-0="

    Returns: 1

  9. "99*99=00+00="

    Returns: 1

  10. "98+01234=234*1230=000/000="

    Returns: 1

  11. "012345678901234567890123456789/0123456789=123"

    Returns: 3

  12. "+++"

    Returns: 0

  13. "932++123"

    Returns: 0

  14. "123+"

    Returns: 1

  15. "0"

    Returns: 3

  16. "=123+456="

    Returns: 0

  17. "1234+123=98*0"

    Returns: 5

  18. "98+02134=234*1230=000/000"

    Returns: 5

  19. "98+76+23="

    Returns: 0

  20. "1+2="

    Returns: 1

  21. "1"

    Returns: 3

  22. "++5"

    Returns: 0

  23. "1+1+"

    Returns: 0

  24. "++"

    Returns: 0

  25. "+"

    Returns: 0

  26. "123+1234+1234=1234+142/123*1234+123+123=12+11+2=1"

    Returns: 0

  27. "0/0/"

    Returns: 0

  28. "+5"

    Returns: 0

  29. "=2-3"

    Returns: 0

  30. "98+98=98+"

    Returns: 1

  31. "100+=100+3="

    Returns: 0

  32. "+1"

    Returns: 0

  33. "="

    Returns: 0

  34. "123++"

    Returns: 0

  35. "1+2="

    Returns: 1

    Thus far we have which is one valid calculation. We can now accept another valid calculation of the form . Since is next we will only accept a Digit key-press.

  36. "123+"

    Returns: 1

    Thus far we have so we need the next . The only possible key-press is Digit since if, Equal was given, the second would have a length of 0.

  37. ""

    Returns: 1

    No input has been given thus far so we are waiting for a Digit key-press.

  38. "1"

    Returns: 3

    The next key-press could be either a Digit or an Operator.

  39. "=123+456="

    Returns: 0

    The input started with an '=' which is an invalid key-press so the entire computation is invalid.

  40. "1234+123=98*0"

    Returns: 5

  41. "98+01234=234*1230=000/000="

    Returns: 1

  42. "98+76+23="

    Returns: 0


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.
This problem was used for: