# better algorithm to riffle shuffle (or interleave) multiple lists of varying lengths

• Last Update :
• Techknowledgy :

Select the biggest and arrange a matrix with the number of columns corresponding to the number of episodes of this one, filled in the following way:

```A A A A A A < -First show consist of 6 episodes
B B B B C C < -Second and third show - 4 episodes each
C C D D < -Third show 2 episodes```

Then collect by columns

```{
A,
B,
C
}, {
A,
B,
C
}, {
A,
B,
D
}, {
A,
B,
D
}, {
A,
C
}, {
A,
C
}```

Then Join

```{
A,
B,
C,
A,
B,
C,
A,
B,
D,
A,
B,
D,
A,
C,
A,
C
}```

```[
['A'] * 2, ['L'] * 3, ['X'] * 5
])

X X X X X
L L L A A

-
> {
X1,
L1,
X2,
L2,
X3,
L3,
X4,
A1,
X5,
A2
}```

As no Python here, perhaps a Mathematica code may be of some use:

```l = {
,
,
,
};
( * Prepare input * )
l[[1]] = {
a,
a,
a,
a,
a,
a
};
l[[2]] = {
b,
b,
b,
b
};
l[[3]] = {
c,
c,
c,
c
};
l[[4]] = {
d,
d
};
le = Length @First @l;

k = DeleteCases[( * Make the matrix * ) Flatten @Transpose @Partition[Flatten[l], le, le, 1, {
Null
}], Null];

Table[r[i] = 1, {
i,
k
}];
( * init counters * )
ReplaceAll[#, x_: > x[r[x]++]] & /@ k         (*assign numbers*)

-
> {
a[1],
b[1],
c[1],
a[2],
b[2],
c[2],
a[3],
b[3],
d[1],
a[4],
b[4],
d[2],
a[5],
c[3],
a[6],
c[4]
}```

My try:

```program, play = [
['ABCe1.mp4', 'ABCe2.mp4'],
['XYZe1.mp4', 'XYZe2.mp4', 'XYZe3.mp4', 'XYZe4.mp4',
'XYZe5.mp4', 'XYZe6.mp4', 'XYZe7.mp4'
],
['OTHERe1.mp4', 'OTHERe2.mp4']
], []
start_part = 3
while any(program):
m = max(program, key = len)
if (len(play) > 1 and play[-1][: start_part] != m[0][: start_part] and play[-2].startswith(play[-1][: start_part])):
play.insert(-1, m.pop(0))
else:
play.append(m.pop(0))

print play```

The one you ask probably could return a few (1, 2) results limited by your requests.

```from random
import choice, randint
from operator

def randpop(playlists):
pl = choice(playlists)
el = pl.pop(randint(0, len(pl) - 1))
return pl, el

def shuffle(playlists):
curr_pl = None
while any(playlists):
try:
curr_pl, el = randpop([pl
for pl in playlists
if pl and pl != curr_pl
])
except IndexError:
break
else:
yield el
yield el
raise StopIteration

if __name__ == "__main__":
sample = [
'A1 A2 A3 A4'.split(),
'B1 B2 B3 B4 B5'.split(),
'X1 X2 X3 X4 X5 X6'.split()
]
for el in shuffle(sample):
print(el)```

Given episodes order is mandatory just simplify `randpop` function:

```def randpop(playlists):
pl = choice(playlists)
el = pl.pop(0)
return pl, el```

Suggestion : 2

Last Updated : 08 Jul, 2022

```Original list 1: [1, 4, 5]
Original list 2: [3, 8, 9]
The interleaved list is: [1, 3, 4, 8, 5, 9]```

Suggestion : 3

Implement the (seemingly) more common techniques of the riffle shuffle and overhand shuffle for   n   iterations. , Implementing playing cards is not necessary if it would be easier to implement these shuffling methods for generic collections. , There are many techniques that people use to shuffle cards for card games. Some are more effective than others. ,Here are some examples of the underlying selection mechanism in action for a deck of 10 cards:

If we generate a deck by

`deck←⊂[1](52⍴ 'A23456789TJQK'), [0.5](13⍴ 'S'), (13⍴ 'H'), (13⍴ 'D'), (13⍴ 'C')`

Then a generated deck looks like

`AS 2 S 3 S 4 S 5 S 6 S 7 S 8 S 9 S TS JS QS KS AH 2 H 3 H 4 H 5 H 6 H 7 H 8 H 9 H TH JH QH KH AD 2 D 3 D 4 D 5 D 6 D 7 D 8 D 9 D TD JD QD KD AC 2 C 3 C 4 C 5 C 6 C 7 C 8 C 9 C TC JC QC KC`

Sorting a deck merely requires generating 52 unique random nunmbers from 1 to 52.

`deck[52 ? 52] JD 8 C TH 8 D KH QH 6 S AH 4 D JS 5 S AD 6 H 3 H 3 D 5 C 9 C 7 C 7 S 4 C JC 3 S KD 9 H 3 C 4 H 2 D TD KS TS 7 D JH 9 D 8 H 6 D 7 H 2 H 4 S QC AC KC 9 S AS QS TC 2 C 8 S 5 D 2 S 6 C 5 H QD`
5._
```[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[9, 2, 8, 3, 20, 15, 1, 13, 7, 18, 5, 16, 4, 19, 10, 6, 12, 14, 11, 17]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[12, 1, 2, 3, 4, 13, 14, 5, 15, 16, 6, 7, 17, 8, 18, 19, 9, 10, 20, 11]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[13, 18, 12, 17, 10, 9, 19, 11, 16, 15, 6, 8, 14, 1, 3, 2, 5, 4, 7, 20]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 17, 18, 19, 15, 16, 11, 12, 13, 14, 10, 7, 8, 9, 5, 6, 2, 3, 4, 1]```
6._
```RIFFLE
18 9 17 20 3 4 16 8 7 10 5 14 12 1 13 19 2 11 15 6

OVERHAND
2 13 12 11 10 9 18 17 6 5 4 3 7 20 19 15 8 14 16 1

STD SHUFFLE
14 4 17 3 12 5 19 6 20 2 16 11 8 15 7 13 10 18 9 1```