rovf has asked for the wisdom of the Perl Monks concerning the following question:
Assuming: @arr is an array, $n is a number greater than zero and less than the number of elements in the array. Problem: Rotate the array left by $n positions.
Example:
For the special case $n==1, I think the best solution would be@arr=(1,2,3,4,5,6,7); $n=3; # @arr afterwards: # (4,5,6,7,1,2,3)
push @arr,(shift @arr);
For the general case, I came up with two possible solutions:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Rotating an array
by moritz (Cardinal) on Jun 17, 2011 at 11:38 UTC | |
|
Re: Rotating an array
by BrowserUk (Patriarch) on Jun 17, 2011 at 12:19 UTC | |
by Anonymous Monk on Jun 17, 2011 at 12:24 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2011 at 12:40 UTC | |
by Anonymous Monk on Jun 17, 2011 at 12:51 UTC | |
by rovf (Priest) on Jun 17, 2011 at 12:53 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2011 at 12:59 UTC | |
by rovf (Priest) on Jun 17, 2011 at 14:06 UTC | |
|
Re: Rotating an array
by zentara (Cardinal) on Jun 17, 2011 at 11:40 UTC | |
by rovf (Priest) on Jun 17, 2011 at 12:24 UTC | |
|
Re: Rotating an array
by Anonymous Monk on Jun 17, 2011 at 12:59 UTC |