Connection Status:
Competition Arena > Datatype
SRM 136 · 2003-02-25 · by lars2520
Class Name: Datatype
Return Type: String
Method Name: getType
Arg Types: (string)
Problem Statement

Problem Statement

When reading data as a string of characters, it is often useful to convert the data into a different datatype, which is easier to work with than a string of characters. For example, many operations are made much easier if the string of characters "123" is converted from a string of characters into a different datatype, such as an int or a float. We want to determine the best datatype to use for some data, given a string of characters. For the purposes of this problem, we will consider converting a string of characters into one of 4 datatypes:

  • "INTEGER" - if the string consists only of the digits '0' - '9'.
  • "BOOLEAN" - if the string is "true" or "false" (ignoring case)
  • "DECIMAL" - if the string contains only digits '0' - '9', and exactly one decimal point ('.'). "123.12", ".123", and "124." are all of type "DECIMAL". Note that by this definition the string "." is classified as a "DECIMAL"
  • "STRING" - if the string is none of the other datatypes.

Your task is to write a class Datatype with a method getType that takes a String, var, and classifies it into one of the above four types, and returns the datatype as a String.

Constraints

  • var will contain between 1 and 50 characters, inclusive.
  • Each character in var will be a letter ('a'-'z' or 'A'-'Z'), a digit ('0'-'9'), a decimal point ('.'), a space (' '), or one of the following characters: ,/<>?;':"[]{}\|`~!@#$%^*()_+-=&
Examples
0)
"123"
Returns: "INTEGER"
1)
"324.1"
Returns: "DECIMAL"
2)
".12"
Returns: "DECIMAL"
3)
"453."
Returns: "DECIMAL"
4)
"770.555.1212"
Returns: "STRING"

Submissions are judged against all 37 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Datatype with a public method string getType(string var) · 37 test cases · 2 s / 256 MB per case

Submitting as anonymous