Problem Statement | |||||||||||||
A table in a database consists of rows and columns. Rows correspond to elements in the table, and columns correspond to fields in the elements. In this problem we will only deal with elements that have integer fields in the range 0 to 100, inclusive. The columns will be named sequentially with lower case letters, starting with 'a', then 'b' and so on. So an element in a table with 4 columns may be described like this: "a=3, b=10, c=15, d=0". Note that all fields in an element must contain an integer value in the valid range, i.e. no field may be omitted. A large table in a database can be a performance hog. One way to avoid this is to logically partition the table into several smaller tables. This is done by giving each partition a set of constraints on the columns, specifying which rows in the large table should go to which partition. Given the constraints for the partitions of a table, write a class that determines whether the partitions are well formed (see below). The constraints will be given as a String[] where each element corresponds to one partition. Each element contains a space separated list of constraints, where each constraint is in the form "<column><operator><value>" (quotes for clarity only) where <column> is a table column, <operator> is one of the relational operators '=', '<', '>', '<=', '>=', and <value> is an integer between 0 and 100, inclusive (without leading zeros). The constraints for each partition represents a conjunction, i.e. all constraints must hold (see example 1). For instance, if we have 3 partitions and a table with 2 columns, the constraints might look like this: {"a>3", "a<=3 b>5", "b<6 a<=3"}. Element "a=4, b=3" would then go to the first partition, element "a=3, b=9" would go to the second partition and element "a=0, b=5" would go to the third partition. The partitioning is badly constructed if one or more of the criteria below holds:
If several of the criteria above holds for the partitions, return the first that holds (i.e. "EMPTY" has highest priority, followed by "OVERLAP" and finally "INCOMPLETE"). Otherwise the partitioning is well formed; that is, an arbitrary valid element can go into exactly one partition and no partition is empty. In this case you should return the String "OK". Create a class TablePartitions containing the method validate which takes an int n, the number of columns in the table, and a String[] partitions containing the constraints for each partition in the format above. The method should "EMPTY", "OVERLAP", "INCOMPLETE" or "OK" (the meanings of these Strings are explained above). | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | A partition might have no constraints, meaning that all elements can go into this partition (see example 6). | ||||||||||||
- | Remember that the integer fields in a valid element is always between 0 and 100, inclusive. | ||||||||||||
- | There can be multiple constraints on the same column in one partition, see example 0. | ||||||||||||
- | All quotes around the return Strings above are for clarity only. | ||||||||||||
Constraints | |||||||||||||
- | n will be between 1 and 8, inclusive. | ||||||||||||
- | partitions will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element in partitions will contain between 0 and 50 characters, inclusive. | ||||||||||||
- | Each element in partitions with more than 0 characters will contain a space separated list of constraints in the format specified above. | ||||||||||||
- | There will be no leading or trailing spaces in the elements in partitions. | ||||||||||||
- | Each constraint in the elements in partitions will be separated with exactly one space. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
| |||||||||||||
5) | |||||||||||||
|