TopcoderArchive
Archive
Algorithm problem

SetType

Problem statement, definition, constraints, and public examples.

Problem Statement

We would like to add some syntax to a programming language to make it easier to work with sets and pairs. To make this work though, we need to be able to define a type for everything, and so we will use the following simple grammar for our types:
<type> ::= set(<type>) | pair(<type1>,<type2>) | integer
If something has type set(<type>), it will be written as:
{e0,e1,...,en}
where each ei has type <type> and e0 exists.
If something has type pair(<type>1,<type>2), it will be written as:
(a,b)
where a has type <type>1 and b has type <type>2. Naturally, if something has type integer, it is an integer (which will be between 1 and 100 in this problem). You will be given a well-formed set, pair or integer as defined above, and you are to return the type of that expression.

Definition

Class:
SetType
Method:
type
Parameters:
String
Returns:
String
Method signature:
String type(String expression)
(be sure your method is public)

Constraints

  • expression will represent a well formed expression. All elements of a set will contain the same type, and all integers will be between 1 and 100 with no leading zeros. All sets will contain at least 1 element.
  • expression will contain between 1 and 50 characters, inclusive.

Examples

  1. "100"
    Returns: "integer"
  2. "(1,2)"
    Returns: "pair(integer,integer)"
  3. "{1,2,3}"
    Returns: "set(integer)"
  4. "({1},2)"
    Returns: "pair(set(integer),integer)"
Back to all problems