Shift list elements to left by 3: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added C++ solution)
m (Minor edit to C++ code)
Line 52: Line 52:
template <typename T>
template <typename T>
void print(const std::vector<T>& v) {
void print(const std::vector<T>& v) {
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << '\n';
std::cout << '\n';
}
}

Revision as of 15:43, 14 March 2021

Shift list elements to left by 3 is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Shift list elements to left by 3.


shift = [1,2,3,4,5,6,7,8,9]
result = [4, 5, 6, 7, 8, 9, 1, 2, 3]

ALGOL 68

<lang algol68>BEGIN

   # shifts in place the elements of a left by n                          #
   PRIO <<:= = 1;
   OP   <<:= = ( REF[]INT a, INT n )REF[]INT:
        BEGIN
           INT    a length = ( UPB a - LWB a ) + 1;
           IF n < a length AND n > 0 THEN
               # the amount to shift is less than the length of the array #
               [ 1 : a length ]INT shifted;
               shifted[ 1                    : a length - n ] := a[ n + 1 :   @ 1 ];
               shifted[ ( a length + 1 ) - n :              ] := a[ 1     : n @ 1 ];
               a[ @ 1 ] := shifted
           FI;
           a
        END # <<:= # ;
   # prints the elements of v                                             #
   OP   PRINT = ( []INT v )VOID:
        BEGIN
           print( ( "[" ) );
           BOOL need comma := FALSE;
           FOR i FROM LWB v TO UPB v DO
               print( ( IF need comma THEN "," ELSE "" FI, whole( v[ i ], 0 ) ) );
               need comma := TRUE
           OD;
           print( ( "]" ) )
        END # PRINT # ;            
   [ 1 : 9 ]INT v := ( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
   PRINT v;
   print( ( " -> " ) );
   v <<:= 3;
   PRINT v;
   print( ( newline ) )

END</lang>

Output:
[1,2,3,4,5,6,7,8,9] -> [4,5,6,7,8,9,1,2,3]

C++

<lang cpp>#include <algorithm>

  1. include <iostream>
  2. include <iterator>
  3. include <vector>

template <typename T> void print(const std::vector<T>& v) {

   std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
   std::cout << '\n';

}

int main() {

   std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
   std::cout << "Before: ";
   print(vec);
   std::rotate(vec.begin(), vec.begin() + 3, vec.end());
   std::cout << " After: ";
   print(vec);

}</lang>

Output:
Before: 1 2 3 4 5 6 7 8 9 
 After: 4 5 6 7 8 9 1 2 3 

Factor

In place

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: kernel math math.ranges prettyprint sequences ;

rotate! ( seq n -- seq' )
   over length 1 - [ [a..b] ] [ <iota> ] bi
   pick [ exchange ] curry 2each ;

{ 1 2 3 4 5 6 7 8 9 } 3 rotate! .</lang>

Output:
{ 4 5 6 7 8 9 1 2 3 }

Non-mutating

<lang factor> USE: sequences.extras

{ 1 2 3 4 5 6 7 8 9 } 3 rotate</lang>

Virtual rotated sequence

In other words, the underlying array remains the same, but the way it is indexed has been changed.

<lang factor>USE: sequences.rotated

{ 1 2 3 4 5 6 7 8 9 } 3 <rotated></lang>

Virtual circular sequence

The starting index can be changed, effectively rotating it, and it can be indexed beyond its length, wrapping around to the beginning.

<lang factor>USING: circular kernel ;

3 { 1 2 3 4 5 6 7 8 9 } <circular> [ change-circular-start ] keep</lang>

Julia

<lang julia>list = [1, 2, 3, 4, 5, 6, 7, 8, 9] println(list, " => ", circshift(list, -3))

</lang>

Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] => [4, 5, 6, 7, 8, 9, 1, 2, 3]

Ring

<lang ring> see "working..." + nl

shift = [1,2,3,4,5,6,7,8,9] lenshift = len(shift) shiftNew = list(lenshift) nshift = 3 temp = list(nshift)

see "original list:" + nl showArray(shift) see nl + "Shifted left by 3:" + nl

for n = 1 to nshift

   temp[n] = shift[n]

next

for n = 1 to lenshift - nshift

   shiftNew[n] = shift[n+nshift]

next

for n = lenshift-nshift+1 to lenshift

   shiftNew[n] = temp[n-lenshift+nshift]

next

showArray(shiftNew)

see nl + "done..." + nl

func showArray(array)

    txt = "["
    for n = 1 to len(array)
        txt = txt + array[n] + ", "
    next
    txt = left(txt,len(txt)-2)
    txt = txt + "]"
    see txt

</lang>

Output:
working...
original list:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Shifted left by 3:
[4, 5, 6, 7, 8, 9, 1, 2, 3]
done...

Wren

<lang ecmascript>// in place left shift by 1 var lshift = Fn.new { |l|

   var n = l.count
   if (n < 2) return
   var f = l[0]
   for (i in 0..n-2) l[i] = l[i+1]
   l[-1] = f

}

var l = (1..9).toList System.print("Original list  : %(l)") for (i in 1..3) lshift.call(l) System.print("Shifted left by 3 : %(l)")</lang>

Output:
Original list     : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Shifted left by 3 : [4, 5, 6, 7, 8, 9, 1, 2, 3]