Skip to main content

Timeline for Left rotation arrays algorithm

Current License: CC BY-SA 3.0

11 events
when toggle format what by license comment
Oct 8, 2016 at 20:56 history edited Flambino CC BY-SA 3.0
tweaked for clarity
Oct 5, 2016 at 12:25 history edited Flambino CC BY-SA 3.0
added 15 characters in body
Oct 3, 2016 at 16:28 comment added cFreed Thanks for the conforting words:) Yes, I know my suggestion may be valid, but on condition I remain consistent: so "Sigh" is still relevant, since it's about the fact that I could suddently forget a constraint that I'd approved 5 minutes ago! Poor old tired brain...
Oct 3, 2016 at 16:14 comment added Flambino @cFreed No need to sigh; it's still a valid solution :) Just make sure that the in-place modification is thoroughly documented to avoid any surprises for the user
Oct 3, 2016 at 8:17 comment added cFreed Oops! You're totally right. The ironic point is that I'd previously read the comment about not modifying the original array, and already strongly agreed. But I forgot it when I thought to use splice()! Sigh...
Oct 3, 2016 at 0:51 comment added Flambino @cFreed Good point. However, splice modifies in-place, which I'd rather avoid (though it's a valid solution; alluded to something like that in an earlier comment). You could of course make a copy and modify that with splice
Oct 2, 2016 at 23:01 comment added cFreed Excellent review. But I suggest a slight improvement: the whole // slice and concat part might be replaced by a // splice and concat which is merely return array.splice(n).concat(array);. This way only one splice() operation is needed instead of two slice() ones, and it works with n positive or negative.
Oct 2, 2016 at 12:11 comment added Flambino @tony19 Because the function should always return a copy in order to be consistent. If it sometimes doesn't, you don't know what you're getting. E.g. you have array a, and you do var x = rotateArray(a, n).pop(). If the function returns a copy, a remains untouched. But if it just returns a reference to its input, then the pop will actually be modifying the original a array. If the function's inconsistent, you just don't know what'll happen. The function could also be written to do the opposite: Always modify the array in-place (i.e. side-effects). But I'd rather get a copy back.
Oct 2, 2016 at 11:57 comment added tony19 In the case of no rotation, why return a copy instead of the original array? (Could we optimize by simply returning array in that case?)
Oct 2, 2016 at 1:54 history edited Flambino CC BY-SA 3.0
added 682 characters in body
Oct 2, 2016 at 1:41 history answered Flambino CC BY-SA 3.0