| Bytes | Lang | Time | Link |
|---|---|---|---|
| 043 | Tcl | 211130T004637Z | sergiol |
| 011 | Dyalog APL | 241205T192347Z | Aaron |
| 006 | Uiua | 241205T142401Z | nyxbird |
| 037 | AWK | 241202T191133Z | xrs |
| 039 | Clojure | 240923T080104Z | Martin P |
| 082 | Turing Machine | 240922T184128Z | z.. |
| 025 | Arturo | 221111T144911Z | chunes |
| 034 | BASH | 240912T150855Z | john |
| 040 | C clang | 240912T140248Z | jdt |
| 012 | Qdeql | 240911T224348Z | Bbrk24 |
| nan | Pxem rpxem v0.0.7 | 240910T131939Z | 鳴神裁四点一号 |
| 013 | Labyrinth | 240318T064223Z | Bubbler |
| 048 | Python 3 | 240222T023041Z | Larry Ba |
| 007 | Uiua SBCS | 231105T170326Z | chunes |
| 063 | Rockstar | 230622T114654Z | Shaggy |
| 002 | Vyxal | 231107T162349Z | pacman25 |
| 017 | TIBasic | 211110T231300Z | Youserna |
| 131 | Whitespace | 231105T194108Z | Kamila S |
| 039 | Python 3.8 prerelease | 230623T153935Z | Hunaphu |
| 036 | Prolog | 230623T140844Z | coredump |
| 007 | Pip xp | 230306T164101Z | Baby_Boy |
| nan | 230306T170455Z | The Thon | |
| 037 | rSNBATWPL | 221111T152613Z | rydwolf |
| 035 | Knight | 220807T004956Z | naffetS |
| 039 | ECMAScript 2015 with Lodash Array | 220719T202418Z | Eric Xue |
| 038 | Desmos | 220717T071143Z | MathEnth |
| 005 | Brachylog | 220720T112650Z | Fatalize |
| 010 | sed n | 220717T133241Z | Jonah |
| nan | 220717T061337Z | Czylabso | |
| 008 | Perl 5 + p | 220715T104053Z | Dom Hast |
| 036 | Nibbles | 220714T165134Z | Dominic |
| 4010 | MMIX | 220714T224551Z | NoLonger |
| 015 | APL | 220714T155625Z | Vadim Tu |
| 087 | x8616 machine code | 211113T143308Z | 640KB |
| 004 | Husk | 220104T234557Z | Natte |
| 029 | Factor + sequences.merged | 211110T173917Z | chunes |
| 009 | BQN | 211209T155018Z | ovs |
| 038 | Desmos | 211111T065330Z | Aiden Ch |
| 027 | Pari/GP | 211111T023457Z | alephalp |
| 022 | Wolfram Language Mathematica | 211110T183630Z | att |
| 067 | CLCINTERCAL | 211113T154143Z | user1004 |
| 050 | C++ gcc | 211115T170143Z | jdt |
| 006 | Burlesque | 211118T140537Z | DeathInc |
| 011 | Vim | 211118T215952Z | Aaroneou |
| 047 | Rust v1.37.0 | 211110T181753Z | Mayube |
| 032 | TSQL | 211112T135303Z | t-clause |
| 048 | Coconut | 211113T192818Z | Alexandr |
| 041 | AWK | 211112T235715Z | Pedro Ma |
| nan | Pxem | 211113T155137Z | user1004 |
| 022 | 4 | 211112T225231Z | Conor O& |
| 007 | convey | 211110T174553Z | xash |
| 003 | ><> | 211110T191251Z | emanresu |
| 039 | Python 2 | 211111T153627Z | ElPedro |
| 037 | Red | 211111T080710Z | Galen Iv |
| 009 | J | 211112T075140Z | Galen Iv |
| 007 | Ly | 211111T014339Z | cnamejj |
| 065 | Whitespace | 211111T134404Z | Kevin Cr |
| 010 | K oK | 211111T135304Z | mkst |
| nan | Pure Bash | 211111T104203Z | user1004 |
| 026 | Ruby | 211111T074959Z | G B |
| 047 | Icon | 211111T082810Z | Galen Iv |
| 004 | 05AB1E | 211111T080714Z | Kevin Cr |
| 030 | Python 2 | 211111T074738Z | xnor |
| 029 | R | 211111T021654Z | Giuseppe |
| 115 | Core Maude | 211111T013255Z | Chris Bo |
| 031 | Python 3 + numpy | 211111T001703Z | loopy wa |
| 009 | brainfuck | 211111T001108Z | dingledo |
| 004 | Japt | 211111T000157Z | AZTECCO |
| 022 | Haskell | 211110T233716Z | AZTECCO |
| 017 | Raku | 211110T172929Z | Sean |
| 010 | CJam | 211110T231652Z | Luis Men |
| 035 | Python 3.8 prerelease | 211110T231627Z | loopy wa |
| 098 | Rockstar | 211110T210121Z | Mayube |
| 010 | Add++ | 211110T191014Z | caird co |
| 031 | PowerShell | 211110T190348Z | user3141 |
| 007 | Pip rp | 211110T190006Z | DLosc |
| 042 | C clang | 211110T171340Z | Noodle9 |
| 057 | Add++ | 211110T170625Z | Fmbalbue |
| 023 | JavaScript ES6 | 211110T175413Z | rydwolf |
| 005 | Charcoal | 211110T175246Z | Neil |
| 002 | Vyxal r | 211110T174819Z | Aaroneou |
| 026 | C# .NET Core with Linq | 211110T173425Z | Mayube |
| 018 | Zsh | 211110T173623Z | pxeger |
| 006 | ayr | 211110T173119Z | ZippyMag |
| 017 | APL+WIN | 211110T172958Z | Graham |
| 034 | Python 3 | 211110T171359Z | m90 |
| 007 | Pyth | 211110T171300Z | wasif |
| 011 | Grok | 211110T165736Z | Aaroneou |
| 036 | Python 3 | 211110T164738Z | hyperneu |
| 021 | Perl 5 p | 211110T164640Z | Kjetil S |
| 004 | Vyxal | 211110T164255Z | Aaroneou |
| 004 | Jelly | 211110T164111Z | hyperneu |
Tcl, 43 bytes
proc S L {join [lmap a\ b $L {list $b $a}]}
proc S L {lmap a\ b $L {lappend M $b $a}
set M}
Dyalog APL, 11 chars
,⌽⍤⊢⌺(⍪2 2)
A function, à la:
f←,⌽⍤⊢⌺(⍪2 2)
f 1 2 3 4 5 6
2 1 4 3 6 5
AWK, 37 bytes
{for(;i++<NF;i++)printf$(i+1)FS$i" "}
Bonus AWK/Regex 42 bytes
$0=gensub(/([^,]+),([^,]+)/,"\\2,\\1","g")
Clojure, 39 bytes
#(mapcat(fn[[a b]][b a])(partition 2%))
Examples:
(#(mapcat(fn[[a b]][b a])(partition 2%)) [1,2,3,4,5,6])
=> (2 1 4 3 6 5)
(#(mapcat(fn[[a b]][b a])(partition 2%)) [0,1,0,1])
=> (1 0 1 0)
Turing Machine, 82 bytes
a * * b _ *^ > <
b * * c _ *^ > >
c * * c * * > >
c * _ a * *^ < >
c _ * d * * - -
C (clang), 40 bytes
f(*l,n){*l=*l<<16|*l>>16;n&&f(l+1,n-1);}
The function treats pairs of short values (each 16 bits) as a single 32-bit int and swaps their halves.
Qdeql, 12 bytes
Two different twelve-byte solutions:
&&\**\/\/&&/
&\&=\/\/**&/
Both take input as a sequence of bytes from stdin, write the output in the same way to stdout, and terminate on EOF or upon encountering a null terminator (since those are impossible to tell apart in Qdeql).
Example:
$ ./run_qdeql.sh '&\&=\/\/**&/' 'abcdef'
badcfe
Pxem (rpxem v0.0.7), 0 + 15 = 15 bytes
Golfed off 3 bytes. Expect list of code points.
Filename
.w.p.i.i.c[\x01].+.a
Labyrinth, 13 bytes
??!
; \
,\!
@
Takes a list of integers separated by any non-numeric non-null single byte, and prints newline-separated integers. It is important that the input does NOT have a trailing separator, since it is used to detect the end of input.
Loop: ??!\!\,;
?? Take two integers A and B [A B]
!\!\ Print B, newline, A, newline
, Take the next separator, -1 on EOF
; Continue loop if not EOF; discard the separator
End: @ (Halt)
Python 3, 48 bytes
f=lambda l:l[::-1][:2]+f(l[2:])if len(l)>1else l
Very simple. For some reason l[:2:-1] doesn't work.
Uiua SBCS, 7 bytes
♭≡⇌↯∞_2
-1 thanks to Bubbler
♭≡⇌↯∞_2
↯∞_2 # reshape into matrix with two columns
≡⇌ # reverse columns
♭ # deshape
Rockstar, 63 bytes
Takes input as a space separated string, outputs each element on its own line. 9 bytes could be saved if we were guaranteed every element of the array was a single digit by taking input with no delimiting spaces.
listen to S
cut S with " "
while S
roll S in N
say roll S
say N
Try it (Code will need to be pasted in)
listen to S :Read the input string into variable S
cut S with " " :Split S to an array on spaces
while S :While S is not empty
roll S in N : Pop the first element from S into variable N
say roll S : Pop the first element from S and output it
say N : Output N
TI-Basic, 19 17 bytes
seq(Ans(I-i²^I),I,1,dim(Ans
i represents the imaginary number.
Takes input in Ans. Output is stored in Ans and is displayed.
-2 bytes thanks to MarcMush.
Whitespace, 131 bytes
Generated from the following asm2ws program:
2 / GETN / 2 / RCL / 2 / DIV
@LOOP
DUP / BZ %END
0 / GETN
1 / GETN
1 / RCL / PUTN
32 / PUTC
0 / RCL / PUTN
32 /PUTC
1 / SUB / B %LOOP
@END
Prolog - 36 bytes
s([],[]).s([A,B|L],[B,A|R]):-s(L,R).
Non code-golf:
;; swap empty list is empty list
swap([],[]).
;; swap of A,B followed by L is B,A followed by R, where swap(L,R).
swap([A,B|L],[B,A|R]):-swap(L,R).
Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes
ZlrzZS
Explanation
ZlrzZS # Implicit input [1, 2, 3, 4, 5, 6]
Zl # Uninterleave [[1, 3, 5], [2, 4, 6]]
r # Reverse [[2, 4, 6], [1, 3, 5]]
zZ # Zip [[2, 1], [4, 3], [6, 5]]
S # Sum (Flatten) [2, 1, 4, 3, 6, 5]
rSNBATWPL, 37 bytes
a~(a cond (a{1},0 a)+{a sfro 2}){+for
This is very golfed. It is "bad rSNBATWPL" in that it abuses the syntax very heavily. The ungolfed version of this is:
f = a ~ cond{a} {
(a{1}, a{0}) conc f{a sfro 2}
} { cast.array{""} }
This is a simple recursive lambda function, which iterates pair-by-pair. Some golfs I made to this were:
- Removing whitespace and the unnecessary trailing
}, of course - Substituting
cast.array{""}for+for, a very strange way of generating an empty array (works by iterating with a function,+in this case, over an empty array generated whenforcasts its right input, which isNull, to an array) - Replacing
conc, the concat function, with+ - Replacing
a{0}with0 a, which callsaas a curried binary function. Its second argument isNull, and since calling any primitive value as a function withNullas its input returns it unchanged, this saves a byte for little cost - Running
condas a binary function. Since rSNBATWPL functions/built-ins are curried, this saves a byte over applying the function three times (which is the traditional way of providing three arguments) - Replacing
f=andf{...}with an implicit recursive call..is a variable which always refers to the current function, sof{...}can be replaced with.{...}to remove thef=, and any time there's a{...}without a function name, an implicit.is added, allowing me to save another byte by putting it after the+
ECMAScript 2015 with Lodash (Array), 39 bytes
l=>_.chunk(l,2).flatMap(s=>[s[1],s[0]])
Solution below works if array doesn't chunk evenly (l.length % 2 == 1)
l=>_.chunk(l,2).flatMap(s=>s.length-1?[s[1],s[0]]:s)
Desmos, 42 38 bytes
-4 bytes thanks to @aiden's suggestion and wackscope variables
l=[1...L.length]
f(L)=L[l-1+2mod(l,2)]
Brachylog, 5 bytes
ġ₂↔ᵐc
Explanation
ġ₂ Split the list in groups of 2 consecutive elements
↔ᵐ Map reverse
c Concatenate
sed -n, 10 bytes
2~2{G;p};h
2~2{G;p}For even numbered lines, append hold space into pattern space and printhFor all lines, replace hold space with pattern space. This means that the hold space will always contain the previous, odd-numbered line when we are on an even-numbered line.
Perl 5 + -p, 8 bytes
This sets $_ (implicitly printed by -p) to the next line of STDIN (<> - which isn't trimmed via chomp) concatenated with the current line ($_).
$_=<>.$_
or (with -nl -M5.10.0):
say<>.$_
Perl 5 + -p -M5.10.0, 8 bytes
This outputs (say) the next line of STDIN (<>), after converting to a number ($-=) to avoid the trailing newline, then the current line is output via -p.
say$-=<>
Nibbles, 3.5 3 bytes (6 nibbles)
\+\`/~
`/ # chunks of input
~ # (using default size = 2),
\ # reverse,
+ # concatenate,
\ # and reverse again.
MMIX, 40 bytes (10 instrs)
Assumes 8-byte size for list elements.
void __mmixware swao(uint64_t *l, size_t len)
hexdump (jxd -T):
00000000: 42010009 3f010101 27010101 8fff0000 B¢¡µ?¢¢¢'¢¢¢Ɓ”¡¡
00000010: 8f020008 afff0008 af020000 e7000010 Ɓ£¡®Ḥ”¡®Ḥ£¡¡ḃ¡¡Ñ
00000020: 5b01fffa f8000000 [¢”«ẏ¡¡¡
Disassembled:
swae BZ $1,1F // if nothing, return
SRU $1,$1,1 // turn length into loop count
0H SUBU $1,$1,1 // decrement loop count
LDOU $255,$0,0 // load l[0]
LDOU $2,$0,8 // load l[1]
STOU $255,$0,8 // store the other way
STOU $2,$0,0 // store the other way
INCL $0,16 // l += 2
PBNZ $1,0B // if loop count isn't yet 0, loop back
1H POP 0,0 // return
APL, 15 bytes
{,⌽⍵⍴⍨(2÷⍨≢⍵)2}
The same idea as in the existing solution, but implemented as a separate function and two bytes less.
x86-16 machine code, 8 7 bytes
00000000: aca4 aa49 e2fa c3 ...I...
Listing:
TWO_LOOP:
AC LODSB ; load first byte (into AL)
A4 MOVSB ; move second byte into first byte
AA STOSB ; store first byte (from AL) into second byte
49 DEC CX ; only loop half as many times as length
E2 F9 LOOP TWO_LOOP
C3 RET
Callable function: input list at [SI], output to [DI] length in CX.
-1 bytes thx to @m90!
Factor + sequences.merged, 34 29 bytes
[ 2 group unzip swap 2merge ]
Explanation
! { 1 2 3 4 5 6 }
2 group ! { { 1 2 } { 3 4 } { 5 6 } }
unzip ! { 1 3 5 } { 2 4 6 }
swap ! { 2 4 6 } { 1 3 5 }
2merge ! { 2 1 4 3 6 5 }
BQN, 10 9 bytesSBCS
-1 byte thanks to Razetime!
⥊·⌽˘∘‿2⊸⥊
∘‿2⥊𝕩 Reshape the input into a matrix with 2 columns and and the necessary number of rows.
⌽˘ Reverse horizontally (Swap the 2 columns).
⥊ Flatten into a vector.
An alternative using the Under operator at 10 bytes:
⌽˘⌾(∘‿2⊸⥊)
Desmos, 39 38 bytes
l=[1...L.length]
f(L)=L[l-1+2mod(l,2)]
Wolfram Language (Mathematica), 22 bytes
0<##<1||#2&&#&�@##3&
Input [L...], and returns in an And. Works on integer inputs.
Wolfram Language (Mathematica), 24 bytes
{}=={##}||#2&&#&�@##3&
Input [L...], and returns in an And. Works on non-boolean inputs.
Wolfram Language (Mathematica), 26 bytes
#2~##&~#&@@@#~Partition~2&
Input [L], and returns a list. Works.
CLC-INTERCAL, 95 67 bytes.
Why does CLC-INTERCAL 1.-94.-2 lack !1~.1'? I could have golfed off one byte.
DOCOMEFROM#9(1)DOWRITEIN.1+.2DDOCOMEFROM'.1~.1'~#1(9)DOREADOUT.2+.1
Copy and paste to try it online!
Usage
- Each item of list must be in
ONEtoSIX FIVE FIVE THREE FIVE(inclusive);ZEROfor end of list.- Given from STDIN, delimited with a LF.
- Outputs to STDOUT, as roman number.
Try these inputs
ONE
TWO
ONE
TWO
ZERO
ONE
TWO
THREE
FOUR
FIVE
SIX
ZERO
C++ (gcc), 50 bytes
[](auto&v){for(int&i:v)swap(i,(&i)[int(&i)%8<1]);}
-5 bytes thanks to ceilingcat!
Burlesque, 6 bytes
2co)^p
Improved: Remembered map shorthand
2co # Chunks of 2
)^p # Push in reverse for each block
Burlesque, 7 bytes
2co{}^m
2co # Chunks of 2
{}^m # Push in reverse for each block (strictly push-map with a nop)
Vim, 11 bytes
qqddpj@qq@q
Explanation:
qq @qq@q # Loop:
dd # Delete the line
p # Paste it below the next line
j # Move down a line
Rust v1.37.0, 59 55 47 bytes
|a:&[_]|(0..).zip(a).map(|i|a[i.0^1]).collect()
-2 bytes thanks to ZippyMagician
-2 bytes thanks to alephalpha
-8 bytes thanks to (0..).zip(a)
Rust is not a golfy language...
Port of the C# and JS answers
v1.37.0 is the version available on TIO
Rust stable, 54 bytes
|a:&[_]|a.chunks(2).flat_map(|a|[a[1],a[0]]).collect()
Thanks to ZippyMagician for this slightly golfier answer on the latest version of Rust.
Coconut, 48 bytes
l->(l[1::2],l[:-1:2])|*>zip|>reduce$(+)
Python equivalent:
from functools import reduce
from operator import add
f = lambda l: reduce(add, zip(l[1::2], l[:-1:2]))
AWK, 43 41 bytes
{for(;++n<NF;n++)$n+=$(n+1)-($(n+1)=$n)}1
Thanks for Dominic van Essen for 2 less bytes!
{
for(;++n<NF;n++) Starts a loop though the numbers.
n has to start as 1, because $0 is the whole line.
$n+=$(n+1)-($(n+1)=$n) Swap the numbers two by two.
Readable: a += b - (b = a)
}
1 Prints the line.
Pxem, 18 + 0 = 18 bytes.
The configure is:
- No breaking loop when insufficient items.
- Abort to illegal integer input.
Filename:
.w._._.n .o.n .o.a
Content is empty.
Usage
- With STDIN and STDOUT.
- Each integer shall be given, separated by one or more space characters.
- Outputted separated with a space.
4, 22 bytes
3.72082072152152072094
Readable + explanation:
3. 720 820 721 521 520 720 9 4
^^------------------------------ mandatory prefix
^^^-------------------------- initial input in cell 20
^^^-----------------^---- loop while cell 20 is nonzero
^^^------------------ input to cell 21
^^^-------------- output cell 21
^^^---------- output cell 20
^^^------ input to cell 20
^---- end loop
^-- exit
Corresponding Quartic program:
decl a, b
input a
loop a
input b
print b
print a
input a
end
convey, 7 bytes
I knew I should have implemented SpaceChem's flipflop operator, then this would be 5 bytes. :-)
-6 byte by @Manny Queen
}?{
}~1
{ puts the input list on the conveyor, } prints the result. On the ? the elements try to go down if they can. On the ~ they will wait for 1 tick before going to the output, thus starting with the first item, every second element is delayed for two ticks.
13 bytes
},<
{@^
0"
1^
The lower loop 0"\1^ copies 0 1 0 1 … into choose @. The input is thus split into two paths. The right one takes a little bit longer, so the other number can overtake it before joining , again.
><>, 4 3 bytes
i#o
Lol, ><> ties with beats Jelly & 05AB1E. Terminates with an error.
-1 thanks to @Manny Queen.
How does it work?
The instruction pointer (IP) is currently moving right
i Take input as a character
# Reflect - The IP starts moving left
i Take input again
o Output that
# Reflect - The IP starts moving right again
o Output the first input we took - the inputs are now swapped
And now, we're moving left and back at the start again on i + moving right.
This loops forever (i.e. until erroring when we're out of input)
because there is no halt instruction (;)
This is essentially executing "iioo" in an infinite loop.
Python 2, 49 39 bytes
lambda l:[l[c^1]for c in range(len(l))]
-10 with thanks to @MannyQueen for an awesome tip.
Simply because no-one has submitted a Python 2 answer yet.
Red, 37 bytes
func[a][foreach[b c]a[print[c lf b]]]
Alternative
Much longer but more fun approach using parse:
Red, 59 bytes
func[a][parse a[collect[any[b: skip keep skip keep(b/1)]]]]
Ly, 7 bytes
ir[foo]
Pretty straightforward, but might be worth posting since it's almost pronounceable. :)
ir -- read input codepoints into the stack, reverse the order
[ ] -- while the stack isn't empty
foo -- flip the top two entries and print as characters
Cubix, 14 bytes
..@.i?i.o;o;.^
This is the first time I've tried to use Cubix, so I wouldn't be suprised if it's possible to do this with less code. It's a hard language to explain, since it's a 2D language where the code is wrapped around a cube. So the directions the instruction pointer take a hard to show in a description. I'll include a link to the online interpreter since it has a debug mode when you can see the way the code iterates through the cells.
But I'll try to explain it too...
..@.i?i.o;o;.^ - code before it's wrapped on the cube...
i - (1) starts here, input a codepoint
? - branch if top of stack, <0 "left", >0 "right"
@ - "left" (true on EOF), halt program
^ - "right", set IP direction to "up"
? - same branch, but coming from another direction
i - "right", input another codepoint
o; - output top of stack as char, pop it off stack
o; - repeat... code wraps back to (1)
The . characters are no-ops and are needed to place the other characters in the right position so they will wrap into place on the cube.
Here's a link to the online interpreter http://ethproductions.github.io/cubix/?code=Li5ALmk/aS5vO287Ll4=&input=MTIzNDU2Nzg=&speed=20
Whitespace, 65 bytes
[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T N
T T _Read_STDIN_as_integer][T T T _Retrieve_input1][S N
S _Duplicate_input1][S N
S _Duplicate_input1][T N
T T _Read_STDIN_as_integer][T T T _Retrieve_input2][T N
S T _Print_input2_as_integer][S S S T S T S N
_Push_10][S N
S _Duplicate_10][T N
S S _Print_10_as_character][S N
T _Swap_top_two][T N
S T _Print_input1_as_integer][T N
S S _Print_10_as_character][N
S N
N
_Jump_to_Label_LOOP]
Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.
Whitespace doesn't have lists, so I/O are newline delimited (strictly non-negative†) integers.
Try it online (with raw spaces, tabs and new-lines only).
Explanation in pseudo-code:
LOOP:
Integer a = STDIN as integer
Integer b = STDIN as integer
Print b as integer to STDOUT
Print '\n' as character to STDOUT
Print a as integer to STDOUT
Print '\n' as character to STDOUT
Go to next iteration of LOOP
This will stop with an error automatically when there are no more inputs.
†: For every second input it will use the input before it as heap-address ([SNS_Duplicate_input1][SNS_Duplicate_input1][TNTT_Read_STDIN_as_integer]), so the inputs should be strictly non-negative, since negative heap addresses aren't allowed in Whitespace.
K (oK), 10 bytes
Solution:
,/|:'0N 2#
Explanation:
,/|:'0N 2# / the solution
0N 2# / reshape into n x 2 grid
|:' / reverse (|:) each (')
,/ / flatten
Alternatives:
,/+|+0N 2#- also 10 bytes
Pure Bash, 32 + 1 = 33 bytes
One additional byte for filename specification: x.
echo "$2
$1"
shift 2
(($#))&&. x
Usage
- Input from program arguments.
- Outputs to stdout.
Pure Bash, 34 + 1 = 35 bytes
One additional byte for filename specification: x.
read x
read y||>x
echo "$y
$x"
. x
Usage
- Each item is given from STDIN.
- Outputs to STDOUT.
- Items are separated by a LF.
05AB1E, 4 bytes
2ôí˜
Try it online or verify all test cases.
4 bytes alternatives:
ιRø˜
Try it online or verify all test cases.
2ô€`
Try it online or verify all test cases.
Explanation:
2ô # Split the (implicit) input-list into parts of size 2
# i.e. [1,2,3,4,5,6] → [[1,2],[3,4],[5,6]]
í # Reverse each pair
# → [[2,1],[4,3],[6,5]]
˜ # Flatten the list of pairs
# → [2,1,4,3,6,5]
# (after which this list is output implicitly as result)
ι # Uninterleave the (implicit) input-list, using 2 as implicit second argument
# i.e. [1,2,3,4,5,6] → [[1,3,5],[2,4,6]]
R # Reverse it
# → [[2,4,6],[1,3,5]]
ø # Zip/transpose, swapping rows/columns
# → [[2,1],[4,3],[6,5]]
˜ # Flatten the list of pairs
# → [2,1,4,3,6,5]
# (after which this list is output implicitly as result)
2ô # Split the (implicit) input-list into parts of size 2
# i.e. [1,2,3,4,5,6] → [[1,2],[3,4],[5,6]]
€ # Map over each pair:
` # Pop and push the pair to the stack
# (€` is usually used to flatten a multi-level list one level down. One side-effect
# is that the items are dumped in reversed order, which is what we want in this
# case)
# → [2,1,4,3,6,5]
# (after which this list is output implicitly as result)
Python 2, 30 bytes
def f(a,b,*m):print b,a,;f(*m)
The function f takes input splatted like f(1,2,3,4,5,6), and prints the output space-separated, terminating with error.
34 bytes
lambda l:map(l.pop,len(l)/2*[1,0])
This works by alternating popping the elements at index 1 and index 0.
Core Maude, 115 bytes
mod S is pr LIST{Nat}. op f : Nat ~> Nat . var A B : Nat . eq f(A B X:[Nat])=
B A f(X:[Nat]). eq f(nil)= nil . endm
The result is obtained by reducing the f function with the input list.
Example Session
Maude> red f(1 2 3 4 5 6) . --- Expected: 2 1 4 3 6 5
result NeList{Nat}: 2 1 4 3 6 5
Maude> red f(0 1 0 1) . --- Expected: 1 0 1 0
result NeList{Nat}: 1 0 1 0
Ungolfed
mod S is
pr LIST{Nat} .
op f : Nat ~> Nat .
var A B : Nat .
eq f(A B X:[Nat]) = B A f(X:[Nat]) .
eq f(nil) = nil .
endm
Just the straightforward recursive solution. Saved a couple bytes by inlining the type in X:[Nat] rather than declaring it with the var keyword. Doing the same with A and B doesn't change the byte count — just the way the math works out.
Python 3 + numpy, 31 bytes
lambda m:m[~-(m==m).cumsum()^1]
Expects a numpy array. Most of the code is about avoiding referencing numpy directly, so we can avoid the explicit import. It then calculates the indices directly by xoring a counter with 1.
Raku, 19 17 bytes
*.flatmap:{$^b,$^a}
*[^* »+»<1 -1>]
*[...]is an anonymous function that slices into the list given as the sole argument using the bracketed expression, which is another anonymous function.^* »+» <1 -1>takes the numbers from 0 up to one less than the argument, and produces a new list by alternatively adding 1 and -1. Those reordered indices are used to slice into the original input array.
CJam, 10 bytes
N{riripp}h
Explanation
N e# Push newline (this is truthy)
{ }h e# Do while without popping the condition: infinite loop
r e# Read line and push string to the stack
i e# Interpret as integer
r e# Read line and push string to the stack
i e# Interpret as integer
p e# Pop and print with a newline
p e# Pop and print with a newline
e# Implicitly print stack: newline
Python 3.8 (pre-release), 35 bytes
def f(m):*m[1:],_,m[::2]=*m,m[1::2]
In-place. Essentially a golfed version of m[1::2],m[::2]=m[::2],m[1::2]
Rockstar, 98 bytes
Listen to B
cut B with ","
I is 0
while I is less than B
shout B at I+1
shout B at I
Build I up up
Try it online!
(Must manually copy-paste code)
Takes input as comma-separated string of numbers eg:
1,2,3,4,5,6
Outputs as newline-separated string of numbers eg:
2
1
4
3
6
5
Rockstar has no modulo, and no bitwise operators, so porting the other solutions is out of the question.
Explanation
Listen to B ; Read input to B
cut B with "," ; Split B on ","
I is 0 ; Set I to 0
while I is less than B ; While I < B.length
; (Rockstar Arrays automatically coerce to length when used as a scalar)
shout B at I+1 ; Print B[I+1] with trailing newline
shout B at I ; Print B[I] with trailing newline
Build I up up ; Increment I twice (each 'up' increments)
Add++, 10 bytes
L,2$T€bRbF
Uses Add++'s much more powerful functional mode, rather than it's vanilla mode. However, definitely check out the other Add++ answer, as it doesn't use any form of indexing, just looping!
How it works
L,2$T€bRbF - Unnamed lambda function. Takes 1 argument, L
L, - Define the lambda:
2$T - Split into runs of length 2
€ - Over each:
bR - Reverse it
bF - Flatten and return
PowerShell, 31 bytes
%{If(++$i%2){$j=$_}Else{$_,$j}}
Input comes from the pipeline.
Try it in a PS console:
$array = 1,2,3,4,5,6
$result = $array |
%{If(++$i%2){$j=$_}Else{$_,$j}}
"$($array -join ',') --> $($result -join ',')"
Explanation
% is an Alias for the Cmdlet "ForEach-Object", which accepts input from the pipeline and processes each incoming object inside the ScriptBlock {...}
If(++$i%2) first increments $i (which starts at 0) in each loop and takes it modulo 2
If the result is 1:
{$j=$_} the current element will be saved in in $j
Otherwise (0):
{$_,$j} first puts the current element, then the element from the last loop iteration into the pipeline/stdout.
Pip -rp, 7 bytes
WVRVUWg
Explanation
The flags are for input & output formatting: -r reads the program arguments from lines of stdin; -p formats the output as a list rather than concatenating it.
g List of arguments
UW Unweave into a pair of lists, each consisting of every other element
RV Reverse the order of those two lists
WV Weave them back together
C (clang), 53 \$\cdots\$ 43 42 bytes
f(*l,n){*l^=l[1]^(l[1]=*l);n&&f(l+2,n-2);}
Inputs a pointer in an int array and the array's length (since pointers in C carry no length info).
Swaps every two elements in place.
Add++, 57 bytes
a:?
b:0
n:0
m:3
`b
Ea,Ib=n,,b:1,,x:i,+1,Ib=m,,b:0,,Oi,,Ox
Input is the first argument.
Charcoal, 5 bytes
WS⟦Sι
Try it online! Link is to verbose version of code. Takes newline-terminated input. Explanation:
WS
Repeat while there is still input...
⟦Sι
... output the subsequent input before it.
C# (.NET Core) with Linq, 32 26 bytes
a=>a.Select((n,i)=>a[i^1])
-8 bytes thanks to m90's witchcraft
C# (.NET Core), 73 bytes
a=>{for(int i=0,d=0;i<a.Length;i++){d=a[i];a[i]=a[++i];a[i]=d;}return a;}
Python 3, 34 bytes
f=lambda l:l and l[1::-1]+f(l[2:])
When l is empty (which makes it falsy), return it directly. Otherwise, reverse the first two elements and make a recursive call for the rest.
Pyth, 7 bytes
.n_McQ2
cQ2chunks of evaluated input in length of 2_Mmaps over reversal in the chunk matrix.nflattens it
Grok, 11 bytes
:Y}:zpz
q
Explanation:
:Y # Get input and copy it to the register
} # Quit if no more input
q
:z # Print next input
pz # Print value in the register
Vyxal, 4 bytes
2ẇRf
Explanation:
2ẇ # Split list into groups of two
R # Reverse each group
f # Flatten


