TopcoderArchiveVisit Topcoder
SRM5 Jun 2012
SRM 565

ValueHistogram

Problem statement, definition, constraints, and public examples.

Problem Statement

A histogram of a given collection of numbers graphically represents the frequency of each value in the collection. We are given several numbers ranging from 0 to 9 as a int[] values. The goal is to build their histogram according to the following rules.

1) The width of the histogram should be exactly 10.

2) The height of the histogram should equal to H+1, where H is the number of times the most frequent element occurs in values.

3) The i-th (0-based) column of the histogram corresponds to the value i. Let X(i) be the frequency of value i in values. Then the last X(i) characters in the column should be 'X's and the other ones should be '.'s. For example, if value i was not present in values, the column should be filled with '.' characters. If i was present once, the last element of the column should be 'X' and and the other ones should be '.'s. If i was present twice, the last two elements should be 'X's and and the other ones should be '.'s, and so on.

Build the histogram and return it as a String[].

Definition

Class:
ValueHistogram
Method:
build
Parameters:
int[]
Returns:
String[]
Method signature:
String[] build(int[] values)
(be sure your method is public)

Constraints

  • values will contain between 1 and 50 elements, inclusive.
  • Each element of values will be between 0 and 9, inclusive.

Examples

  1. {2, 3, 5, 5, 5, 2, 0, 8}
    Returns: {"..........", ".....X....", "..X..X....", "X.XX.X..X." }
    The most frequent value is 5, which occurs 3 times. Hence the height of the histogram is 4. It looks as follows: .......... .....X.... ..X..X.... X.XX.X..X.
  2. {9, 9, 9, 9}
    Returns: {"..........", ".........X", ".........X", ".........X", ".........X" }
    .......... .........X .........X .........X .........X
  3. {6, 4, 0, 0, 3, 9, 8, 8}
    Returns: {"..........", "X.......X.", "X..XX.X.XX" }
    .......... X.......X. X..XX.X.XX
  4. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    Returns: {"..........", "XXXXXXXXXX" }
    .......... XXXXXXXXXX
  5. {6, 2, 3, 3, 3, 7, 8, 1, 0, 9, 2, 2, 4, 3}
    Returns: {"..........", "...X......", "..XX......", "..XX......", "XXXXX.XXXX" }
    .......... ...X...... ..XX...... ..XX...... XXXXX.XXXX
Back to all problems