TopCoder problem "Glossary" used in SRM 353 (Division I Level One , Division II Level Two)



Problem Statement

    

A glossary is an alphabetical list of terms in some specialized field of knowledge, usually published as an appendix to a text on that field.

In this problem, you will be given a String[] items, where each element is a single term. You must build a two column glossary of these terms using the following method:

For each letter 'A'-'Z', if there is at least one term that begins with that letter (case-insensitive), create an entry for the letter in the following format:

  1. Place the uppercase letter on the first line.
  2. Place 19 dashes ('-') on the second line.
  3. Place each term that begins with the letter on a new line, in alphabetical order (again, case-insensitive). Each term should be preceded by exactly 2 spaces.
  4. Pad all lines with spaces on the right side until they are exactly 19 characters wide.

Now, sort these entries alphabetically by their letter, and split them into two columns. The left column should contain the entries for letters 'A' through 'M', inclusive, and the right column should contain the entries for letters 'N' through 'Z', inclusive. Each column must be exactly 19 characters wide, and the two columns must be separated by exactly 2 spaces. See examples for clarification.

 

Definition

    
Class:Glossary
Method:buildGlossary
Parameters:String[]
Returns:String[]
Method signature:String[] buildGlossary(String[] items)
(be sure your method is public)
    
 

Notes

-Two strings differing only in capitalization are considered the same, for example "aa" and "aA" are same.
 

Constraints

-items will contain between 1 and 50 elements, inclusive.
-Each element of items will contain between 1 and 17 characters, inclusive.
-Each element of items will contain letters ('A' - 'Z', 'a' - 'z') and spaces (' ') only.
-Each element of items will start with a letter.
-Each term will appear at most once in items.
 

Examples

0)
    
{"Canada", "France", "Germany", "Italy", "Japan", "Russia", 
 "United Kingdom", "United States"}
Returns: 
{"C                    R                  ",
"-------------------  -------------------",
"  Canada               Russia           ",
"F                    U                  ",
"-------------------  -------------------",
"  France               United Kingdom   ",
"G                      United States    ",
"-------------------                     ",
"  Germany                               ",
"I                                       ",
"-------------------                     ",
"  Italy                                 ",
"J                                       ",
"-------------------                     ",
"  Japan                                 " }
The Group of Eight. Together, these countries represent about 65% of the world economy.
1)
    
{"alpha", "beta", "gamma", "delta", "omega"}
Returns: 
{"A                    O                  ",
"-------------------  -------------------",
"  alpha                omega            ",
"B                                       ",
"-------------------                     ",
"  beta                                  ",
"D                                       ",
"-------------------                     ",
"  delta                                 ",
"G                                       ",
"-------------------                     ",
"  gamma                                 " }
2)
    
{"AVL tree", "backtracking", "array", "balanced tree", "binary search"}
Returns: 
{"A                                       ",
"-------------------                     ",
"  array                                 ",
"  AVL tree                              ",
"B                                       ",
"-------------------                     ",
"  backtracking                          ",
"  balanced tree                         ",
"  binary search                         " }
The second column might be empty, but you need to pad each line of the result with spaces.
3)
    
{"XXXXXXXXXXXXXXXXX", 
 "YYYYYYYYYYYYYYYYY",
 "ZZZZZZZZZZZZZZZZZ"}
Returns: 
{"                     X                  ",
"                     -------------------",
"                       XXXXXXXXXXXXXXXXX",
"                     Y                  ",
"                     -------------------",
"                       YYYYYYYYYYYYYYYYY",
"                     Z                  ",
"                     -------------------",
"                       ZZZZZZZZZZZZZZZZZ" }
The first column may be empty too.
4)
    
{"Asteria", "Astraeus", "Atlas", "Clymene", "Coeus", "Crius", 
 "Cronus", "Dione", "Epimetheus", "Helios", "Hyperion", "Iapetus", 
 "Leto", "Mnemosyne", "Oceanus", "Ophion", "Phoebe", "Prometheus", 
 "Rhea", "Tethys", "Theia", "Themis"}
Returns: 
{"A                    O                  ",
"-------------------  -------------------",
"  Asteria              Oceanus          ",
"  Astraeus             Ophion           ",
"  Atlas              P                  ",
"C                    -------------------",
"-------------------    Phoebe           ",
"  Clymene              Prometheus       ",
"  Coeus              R                  ",
"  Crius              -------------------",
"  Cronus               Rhea             ",
"D                    T                  ",
"-------------------  -------------------",
"  Dione                Tethys           ",
"E                      Theia            ",
"-------------------    Themis           ",
"  Epimetheus                            ",
"H                                       ",
"-------------------                     ",
"  Helios                                ",
"  Hyperion                              ",
"I                                       ",
"-------------------                     ",
"  Iapetus                               ",
"L                                       ",
"-------------------                     ",
"  Leto                                  ",
"M                                       ",
"-------------------                     ",
"  Mnemosyne                             " }
Titans.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7838

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10710&pm=7838

Writer:

xOberon

Testers:

PabloGilberto , brett1479 , Olexiy , lovro

Problem categories:

String Parsing