g | x | w | all
Bytes Lang Time Link
081JavaScript SpiderMonkey250124T182706ZArnauld
119Desmos250204T041738ZDesmosEn
013J250126T060056ZJonah
009Uiua 0.15.0dev.1250124T171426ZTbw
042APL+WIN250124T173139ZGraham
00805AB1E250127T082701ZKevin Cr
078Maple250125T211203Zdharr
010Japt250124T205238ZShaggy
028Haskell + hgl250124T171047ZWheat Wi
027Charcoal250125T003302ZNeil
080Perl 5 nl250124T223656ZXcali
052Python 3250124T182049ZJonathan
006Jelly250124T165855ZJonathan

JavaScript (SpiderMonkey), 81 bytes

Expects (piles)(cards) and prints the cards one by one, 0-indexed.

p=>n=>{for(k=p-1&~1;~k<p;k-=2)for(j=i=k<0?~k:k;j<n-n%p+p*(i<n%p)+i;j+=p)print(j)}

Try it online!


JavaScript (ES7), 65 bytes

A port of Jonathan Allan's Python answer.

Expects (piles)(cards) and returns an array of 0-indexed cards.

p=>n=>[...Array(n).keys(g=v=>v%p%2-1^v%p)].sort((a,b)=>g(a)-g(b))

Try it online!

Desmos, 119 bytes


d(n)=n+[0...\floor((c-n)/p)]p
R(n,a,r)=\{n>p:a,r=1:R(n+1,\join(a,d(n)),-1),R(n+1,\join(d(n),a),1)\}
f(p,c)=R(2,d(1),1)

This solution works by having a function d(n) find the contents of deck n, then calls a recursive function R(n,a,r) to append d(n) to and accumulator array. I used recursion since it isn't possible to push all d(n) for n=[1...p] into the array at once.

To use it, just call f(Numpiles, Numcards).

Try it on Desmos!

Try it on Desmos! - Prettified

J, 23 15 13 bytes

(]/:_2-@^|)i.

Attempt This Online!

-2 thanks to Tbw, in a snake eating its tail chain of improvements

This approach avoids having to group and instead uses a single sort on a flat array. The logic only has to worry about finding the right sorting vector.

Consider 7 f 14 -- 14 cards, 7 piles.

Uiua 0.15.0-dev.1, 30, 21, 16, 11 9 bytes SBCS

⍏¯ⁿ⊙¯2◿⊙⇡

Try on Uiua Pad!

-5 bytes thanks to @noodle person's port of @Jonah's answer.

Takes number of piles and deck size and returns a 0-indexed array.

Explanation

⍏¯ⁿ⊙¯2◿⊙⇡ 
       ◿⊙⇡ # range(deck) mod piles
  ¯ⁿ⊙¯2    # map x -> -(-2)^x
⍏          # indices to sort array

Since the other solutions had significantly different strategies, I include them here as well.

Old solutions

⍏×ⁿ+₁⤙¯1◿⊙⇡ (thanks @noodle person and @Jonah)
⍏↯¤:⍏⊂⇌∩▽¬,,◿2.⇡
≡⊡1⊚⊞=⊂⇌∩▽¬,,◿2.⇡:◿,⇡
⊡◴⊚.⬚0⊏♭⍜⊢⇌⍉⬚∞↯∞_2⇡:⍉⬚0↯⊂∞,+1⇡

APL+WIN, 58 42 bytes

Prompts for number of piles followed by number of cards.

(v≤c)/v←∊(⊂¨⍋(i⍴¯1 1)×⍳i)+⊂0,i×⍳⌈(c←⎕)÷i←⎕

Try it online! Thanks to Dyalog Classic

05AB1E, 8 bytes

LIιιćRì˜

Inputs in the order \$cards,piles\$.

Try it online or verify all test cases.

Explanation:

L         # Push a list in the range [1, (implicit) first input]
 Iι       # Uninterleave it into the second input amount of parts
   ι      # Uninterleave that into two parts
    ć     # Extract the first part
     R    # Reverse this first list of lists
      ì   # Prepend it back to the second part
       ˜  # Flatten the list of lists
          # (after which the result is output implicitly)

Maple, 78 bytes

(p,n)->op~(sort([ListTools:-Deal([$n],p)],(a,b)->`if`(b[1]::odd,false,true)));

Ungolfed:

