Shift list elements to left by 3: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Wren}}: Added alternative using library method.)
m (syntax highlighting fixup automation)
Line 10:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F rotate(l, n)
V k = (l.len + n) % l.len
R l[k ..] [+] l[0 .< k]
 
V l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l‘ => ’rotate(l, 3))</langsyntaxhighlight>
 
{{out}}
Line 23:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Line 65:
 
Test(a,9,-3)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Shift_list_elements_to_left_by_3.png Screenshot from Atari 8-bit computer]
Line 75:
=={{header|Ada}}==
Using slices and array concatenation.
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Shift_Left is
Line 101:
 
Put_List (Shift);
end Shift_Left;</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
Line 107:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
# shifts in place the elements of a left by n #
PRIO <<:= = 1;
Line 140:
PRINT v;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 148:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="pascal">begin
% increments a and returns the new value %
integer procedure inc ( integer value result a ) ; begin a := a + 1; a end;
Line 181:
write()
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 190:
===Functional===
====Prefix appended====
<langsyntaxhighlight lang="applescript">------------- SHIFT LIST ELEMENTS TO LEFT BY 3 -----------
 
-- rotated :: Int -> [a] -> [a]
Line 307:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre> Input list: {1, 2, 3, 4, 5, 6, 7, 8, 9}
Line 316:
====Cycle sampled====
Another approach: drawing from an infinite list of cycles:
<langsyntaxhighlight lang="applescript">------------- SHIFT LIST ELEMENTS TO LEFT BY 3 -----------
 
