7
1
Background
Imagine, you have a big array A
, which is mostly zeroes, but contains a short subarray B
which has only strictly positive entries. For instance:
| A |
[0 0 0 0 1 2 3 0]
| B |
Now say, we split the array A
into consecutive subarrays of length d
(with a shorter final array, if the d
doesn't divide the length of A
). For d = 3
:
[0 0 0 0 1 2 3 0]
| |
[0 0 0] [0 1 2] [3 0]
And now sum up each of those arrays:
[0 3 3]
And discard zero sums:
[3 3]
The Challenge
You're to write a function which prints that final array of sums in any convenient array format, given the following 5 parameters (or any subset thereof, if you don't need all of them):
- The small array
B
. - The size of the small array
B
(3
in the above example). - The size of the big array
A
(8
in the above example). - The index where the small array begins (
4
in the above example). - The length
d
of the subarrays (3
in the above example).
The Rules
- You are not allowed to construct the big array
A
explicitly! - You are not allowed to construct any of the subarrays of length
d
explicitly! - That means you have to get the computation directly from the
B
and the other parameters at your disposal!
Fewest characters wins!
Can we remove/omit the inputs we don't need? As far as I can tell, I don't need
size_bigarray
orsize_smallarray
at all. – Geobits – 2014-11-24T16:00:27.400yes if you don't need it – nvidia – 2014-11-24T16:02:53.030
May we construct the mini arrays that overlap with
small_array
? In your example, that would only be[0,1,2], 3,0]
. Or none at all? – Martin Ender – 2014-11-24T16:09:48.687no mini arrays should be constructed – nvidia – 2014-11-24T16:14:59.460
I tried to reword the spec a bit, to make the actual problem a bit more visual in the first place. Feel free to roll back the edit if you don't like it, it's just a suggestion! – Martin Ender – 2014-11-25T01:48:52.327
@MartinBüttner Are A and B supposed to be switched in the first code block? – NinjaBearMonkey – 2014-11-25T02:25:37.620
@hsl oops, yes... – Martin Ender – 2014-11-25T09:33:15.573