Shift list elements to left by 3: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|Wren}}: Added alternative using library method.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10:
{{trans|Python}}
<
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))</
{{out}}
Line 23:
=={{header|Action!}}==
<
INT i
Line 65:
Test(a,9,-3)
RETURN</
{{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.
<
procedure Shift_Left is
Line 101:
Put_List (Shift);
end Shift_Left;</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
Line 107:
=={{header|ALGOL 68}}==
<
# shifts in place the elements of a left by n #
PRIO <<:= = 1;
Line 140:
PRINT v;
print( ( newline ) )
END</
{{out}}
<pre>
Line 148:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<
% 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.</
{{out}}
<pre>
Line 190:
===Functional===
====Prefix appended====
<
-- rotated :: Int -> [a] -> [a]
Line 307:
set my text item delimiters to dlm
s
end unlines</
{{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:
<
-- rotated :: Int -> [a] -> [a]
Line 511:
set my text item delimiters to dlm
s
end unlines</
{{Out}}
<pre> Input list: {1, 2, 3, 4, 5, 6, 7, 8, 9}
Line 522:
===Two in-place alternatives===
Fewest moves:
<
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</
Wholly in-place:
<
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</
Result in both cases:
{{output}}
<
=={{header|Arturo}}==
<
print rotate.left lst 3</
{{out}}
Line 630:
=={{header|AutoHotkey}}==
<
nums := Arr.Clone()
loop % n
Line 638:
nums.InsertAt(1, nums.RemoveAt(nums.count()))
return nums
}</
Examples:<
output1 := Shift_list_elements(Arr, "L", 3)
output2 := Shift_list_elements(Arr, "R", 3)
return</
{{out}}
<pre>output1 = [4, 5, 6, 7, 8, 9, 1, 2, 3]
Line 649:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SHIFT_LIST_ELEMENTS_TO_LEFT_BY_3.AWK
BEGIN {
Line 671:
return(str)
}
</syntaxhighlight>
{{out}}
<pre>
Line 684:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<
global lista
dim lista(9)
Line 720:
next i
end
</syntaxhighlight>
{{out}}
<pre>
Line 727:
==={{header|PureBasic}}===
<
DataSection
Line 761:
Next i
Input()
CloseConsole()</
{{out}}
<pre>
Line 768:
==={{header|QBasic}}===
<
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</
{{out}}
<pre>
Line 805:
=={{header|C}}==
{{trans|Wren}}
<
/* in place left shift by 1 */
Line 827:
printf("\n");
return 0;
}</
{{out}}
Line 836:
=={{header|C++}}==
<
#include <iostream>
#include <iterator>
Line 854:
std::cout << " After: ";
print(vec);
}</
{{out}}
Line 863:
=={{header|CLU}}==
<
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 </
{{out}}
<pre>Before: 1 2 3 4 5 6 7 8 9
Line 898:
=={{header|Common Lisp}}==
<
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)</
=={{header|Delphi}}==
Line 908:
{{libheader| Boost.Int}}
Boost.Int is part of [https://github.com/MaiconSoft/DelphiBoostLib DelphiBoostLib].
<syntaxhighlight lang="delphi">
program Shift_list_elements_to_left_by_3;
Line 926:
writeln('Shifted left by 3 :', list.ToString);
Readln;
end.</
{{out}}
<pre>Original list :[1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 939:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(n,
LAMBDA(xs,
Line 957:
)
)
)</
{{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:
<
=LAMBDA(n,
LAMBDA(xs,
Line 1,023:
)
)
)</
{{Out}}
{| class="wikitable"
Line 1,068:
=={{header|F_Sharp|F#}}==
<
// 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>
{{out}}
<pre>
Line 1,081:
=={{header|Factor}}==
<
{ 1 2 3 4 5 6 7 8 9 } 3 rotate .</
{{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.
<
{ 1 2 3 4 5 6 7 8 9 } 3 <rotated> >array .</
{{out}}
<pre>
Line 1,100:
=={{header|FreeBASIC}}==
<
'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</
{{out}}<pre>4, 5, 6, 7, 8, 9, 1, 2, 3</pre>
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 1,151:
}
fmt.Println("Shifted left by 3 :", l)
}</
{{out}}
Line 1,160:
=={{header|Haskell}}==
<
rotated :: Int -> [a] -> [a]
Line 1,180:
>> putStrLn "\nRotated 3 or 30 positions to the right:"
>> print (rotated (-3) xs)
>> print (rotated (-30) xs)</
{{Out}}
<pre>Initial list: [1,2,3,4,5,6,7,8,9]
Line 1,193:
=={{header|Java}}==
<
import java.util.Arrays;
import java.util.Collections;
Line 1,205:
}
}
</syntaxhighlight>
{{Out}}
<pre>original: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 1,211:
=={{header|JavaScript}}==
<
"use strict";
Line 1,271:
// MAIN ---
return main();
})();</
{{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'''
<
def rotateLeft($n):
.[$n:] + .[:$n];</
Examples:
<pre>
Line 1,290:
</pre>
=={{header|Julia}}==
<
println(list, " => ", circshift(list, -3))
</
<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}}==
<
{{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.
<
let list = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
echo list, " → ", rotatedLeft(list, 3)</
{{out}}
Line 1,314:
=={{header|Perl}}==
<
use strict;
Line 1,324:
push @list, splice @list, 0, $n;
print join ' ', @list, "\n"</
{{out}}
<pre>
Line 1,331:
=={{header|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>
<!--</
{{out}}
<pre>
Line 1,342:
=={{header|PicoLisp}}==
<
(println (conc (tail -3 L) (head 3 L))) )</
=={{header|Python}}==
<
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))
</
<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:
<
from itertools import cycle, islice
Line 1,417:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>List rotated 3 positions to the left:
Line 1,430:
=={{header|Quackery}}==
<
{{out}}
Line 1,437:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
say [1..9].rotate(3)</
{{out}}
<pre>
Line 1,446:
=={{header|REXX}}==
<
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 " " " */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,465:
=={{header|Ring}}==
<
see "working..." + nl
Line 1,502:
txt = txt + "]"
see txt
</syntaxhighlight>
{{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.
<
p list.rotate(3)
</
<pre>
[4, 5, 6, 7, 8, 9, 1, 2, 3]
Line 1,523:
=={{header|Rust}}==
<
let mut v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
println!("Before: {:?}", v);
v.rotate_left(3);
println!(" After: {:?}", v);
}</
{{out}}
Line 1,537:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<
; 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))))))</
{{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}}==
<
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)")</
{{out}}
Line 1,577:
{{libheader|Wren-seq}}
Alternatively, using a library method to produce the same output as before.
<
var l = (1..9).toList
System.print("Original list : %(l)")
System.print("Shifted left by 3 : %(Lst.lshift(l, 3))")</
=={{header|XPL0}}==
<
[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));
]</
{{out}}
|