Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

2 of 5 edited body

# Shortest programm to split special array

The challenge:

write a function func(small_array, size_bigarray, divisor, index_where_small_array_begins, size_smallarray):

The function does the following:

Imagine a big array with size bigarray. (you don't have the array explicity)

It is filled with zeros. Except from index_where_small_array_begins the values of the big_array are identically with small_array.

small_array contains only positive numbers. (not zero)

So small_array is a part of big_array. (you can assume it fits, that means index_where_small_array_begins + size_smallarray <= size_bigarray)

Now split the big_array in mini_arrays of size divisor. (If divisor is not a divisor of size_bigarray, the last array is smaller)

(Example size_bigarray = 10, divisor = 3, then you have four arrays of size: 3, 3, 3, 1)

For each of the mini_array you add the values together, call it sum_of_the_miniarray.

Print out an array with sum_of_the_miniarray for all miniarrays for which the sum_of_the_miniarray is nonzero. (You can conclude that from index_where_small_array_begins and size_smallarray)

Example: Given:  small_array = [1,2,3] index_where_small_array_begins = 4 size_bigarray = 8 divisor = 3  then big_array would be:  big_array = [0,0,0,0,1,2,3,0]  the mini arrays would be

[0,0,0] [0,1,2] [3,0]

the sums would be

0 3 3

But you only output [3,3] because the first one is zero.

The rules

You are not allowed to construct the big array explicitly!

You are not allowed to construct the mini arrays explicitly!

That means you have to get the computation directly from the small_array!

Fewest characters wins!

Good luck!