Averages of numbers in Java

The challenge

Get the averages of these numbers

Write a method, that gets an array of integer-numbers and return an array of the averages of each integer-number and his follower, if there is one.

Example:

Input: [ 1, 3, 5, 1, -10] Output: [ 2, 4, 3, -4.5]
Code language: CSS (css)

If the array has 0 or 1 values or is null, your method should return an empty array.

The solution in Java code

Option 1:

public class Solution { public static double[] averages(final int[] numbers) { final double res[] = new double[(numbers == null || numbers.length == 0) ? 0 : numbers.length - 1]; for (int i = 0; i < res.length; i++) res[i] = (numbers[i]+numbers[i+1]) / 2.0; return res; } }
Code language: Java (java)

Option 2:

public class Solution { public static double[] averages(int[] numbers) { if (numbers == null || numbers.length < 2) return new double[] {}; double [] ans = new double [numbers.length - 1]; for (int i = 0; i < ans.length; i++) ans[i] = (numbers[i] + numbers[i + 1]) / 2.0; return ans; } }
Code language: Java (java)

Option 3:

import java.util.stream.IntStream; public class Solution { public static double[] averages(int[] numbers) { if (numbers == null || numbers.length < 2) return new double[0]; return IntStream.range(1, numbers.length) .mapToDouble(i -> (numbers[i - 1] + numbers[i]) / 2.0) .toArray(); } }
Code language: Java (java)

Test cases to validate our solution

import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; import java.util.Arrays; public class SolutionTest { @Test public void basicTests() { assertEquals(Arrays.toString(new double[] { 2, 2, 2, 2 }), Arrays.toString(Solution.averages(new int[] { 2, 2, 2, 2, 2 }))); assertEquals(Arrays.toString(new double[] { 0, 0, 0, 0 }), Arrays.toString(Solution.averages(new int[] { 2, -2, 2, -2, 2 }))); assertEquals(Arrays.toString(new double[] { 2, 4, 3, -4.5 }), Arrays.toString(Solution.averages(new int[] { 1, 3, 5, 1, -10 }))); } @Test public void nullEmptyTests() { assertEquals("input 'null' should return an empty array", 0, Solution.averages(null).length); assertEquals("Empty array as input should return an empty array", 0, Solution.averages(new int[0]).length); assertEquals("Array with only one value as input should return an empty array", 0, Solution.averages(new int[] { 2 }).length); } @Test public void randomTests() { for(int r = 0; r < 100; r++) { int len = (int)Math.floor(Math.random() * 20); int[] numbers = new int[len]; for(int k = 0; k < len; k++) { numbers[k] = (int)Math.floor(Math.random() * 41) - 20; } double[] expected = new double[0]; if(numbers.length > 1) { expected = new double[numbers.length - 1]; for(int i=0;i<numbers.length-1;i++) { expected[i] = ((double)numbers[i] + numbers[i+1]) / 2; } } assertArrayEquals("Should work for random input", expected, Solution.averages(numbers), 0.001); } } }
Code language: Java (java)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments