The challenge
You are given a list/array which contains only integers (positive and negative). Your job is to sum only the numbers that are the same and consecutive. The result should be one list.
Extra credit if you solve it in one line. You can assume there is never an empty list/array and there will always be an integer.
Same meaning: 1 == 1
1 != -1
Examples:
[1,4,4,4,0,4,3,3,1] # should return [1,12,0,4,6,1]
"""So as you can see sum of consecutives 1 is 1
sum of 3 consecutives 4 is 12
sum of 0... and sum of 2
consecutives 3 is 6 ..."""
[1,1,7,7,3] # should return [2,14,3]
[-5,-5,7,7,12,0] # should return [-10,14,12,0]
The solution in Java code
Option 1:
import java.util.*;
public class Consecutives {
public static List<Integer> sumConsecutives(List<Integer> s) {
int previous = Integer.MAX_VALUE;
LinkedList<Integer> l = new LinkedList<>();
for (Integer v: s){
l.add(v == previous? l.pollLast() + v : v);
previous = v;
}
return l;
}
}
Option 2:
import java.util.ArrayList;
import java.util.List;
public class Consecutives {
public static List<Integer> sumConsecutives(List<Integer> s) {
List<Integer> accumulator = new ArrayList<>();
for (int i = 0, sum = 0; i < s.size(); i++) {
sum += s.get(i);
if (i == s.size() - 1 || s.get(i) != s.get(i + 1)) {
accumulator.add(sum);
sum = 0;
}
}
return accumulator;
}
}
Option 3:
import java.util.*;
public class Consecutives {
public static List<Integer> sumConsecutives(List<Integer> list) {
List<Integer> result = new ArrayList<>();
int sum = list.get(0);
int i = 0;
while (i < list.size()){
if (i == list.size() - 1) {
result.add(sum);
} else if (list.get(i).equals(list.get(i + 1))) {
sum += list.get(i);
} else {
result.add(sum);
sum = list.get(i + 1);
}
i++;
}
return result;
}
}
Test cases to validate our solution
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class ConsecutivesTest {
@Test
public void test() {
System.out.println("Basic Tests");
List<Integer> i = Arrays.asList(1,4,4,4,0,4,3,3,1);
List<Integer> o = Arrays.asList(1,12,0,4,6,1);
System.out.println("Input: {1,4,4,4,0,4,3,3,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(-5,-5,7,7,12,0);
o = Arrays.asList(-10,14,12,0);
System.out.println("Input: {-5,-5,7,7,12,0}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,1,7,7,3);
o = Arrays.asList(2,14,3);
System.out.println("Input: {1,1,7,7,3}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(3,3,3,3,1);
o = Arrays.asList(12, 1);
System.out.println("Input: {3,3,3,3,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(2,2,-4,4,5,5,6,6,6,6,6,1);
o = Arrays.asList(4, -4, 4, 10, 30, 1);
System.out.println("Input: {2,2,-4,4,5,5,6,6,6,6,6,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,1,1,1,1,3);
o = Arrays.asList(5, 3);
System.out.println("Input: {1,1,1,1,1,3}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,-1,-2,2,3,-3,4,-4);
o = Arrays.asList(1, -1, -2, 2, 3, -3, 4, -4);
System.out.println("Input: {1,-1,-2,2,3,-3,4,-4}");
assertEquals(o, Consecutives.sumConsecutives(i));
}
}