3 ms·
Exactly: In the first example, they take an array, copy it and add extraneous data for the sole purpose of eliminating a couple if statements? I'm not perfect
by cabaalis 8y ago
Exactly: In the first example, they take an array, copy it and add extraneous data for the sole purpose of eliminating a couple if statements?
I'm not perfect either, but that deficiency for the sake of elegance is technical debt. A year later, when that piece of code is running thousands of times over thousands of numbers, nobody is going to know why it does what it does. Just that the system throws random out of memory errors or something.
- phkahler 8y ago>> Exactly: In the first example, they take an array, copy it and add extraneous data for the sole purpose of eliminating a couple if statements? The IF statements are not even necessary. Compute the sum for the first elements neighbors first (sum[0]=element[1]) and the last element (sum[n-1]=element[n-2]) Then loop over the remaining elements with no conditionals. I agree with the author that it's probably better to pad it than to have conditionals at every iteration, but for this problem there is no need to have a conditional at all.
- dpark 8y agoYour algorithm breaks for lists of size 0 or 1.