proc(p,n)
[ListTools:-Deal([$n],p)];          # deal out [1..n] into p piles (sublists)
op~(sort(%,(a,b)->`if`(b[1]::odd,false,true))); # if first in the pile is odd,
                                            # bring the pile to the beginning
                                            # op~ flattens to a single list
end:

This relies on Maple's sort only changing the order if the comparison function returns false; then the second argument moves earlier. So setting false if the second argument is odd means that [1,2,3,4,5,6,7] is sorted to [7,5,3,2,1,2,4,6], precisely the order we want for the piles. So this sort order is applied to the list of piles based on the first element in each pile.

Japt, 10 bytes

õ óV ó vÔc

Try it

õ óV ó vÔc     :Implicit input of integers U=cards & V=piles
õ              :Range [1,U]
  óV           :Partitions of every Vth element
     ó         :Uninterleave
       v       :Modify first element/sub-array
        Ô      :  Reverse
         c     :Flatten 

Haskell + hgl, 28 bytes

(cx<(mp<rv)<%uak<tx)<<fxS<e1

Attempt This Online!

Takes input in reverse order from the test cases.

Explanation

Reflection

Charcoal, 27 bytes

NθNηIΣE⁺⮌Φθ¬﹪ι²Φθ﹪ι²✂…⁰ηιηθ

Attempt This Online! Link is to verbose version of code. 0-indexed. Explanation:

Nθ                          Input number of piles
  Nη                        Input number of cards
          θ                 Number of piles
         Φ                  Filter on implicit range
             ι              Current value
            ﹪               Modulo
              ²             Literal integer `2`
           ¬                Is zero
        ⮌                   Reversed
       ⁺                    Concatenated with
                θ           Number of piles
               Φ            Filter on implicit range
                  ι         Current value
                 ﹪          Modulo
                   ²        Literal integer `2`
      E                     Map over values
                     …      Range from
                      ⁰     Literal integer `0` to
                       η    Number of cards
                    ✂       Sliced from
                        ι   Current value to
                         η  Number of cards
                          θ In steps of number of piles
     Σ                      Concatenate the lists
    I                       Cast to string
                            Implicitly print

Perl 5 -nl, 80 bytes

for$b(sort{$a%2-$b%2||($a%2?$a-$b:$b-$a)}0..($u=<>)-1){map$_%$u-$b||say,0..$_-1}

Try it online!

Python 3,  57 56  52 bytes

-4 from me & xnor (although it looks like he pipped me to the post by about six minutes).

lambda p,d:sorted(range(d),key=lambda v:v%p%2-1^v%p)

An unnamed function that accepts the pile count, p, and the deck size, d, and returns a list of the shuffled, 0-indexed cards.

Try it online! (Increments all cards to align with the examples given in the question.)

How?

Sorts the indexes of the original deck (range(d) being the range \$[0,d-1]\$) by whether firstly the 0-indexed pile index, v, is even and secondly by the pile index. The pile index is v%p so we get -1-(v%p) when v%p%2 is zero (even pile index) or v%p when v%p%2 is one (odd pile index). We could use just -(v%p) but shifting down one to -1-(v%p) is golfier using an XOR (^) with v%p%2-1^v%p - that is, with parentheses to clarify precedence (((v%p)%2)-1)^(v%p).


47 bytes if we may accept a list of the card indexes (i.e. the integers \$[0,d-1]\$) in an undetermined order (i.e. shuffled), by rearranging them to the (one and only) desired order.

lambda p,d:d.sort(key=lambda v:(v%p%2-1^v%p,v))

Try it online!

43 bytes if we can take the same input, but ordered. (Seems cheaty to me.)

lambda p,d:d.sort(key=lambda v:v%p%2-1^v%p)

Try it online!

Jelly,  7  6 bytes

-1 by Unrelated String (use a grade-up of the card's pile indexes.)

NḂ¡€ṁỤ

A dyadic Link that accepts the number of piles on the left and the deck size on the right and yields the shuffled deck.

Try it online!

How?

NḂ¡€ṁỤ - Link: integer, PileCount, integer, DeckSize
   €   - for each {PileIndex in [1..PileCount]}:
  ¡    -     repeat...
 Ḃ     -     ...times: PileIndex mod 2 -> One if odd else Zero
N      -     ...action: negate
    ṁ  - mould like {[1..DeckSize]}
     Ụ - grade {that} up (the indices of {that} sorted by their respective values)