| Bytes | Lang | Time | Link |
|---|---|---|---|
| 537 | Bespoke | 250118T190502Z | Josiah W |
| 015 | K ngn/k | 250114T111033Z | oeuf |
| 021 | APLNARS | 250109T112403Z | Rosario |
| 009 | Uiua | 250106T232801Z | noodle p |
| 004 | Vyxal | 240724T144747Z | lyxal |
| 098 | PHP | 190218T150104Z | Titus |
| 052 | Perl 5 lan | 190215T222753Z | Xcali |
| 116 | Prolog | 160814T084028Z | Leaky Nu |
| 042 | Julia | 160605T065226Z | Dennis |
| 109 | java | 160607T031212Z | Jack Amm |
| 2321 | APL | 160605T131700Z | user2070 |
| 007 | Pyth | 160605T042410Z | Maltysen |
| 044 | Mathematica | 160605T162947Z | DavidC |
| 015 | Actually | 160605T230546Z | user4594 |
| 017 | J | 160605T045124Z | Leaky Nu |
| 062 | Haskell | 160605T095835Z | lynn |
| 054 | vim | 160605T061104Z | Doorknob |
| 012 | J | 160605T163652Z | algorith |
| 011 | MATL | 160605T044134Z | Luis Men |
| 051 | JavaScript ES6 | 160605T094915Z | Neil |
| 007 | Pyke | 160605T104204Z | Blue |
| 059 | Python 2 | 160605T050538Z | Sp3000 |
| 005 | 05AB1E | 160605T054213Z | Adnan |
| 057 | Python | 160605T043624Z | Dennis |
| 007 | Jelly | 160605T042554Z | Leaky Nu |
Bespoke, 537 bytes
PUSH I H V
STACKTOP MINUSONE
CONTROL DOWHILE
INPUT N
DO COPY
PUSH BI H SV
DO SWITCH
STACKTOP PLUSONE
PUSH I H V
INPUT CH
STACKTOP LT
CONTROL END
PUSH TRI H SV
DO P
PUSH BI H V
CONTROL WHILE
PUSH TRI H V
DO COPY
PUSH I H SV
PUSH BI STACKTOP QUOTIENTOF
DO TURNOVERN
CONTROL DOWHILE
PUSH I H V
DO SWITCHN
PUSH I H V
DO ROT
PUSH BI
PUSH I H V
PUSH BI STACKTOP MINUS
DO COPY
PUSH I H SV
STACKTOP LT
CONTROL END
PUSH BI H V
STACKTOP MINUSONE
DO COPY
PUSH BI H SV
CONTROL END
DO TURNOVER
CONTROL DOWHILE
OUTPUT N
PUT XX:TRI BI;OUTPUT CH
DO COPY
I didn't have time to write a "poetic" version of this one, but on the bright side, this gave me a chance to use the heap for storage in a code golf program!
K (ngn/k), 16 15 bytes
{x(,/+2 0N#)/y}
Explanation:
{x(,/+2 0N#)/y} Main function. x and y is input
x( )/y Do the following x times to y...
2 0N# Reshape: Split y into half
+ Flip (Transpose)
,/ Flatten (Concat + Reduce)
APL(NARS), 21 chars
{∊k↑⍵,¨⍵⌽⍨k←2÷⍨≢⍵}⍣⎕⎕
I have copy/seen the use of ⎕ from other solution, test:
⎕:
0
⎕:
1 2 3 4
┌4───────┐
│ 1 2 3 4│
└~───────┘
{∊k↑⍵,¨⍵⌽⍨k←2÷⍨≢⍵}⍣⎕⎕
⎕:
11
⎕:
10, 11, 8, 15, 13, 13, 19, 3, 7, 3, 15, 19
┌12──────────────────────────────┐
│ 10 19 11 3 8 7 15 3 13 15 13 19│
└~───────────────────────────────┘
Vyxal, 4 bytes
(I÷Y
My 1000th code golf answer 🥳. My how the time and answers fly.
Explained
(I÷Y
( # n times:
I # Split the top of the stack into two equal length lists
÷ # Dump those lists onto the stack
Y # And interleave
💎
Created with the help of Luminespire.
PHP, 98 bytes
function($a,$n){while($n--)for($z=count($a)/2;$z;)array_splice($a,$z--,0,array_pop($a));return$a;}
Prolog, 116 bytes
a([],[[],[]]).
a([H,I|T],[[H|U],[I|V]]):-a(T,[U,V]).
f(X,0,X).
f(X,N,Y):-N>0,M is N-1,f(X,M,Z),a(Z,[A,B]),append(A,B,Y).
Usage
?- f([1,2,3,4,5,6,7,8],2,X).
X = [1, 5, 2, 6, 3, 7, 4, 8] ;
false.
Julia, 45 42 bytes
a\n=n>0?reshape(a,endof(a)÷2,2)'[:]\~-n:a
How it works
We (re)define the binary operator \ for this task. Let a be an array and n a non-negative integer.
If n is positive, we shuffle the array. This is achieved by reshaping it into a matrix of length(a) ÷ 2 rows and two columns. ' transposes the resulting matrix, creating of two rows, then flattening the result with [:]. Since Julia stores matrices in column-major order, this interleaves the two rows.
Afterwards, we call \ recursively with the shuffled a and n - 1 (~-n) as arguments, thus performing additional shuffles. Once n reaches 0, we return the current value of a.
java, 109 bytes
int[]f(int[]a,int n){for(int x,q=a.length,d[];0<n--;a=d){d=new int[q];for(x=0;x<q;x++)d[(2*x+2*x/q)%q]=a[x];}return a;}
Explanation: There is a pattern to how the elements move when they are faro shuffled:
let x be the original index
let y be the new index
let L be the length of the array
- y is double x
- if x is greater than or equal to half of L then increment y
- keep y within the array's bounds
or as code: y=(2*x+x/(L/2))%L
This assumes that indicies start at 0. Here's the code further explained:
int[] faroShuffle( int[] array, int numberOfShuffles ) {
//repeat the faro shuffle n times
for( int index, length=array.length, destination[]; 0<numberOfShuffles--; array=destination ) {
//new array to copy over the elements
destination=new int[length];
//copy the elements into the new array
for( index=0; index<length; index++ )
destination[(2*index+2*index/length)%length]=array[index];
//at the end of each loop, copy the reference to the new array and use it going forward
}
return array;
}
APL, 23 21 chars
({⊃,/⍵(↑,¨↓)⍨2÷⍨⍴⍵}⍣N)A
Without the assumption (Thanks to Dennis) and 1 char shorter:
({{∊,⌿2(2÷⍨≢⍵)⍴⍵}⍣⎕)⎕
Try it on online.
Mathematica 44 bytes
With 4 bytes saved thanks to @miles.
Riffle@@TakeDrop[#,Length@#/2]&~Nest~##&
Riffle @@ TakeDrop[#, Length@#/2] &~Nest~## &[list, nShuffles] splits the list into two equal sublists and shuffles (Riffles) them.
Riffle @@ TakeDrop[#, Length@#/2] &~Nest~## &[Range@8, 1]
{1, 5, 2, 6, 3, 7, 4, 8}
Riffle @@ TakeDrop[#, Length@#/2] &~Nest~## &[Range@100, 23]
{1, 30, 59, 88, 18, 47, 76, 6, 35, 64, 93, 23, 52, 81, 11, 40, 69, 98, 28, 57, 86, 16, 45, 74, 4, 33, 62, 91, 21, 50, 79, 9, 38, 67, 96, 26, 55, 84, 14, 43, 72, 2, 31, 60, 89, 19, 48, 77, 7, 36, 65, 94, 24, 53, 82, 12, 41, 70, 99, 29, 58, 87, 17, 46, 75, 5, 34, 63, 92, 22, 51, 80, 10, 39, 68, 97, 27, 56, 85, 15, 44, 73, 3, 32, 61, 90, 20, 49, 78, 8, 37, 66, 95, 25, 54, 83, 13, 42, 71, 100}
Actually, 15 bytes
`;l½≈@│t)HZ♂i`n
Explanation:
`;l½≈@│t)HZ♂i`n
` `n do the following n times:
;l½≈ push half the length of the array
@ swap
│ duplicate entire stack
t)H last L//2 elements, first L//2 elements
Z♂i zip, flatten each element
J, 22 19 17 bytes
3 bytes thanks to @Gareth.
2 bytes thanks to @algorithmshark.
-:@#({.,@,.}.)]^:
Usage
>> f =: -:@#({.,@,.}.)]^:
>> 2 f 1 2 3 4 5 6 7 8
<< 1 3 5 7 2 4 6 8
Where >> is STDIN and << is STDOUT.
Previous 22-byte version:
({~[:,/@|:@i.2,-:@#)^:
Usage
>> f =: ({~[:,/@|:@i.2,-:@#)^:
>> 2 f 1 2 3 4 5 6 7 8
<< 1 3 5 7 2 4 6 8
Where >> is STDIN and << is STDOUT.
Haskell, 62 bytes
0!a=a
n!a|s<-length a=(n-1)![a!!mod(div(s*i+i)2)s|i<-[0..s-1]]
Let s = 2·t be the size of the list. The i-th element of the new list is obtained by taking the
-th element of the old list, zero-indexed, modulo s.
Proof: if i = 2·k is even, then
and if i = 2·k + 1 is odd, then
Thus the values used for indexing are 0, t, 1, t + 1, 2, t + 2, …
vim, 62 59 54
qrma50%mb:norm@q<cr>ggqOjdd'apjma'b@q<esc>0"qDJ<C-a>D@"i@r<esc>xxdd@"
Wow. This is possibly the hackiest thing I've written for PPCG, and that's saying something.
Input is taken as N on the first line followed by the elements of the array, each on its own line.
qr first, we're going to record the contents of the @r macro. this is
the macro which does the faro-shuffle operation.
ma set the mark 'a at the beginning of the file
50% move to the 50% point of the file (i.e. halfway down)
mb set another mark here
:norm@q evaluate the recursive macro @q. we'll get to what that does later,
but the interesting part here is that it's :norm@q instead of @q.
this is because a recursive macro terminates at the end of the
file, which means when @q terminates, @r would also abort, which
would make calling it with a count impossible. running @q under
:norm prevents this.
gg move back to the top of the file for the next iteration
q end recording
O now we're inserting contents of the @q macro, the recursive part
we can't record it directly because it's destructive
j move to line directly below mark 'b (which was just set before @q)
dd delete this line and bring it...
'ap up after mark 'a (which starts on line 1, bringing the N/2th line
directly below line 1, aka line 2)
jma replace mark 'a one line below this so that the next time we call
'ap, the line from the second half is interleaved with the lines
from the first half
'b jump back to mark 'b (remember, 'b is the last line of the first
half of the file, originally reached via 50%)
@q call ourselves, causing the macro to run until hitting EOF
0"qD delete this into register "q
J delete the empty line that remains
<C-a> here's another interesting bit: we want to run @r N times. but 0@r
means "go to column 0, and then run @r once." so we have to
increment the input number...
D@" and then *that* many times...
i@r insert @r...
xx ... and finally, delete two characters, which is the extra @r from
the increment
dd delete the sequence of @rs into the "" register...
@" and run it!
I actually possibly found several vim bugs while writing this answer:
recording macros is not possible within other macros (when setting their text manually, not with
q) or within:*maps.:let @a='<C-v><cr>'<cr>i<C-r>aoutputs two newlines, not one, for whatever arcane reason.
I might investigate those further later.
Thanks to Dr Green Eggs and Ham DJ for 3 bytes!
J - 12 bytes
Adverb (!) taking number of shuffles on the left and the array to shuffle on the right.
/:#/:@$0,#^:
The J parser has rules for writing tacit adverbs, but they have very low precedence: if you want to use a train of verbs as a left argument, you can omit an otherwise necessary set of parentheses. So the above is actually short for (/:#/:@$0,#)^:, which takes the number of shuffles on the left as an adverb, and then becomes a monadic function taking the array to shuffle on the right.
That said, we shuffle as follows. # is the length of the array, so 0,# is a two element list: 0 followed by something nonzero. Then #/:@$ replicates that into a list as long as the input array, and takes its sort vector.
The sort vector of a list is the information for how to sort the list: the (0-based) invdex of the smallest element, followed by the index of the next-smallest, and so on. For example, the sort vector of 0 1 0 1 ... will thus be 0 2 4 ... 1 3 5 ....
If J were now to sort this sort vector, it would Faro-shuffle it; but that would be trivial, since we'd get 0 1 2 3 ... back. So we use dyadic /: to sort the input array as if it were 0 2 4 ... 1 3 5 ..., which Faro-shuffles it.
Example usage below. Try it yourself at tryj.tk!
1 (/:#/:@$0,#^:) 1 2 3 4 5 6 7 8
1 5 2 6 3 7 4 8
f =: /:#/:@$0,#^:
2 f 1 2 3 4 5 6 7 8
1 3 5 7 2 4 6 8
7 f _23 _37 52 0 _6 _7 _8 89 NB. "negative 1" is spelled _1
_23 _6 _37 _7 52 _8 0 89
1 f 0 0 0 0 1 1 1 NB. odd-length lists
0 1 0 1 0 1 0
MATL, 11 bytes
w:"tn2/e!1e
Thanks to @Dennis for a correction
Explanation
w % Take the two inputs N and A. Swap them
: % Generate [1 2 ... N]
" % Repeat N times
tn2/ % Duplicate A. Number of elements divided by 2
e % Reshape to that number of rows
! % Transpose
1e % Reshape to one row
% End (implicit)
% Display (implicit)
JavaScript (ES6), 61 51 bytes
(n,a)=>[...a].map((e,i)=>a[(i<<n)%~-a.length||i]=e)
Modifies the input array in place and returns a copy of the original array. If this is unacceptable, &&a can be suffixed to return the modified array. Only works for small values of n due to the limitations of JavaScript's integer arithmetic. 61 60 byte recursive version that works with larger n, based on @Lynn's formula:
f=(n,a,l=a.length)=>n?f(n-1,a.map((_,i)=>a[(i*-~l>>1)%l])):a
Pyke, 7 bytes
VDlec,s
V - Repeat N times:
D - a,b = a (2nd arg first time round)
le - b = len(b)//2
c - a = chunk(a,b)
, - a = zip(*a)
s - a = sum(a, [])
Python 2, 59 bytes
def f(n,L):exec"l=len(L)/2;L=(L+L[1:]*~-l)[::l];"*n;print L
A different approach, slightly longer than the other Python answers. Only works for positive even numbers of elements.
e.g. for 1, [1,2,3,4,5,6,7,8], take the array and append len(L)/2-1 copies of itself minus the first element, e.g.
[1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,2,3,4,5,6,7,8,2,3,4,5,6,7,8]
Then take every len(L)/2th element.
[1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,2,3,4,5,6,7,8,2,3,4,5,6,7,8]
^ ^ ^ ^ ^ ^ ^ ^
05AB1E, 5 bytes
Code:
F2äø˜
Explanation, input: N, array:
F # Do the following N times
2ä # Split the array into 2 pieces
ø # Zip
˜ # Deep flatten
Uses the CP-1252 encoding. Try it online!.
Python, 68 57 bytes
f=lambda n,x:n and f(n-1,sum(zip(x,x[len(x)/2:]),()))or x
Thanks to @Sp3000 for golfing off 11 bytes!
Test it on Ideone.
Jelly, 9 7 bytes
2 bytes thanks to Dennis!
œs2ZFð¡
Explanation
œs2ZFð¡ Main dyadic chain. Arguments: x,y
¡ Repeat the following y time:
œs2 Split into two.
Z Transpose.
F Flatten.
Previous 9-byte version:
œs2ZF
Ç⁴¡

