Problem Statement
You've collected some data, and now you want to generate a histogram.
Each data point is a floating point number between 0 and 1,
exclusive. Your histogram should have N bars, each representing
an interval of the same size. For example, if N=4, the bars of
the histogram would represent the ranges 0.0-0.25,
0.25-0.5, 0.5-0.75 and 0.75-1.0. Given a
String[], data, where each element represents a decimal value, return a int[], with
N elements, representing the N intervals, in order.
Each element of the return should be equal to the number of elements
of data that fall into the corresponding interval. You need
not worry about data points that fall exactly on the boundary of an
interval, as that will not occur.
Definition
- Class:
- HistogramHelper
- Method:
- generate
- Parameters:
- String[], int
- Returns:
- int[]
- Method signature:
- int[] generate(String[] data, int N)
- (be sure your method is public)
Constraints
- data will contain between 0 and 50 elements, inclusive.
- Each element of data represents a decimal value between 0 and 1, exclusive.
- Each element of data will be formatted as a "0." followed by a sequence of between 1 and 20 integers.
- No element of data will fall within 1e-9 of the boundary of two intervals.
- N will be between 1 and 50, inclusive.
Examples
{"0.1", "0.4", "0.6", "0.999999"}3
Returns: {1, 2, 1 }"0.1" goes in the first interval, "0.4" and "0.6" go in the second, and "0.999999" goes in the third.{"0.01","0.03","0.654","0.8597","0.456"}50
Returns: {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }{"0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9"}1
Returns: {9 }{}1
Returns: {0 }