TopCoder problem "Histogram" used in SRM 202 (Division I Level Two)



Problem Statement

    Given a sequence of values, we want to display a histogram of vertical bars with a title at the bottom of each bar. Each value will be an integer, and the corresponding bar will consist of that many 'X' characters. For example,
                                   X
                                   X
               X                   X
     X         X         X         X         X 
    EAST     NORTH     SOUTH      WEST INTERNATIONAL
We require that the bars be centered over their titles, using the more leftward of the two middle positions for a title whose length is even. We require that there must be at least one space between adjacent titles, and that the number of spaces between adjacent bars be constant. In the example above, there are 9 spaces between adjacent bars.

Create a class Histogram that contains a method draw that is given a String[] title and a int[] value. It returns a String[] in which each element is the next row of the histogram to be printed, starting with the top row and ending with the row that contains the titles.

The histogram must preserve the order given in title and value. The spacing between bars should be as small as possible. Each element of the return should be as short as possible; this implies that the final element of the return will have no leading spaces, and that no element of the return will have trailing spaces.

 

Definition

    
Class:Histogram
Method:draw
Parameters:String[], int[]
Returns:String[]
Method signature:String[] draw(String[] title, int[] value)
(be sure your method is public)
    
 

Constraints

-title will contain between 2 and 10 elements inclusive.
-Each element of title will contain between 1 and 15 characters inclusive, all uppercase letters 'A'-'Z'.
-value will contain the same number of elements as title.
-Each element of value will be between 1 and 10 inclusive.
 

Examples

0)
    
{"EAST","NORTH","SOUTH","WEST","INTERNATIONAL"}
{1,2,1,4,1}
Returns: 
{ "                               X",
 "                               X",
 "           X                   X",
 " X         X         X         X         X",
 "EAST     NORTH     SOUTH      WEST INTERNATIONAL" }
This is the example given in the problem.
1)
    
{"A","B","C"}
{2,1,2}
Returns: { "X   X",  "X X X",  "A B C" }
This corresponds to the following histogram:
   X   X
   X X X
   A B C
2)
    
{"VERYLARGE", "XX"}
{10,1}
Returns: 
{ "    X",
 "    X",
 "    X",
 "    X",
 "    X",
 "    X",
 "    X",
 "    X",
 "    X",
 "    X     X",
 "VERYLARGE XX" }

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5848&pm=2874

Writer:

dgoodman

Testers:

lbackstrom , brett1479

Problem categories:

String Manipulation