5 ms·
Helps how?
by IlliOnato 6y ago
Helps how?
- raverbashing 6y agoWrapping around the array, you just need for your position to be i%len Though ok, reading an array backwards requires a len-1-i
- IlliOnato 6y agoWhy would you need to "wrap around" an array? I guess it's helpful if you use an array to represent a cyclic group, but how often this happens in practice? And when you need the number of the last element, you need to do A[n-1], which is "less helpful", I guess.
- raverbashing 6y agoI think both those cases happen with a certain frequency, yes, your last element is a bit trickier, though in Python it is represented by A[-1] (so that would turn into A[len-1]) Yeah, maybe there's no fundamental advantage, one would have to check the most common cases. > Why would you need to "wrap around" an array? Ring buffers
- secondcoming 6y agoRingbuffers are used all over
- hmry 6y agoOr alternatively, len + ~i Okay, maybe don't use that one in production :)