Thursday, June 29, 2017

Calculate Statistics

This task will process a sequence of integer numbers
to determine the following statistics:

    o) minimum value
    o) maximum value
    o) number of elements in the sequence
    o) average value

For example: [6, 9, 15, -2, 92, 11]

    o) minimum value = -2
    o) maximum value = 92
    o) number of elements in the sequence = 6
    o) average value = 21.833333



import java.util.Arrays;
import java.util.IntSummaryStatistics;

public class CalculateStatistics {
    
     public static void main(String[] args) {
          double[] stats = solution(6, 9, 15, -2, 92, 11);
          System.out.println(String.format("Minimum=%f, Maximum=%f, Count=%f, Average=%f", stats[0], stats[1], stats[2], stats[3]));
     }
    
     public static double[] solution(int...numbers){
          double[] stats = new double[4];
          IntSummaryStatistics summaryStatistics = Arrays.stream(numbers).summaryStatistics();
          stats[0] = summaryStatistics.getMin();
          stats[1] = summaryStatistics.getMax();
          stats[2] = summaryStatistics.getCount();
          stats[3] = summaryStatistics.getAverage();
          return stats;
     }
}

No comments:

Post a Comment