TopCoder problem "GradingGridIns" used in SRM 264 (Division I Level Two)

Problem Statement

    The Grid-In questions on the SAT require that the test-taker fill in a numerical answer from a small set of symbols. In particular, each answer is a String of four characters.
  • Each character is one of ' ' (a space), '.', '/', or a digit '0'-'9'.
  • Spaces are interpreted as unneeded characters. Whitespace must not be surrounded by non-space characters.
  • The '.' is a decimal point; only one may appear in any answer. The '/' is a division sign; only one may appear in any answer. No answer may contain both a '.' and a '/'.
  • All characters to the left of a '/' are interpreted as the numerator, and all characters to the right of a '/' are interpreted as the denominator. If there is a '/', both the numerator and the denominator must contain at least one digit '0'-'9', and the denominator must not be 0.
  • At least one digit '0'-'9' must appear in each answer.
A range will be given between a lower bound and upper bound, inclusive. Both bounds are specified as fractions with integer numerators and denominators. lower and upper are both int[]s containing two elements, the first the numerator and the second the denominator. An answer is correct if and only if either it lies between the bounds or there is no possible submission between the answer and one of the bounds. See example 1 for clarification.

An answer which satisfies the symbol constraints (the first item on the list) but does not satisfy other constraints (such as "1 23", "8//5", or "9.4." is considered malformed. Furthermore, any answer with a denominator of 0 is considered malformed.

You will be given a String[] answer whose elements each contain exactly 4 characters. You are to return a String[] with the same number of elements as the number of elements in answer. Each element of the return value should be one of three possible responses: "CORRECT" for a correct answer, "INCORRECT" if incorrect, or "MALFORMED" if the answer does not conform to the guidelines above. The i-th element of the return value should correspond to the i-th element of answer.


Parameters:String[], int[], int[]
Method signature:String[] score(String[] answer, int[] lower, int[] upper)
(be sure your method is public)


-Leading zeros are allowed.
-An answer which has a denominator of 0 will be considered malformed.


-answer will contain between 1 and 50 elements, inclusive.
-Each element of answer will contain exactly 4 characters.
-Each character in an answer will be a ' ', '.', '/', or a digit '0'-'9'.
-upper and lower will each contain exactly 2 elements between 0 and 9999, inclusive.
-Neither lower[1] nor upper[1] will be 0.
-lower[0] / lower[1] will be less than or equal to upper[0] / upper[1].


{"4/7 "," 4/7","4/07","8/14",".571",".572"}
Each of these is correct. Notice that neither .571 nor .572 are equal to 4 divided by 7.
{" 4/7","1.01","1.02"," 000"}
Any answer within the allowed range is acceptable. Notice also that 1.01, which is greater than the upper bound, is acceptable because there are no possible submissions greater than 1 and less than 1.01.
{"1.15","1 14","1.14"," 8/7"}
{"    ","...."," .  ","1 23","8//5","9.4.","85/ ","/123","123/"}
{"1/0 "}
Returns: {"MALFORMED" }

Problem url:

Problem stats url:




PabloGilberto , brett1479 , Olexiy

Problem categories:

Simple Math, Simple Search, Iteration, String Parsing