I am looking at the Sums of Parts problem on CodeWars:
Let us consider this example (array written in general format):
ls = [0, 1, 3, 6, 10]
Its following parts:
ls = [0, 1, 3, 6, 10] ls = [1, 3, 6, 10] ls = [3, 6, 10] ls = [6, 10] ls = [10] ls = []
The corresponding sums are (put together in a list):
[20, 20, 19, 16, 10, 0]
The function
parts_sums
(or its variants in other languages) will take as parameter a list ls and return a list of the sums of its parts as defined above.
The goal of the function is to sum elements of array then shift the first element of array each time until the length of array becomes 0.
I have this solution for it:
function partsSums(ls) {
let len = ls.length;
let arr = [];
for (let i = 0; i < len +1; i++) {
arr.push(summation(ls));
ls.shift();
}
function summation(a) {
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
return arr;
}
It works when I run it in my editor: all test cases on CodeWars that successfully complete pass, but when I try to submit, I get this error:
Process was terminated. It took longer than 12000ms to complete
I'm new to algorithms and can't understand where the error is? Any suggestions are welcome.