-- rotated :: Int -> [a] -> [a]
Line 511:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre> Input list: {1, 2, 3, 4, 5, 6, 7, 8, 9}
Line 522:
===Two in-place alternatives===
Fewest moves:
<langsyntaxhighlight lang="applescript">(* List range rotation, in-place with temporary external storage. Negative 'amount' = left rotation, positive = right.
Method:
Adjust the specified amount to get a positive, < list length, left-rotation figure.
Line 559:
-- Left-rotate all items (1 thru -1) by three places.
rotate(lst, 1, -1, -3)
return lst</langsyntaxhighlight>
 
Wholly in-place:
<langsyntaxhighlight lang="applescript">(* List range rotation, wholly in-place. Negative 'amount' = left rotation, positive = right.
Method:
Adjust the specified rotation amount to get a positive, < list length, left-rotation figure.
Line 614:
-- Left-rotate all items (1 thru -1) by three places.
rotate(lst, 1, -1, -3)
return lst</langsyntaxhighlight>
 
Result in both cases:
{{output}}
<langsyntaxhighlight lang="applescript">{4, 5, 6, 7, 8, 9, 1, 2, 3}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">lst: [1 2 3 4 5 6 7 8 9]
print rotate.left lst 3</langsyntaxhighlight>
 
{{out}}
Line 630:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Shift_list_elements(Arr, dir, n){
nums := Arr.Clone()
loop % n
Line 638:
nums.InsertAt(1, nums.RemoveAt(nums.count()))
return nums
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">Arr := [1,2,3,4,5,6,7,8,9]
output1 := Shift_list_elements(Arr, "L", 3)
output2 := Shift_list_elements(Arr, "R", 3)
return</langsyntaxhighlight>
{{out}}
<pre>output1 = [4, 5, 6, 7, 8, 9, 1, 2, 3]
Line 649:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SHIFT_LIST_ELEMENTS_TO_LEFT_BY_3.AWK
BEGIN {
Line 671:
return(str)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 684:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">arraybase 1
global lista
dim lista(9)
Line 720:
next i
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 727:
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Global Dim lista.i(9)
 
DataSection
Line 761:
Next i
Input()
CloseConsole()</langsyntaxhighlight>
{{out}}
<pre>
Line 768:
 
==={{header|QBasic}}===
<langsyntaxhighlight QBasiclang="qbasic">DIM SHARED lista(1 TO 9)
DATA 1,2,3,4,5,6,7,8,9
FOR i = 1 TO 9
Line 796:
IF i < 9 THEN PRINT ", ";
NEXT i
END</langsyntaxhighlight>
{{out}}
<pre>
Line 805:
=={{header|C}}==
{{trans|Wren}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
/* in place left shift by 1 */
Line 827:
printf("\n");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 836:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 854:
std::cout << " After: ";
print(vec);
}</langsyntaxhighlight>
 
{{out}}
Line 863:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Shift an array left by a certain amount
shift_left = proc [T: type] (a: array[T], n: int)
at = array[T]
Line 892:
shift_left[int](arr, 3)
stream$puts(po, " After: ") show(po, arr)
end start_up </langsyntaxhighlight>
{{out}}
<pre>Before: 1 2 3 4 5 6 7 8 9
Line 898:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">;; Load the alexandria library from the quicklisp repository
CL-USER> (ql:quickload "alexandria")
 
CL-USER> (alexandria:rotate '(1 2 3 4 5 6 7 8 9) -3)
(4 5 6 7 8 9 1 2 3)</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 908:
{{libheader| Boost.Int}}
Boost.Int is part of [https://github.com/MaiconSoft/DelphiBoostLib DelphiBoostLib].
<syntaxhighlight lang="delphi">
<lang Delphi>
program Shift_list_elements_to_left_by_3;
 
Line 926:
writeln('Shifted left by 3 :', list.ToString);
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Original list :[1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 939:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">rotatedRow
=LAMBDA(n,
LAMBDA(xs,
Line 957:
)
)
)</langsyntaxhighlight>
{{Out}}
The formula in cell B2 defines an array which populates the whole range '''B2:J2'''
Line 1,003:
 
Or, in terms of the MAKEARRAY function:
<langsyntaxhighlight lang="lisp">rotated
=LAMBDA(n,
LAMBDA(xs,
Line 1,023:
)
)
)</langsyntaxhighlight>
{{Out}}
{| class="wikitable"
Line 1,068:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Nigel Gallowy. March 29th., 2021
let fN=function _::_::_::n->n |_->raise(System.Exception("List has fewer than 3 elements"))
[[1..10];[1;2];[1;2;3]]|>Seq.iter(fun n->try printfn "%A->%A" n (fN n) with :? System.Exception as e->printfn "oops -> %s" (e.Message))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,081:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor"> USING: prettyprint sequences.extras ;
 
{ 1 2 3 4 5 6 7 8 9 } 3 rotate .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,091:
You can also make a virtual rotated sequence. In other words, the underlying array remains the same, but the way it is indexed has been changed.
 
<langsyntaxhighlight lang="factor">USING: arrays prettyprint sequences.rotated ;
 
{ 1 2 3 4 5 6 7 8 9 } 3 <rotated> >array .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,100:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">sub shift_left( list() as integer )
'shifts list left by one step
dim as integer lo = lbound(list), hi = ubound(list), first
Line 1,123:
print list(i);
if i<9 then print ", ";
next i</langsyntaxhighlight>
{{out}}<pre>4, 5, 6, 7, 8, 9, 1, 2, 3</pre>
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,151:
}
fmt.Println("Shifted left by 3 :", l)
}</langsyntaxhighlight>
 
{{out}}
Line 1,160:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">------------- SHIFT LIST ELEMENTS TO LEFT BY N -----------
 
rotated :: Int -> [a] -> [a]
Line 1,180:
>> putStrLn "\nRotated 3 or 30 positions to the right:"
>> print (rotated (-3) xs)
>> print (rotated (-30) xs)</langsyntaxhighlight>
{{Out}}
<pre>Initial list: [1,2,3,4,5,6,7,8,9]
Line 1,193:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.List;
import java.util.Arrays;
import java.util.Collections;
Line 1,205:
}
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>original: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,211:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,271:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre> The input list: [1,2,3,4,5,6,7,8,9]
Line 1,280:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># input should be an array or string
def rotateLeft($n):
.[$n:] + .[:$n];</langsyntaxhighlight>
Examples:
<pre>
Line 1,290:
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
println(list, " => ", circshift(list, -3))
</langsyntaxhighlight>{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9] => [4, 5, 6, 7, 8, 9, 1, 2, 3]
Line 1,298:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">RotateLeft[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 3]</langsyntaxhighlight>
{{out}}
<pre>{4, 5, 6, 7, 8, 9, 1, 2, 3}</pre>
Line 1,305:
Using “rotatedLeft” from standard module “algorithm”. Note that its exists also “rotateLeft” for rotation in place.
 
<langsyntaxhighlight Nimlang="nim">import algorithm
 
let list = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
echo list, " → ", rotatedLeft(list, 3)</langsyntaxhighlight>
 
{{out}}
Line 1,314:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">// 20210315 Perl programming solution
 
use strict;
Line 1,324:
push @list, splice @list, 0, $n;
 
print join ' ', @list, "\n"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,331:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">..$]&</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,342:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let L (range 1 9)
(println (conc (tail -3 L) (head 3 L))) )</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def rotate(list, n):
for _ in range(n):
list.append(list.pop(0))
Line 1,361:
list = [1,2,3,4,5,6,7,8,9]
print(list, " => ", rotate(list, 3))
</langsyntaxhighlight>{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9] => [4, 5, 6, 7, 8, 9, 1, 2, 3]
Line 1,369:
and we can also define rotated lists and ranges as slices taken from an infinite cycle of their values, after n initial items have been dropped:
 
<langsyntaxhighlight lang="python">'''Shift list elements to left by 3'''
 
from itertools import cycle, islice
Line 1,417:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>List rotated 3 positions to the left:
Line 1,430:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery">' [ 1 2 3 4 5 6 7 8 9 ] 3 split swap join echo</langsyntaxhighlight>
 
{{out}}
Line 1,437:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line># 20210315 Raku programming solution
 
say [1..9].rotate(3)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,446:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program shifts (using a rotate) elements in a list left by some number N. */
parse arg n $ . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 3 /*Not specified? Then use the default.*/
Line 1,456:
say 'shifting elements in the list by ' n /*display action used on the input list*/
say ' input list=' $ /* " the input list ───► terminal*/
say 'output list=' $$ /* " the output " " " */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,465:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
 
Line 1,502:
txt = txt + "]"
see txt
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,515:
=={{header|Ruby}}==
Note, unlike some other languages, for Ruby's Array.rotate() method, a positive number means rotate to the left, while a negative number means rotate to the right. Use Array.rotate!() if you wish to mutate the original array rather than return a new one.
<langsyntaxhighlight lang="ruby">list = [1,2,3,4,5,6,7,8,9]
p list.rotate(3)
</langsyntaxhighlight>{{out}}
<pre>
[4, 5, 6, 7, 8, 9, 1, 2, 3]
Line 1,523:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
println!("Before: {:?}", v);
v.rotate_left(3);
println!(" After: {:?}", v);
}</langsyntaxhighlight>
 
{{out}}
Line 1,537:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<langsyntaxhighlight lang="scheme">; Rotate a list by the given number of elements.
; Positive = Rotate left; Negative = Rotate right.
 
Line 1,549:
(list-rotate (append (list (list-ref lst end)) (list-head lst end)) (1+ n))))
(else
(list-rotate (cdr (append lst (list (car lst)))) (1- n))))))</langsyntaxhighlight>
{{out}}
<pre>(list-rotate '(1 2 3 4 5 6 7 8 9) 3) --> (4 5 6 7 8 9 1 2 3)
Line 1,555:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">// in place left shift by 1
var lshift = Fn.new { |l|
var n = l.count
Line 1,567:
System.print("Original list : %(l)")
for (i in 1..3) lshift.call(l)
System.print("Shifted left by 3 : %(l)")</langsyntaxhighlight>
 
{{out}}
Line 1,577:
{{libheader|Wren-seq}}
Alternatively, using a library method to produce the same output as before.
<langsyntaxhighlight lang="ecmascript">import "./seq" for Lst
var l = (1..9).toList
System.print("Original list : %(l)")
System.print("Shifted left by 3 : %(Lst.lshift(l, 3))")</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int A, N, T, I;
[A:= [1, 2, 3, 4, 5, 6, 7, 8, 9];
for N:= 1 to 3 do \rotate A left 3 places
Line 1,593:
[IntOut(0, A(I)); Text(0, ", ")];
IntOut(0, A(I));
]</langsyntaxhighlight>
 
{{out}}
10,327

edits