Riffle shuffle kerfuffle

Riffle shuffle kerfuffle

Tags: Algorithms Simulation Sequences

Credit: Ilkka Kokkarinen

Difficulty: Hard

Type: Text

Category: Practice

Time Allowed: 15 s

Description:

Given a list of items whose length is guaranteed to be even (note that “oddly” enough, zero is an even number), create and return a list produced by performing a perfect riffle to the items by interleaving the items of the two halves of the list in an alternating fashion.

When performing a perfect riffle shuffle, also known as the Faro shuffle, the list of items is split in two equal sized halves, either conceptually or in actuality. The first two elements of the result are then the first elements of those halves. The next two elements of the result are the second elements of those halves, followed by the third elements of those halves, and so on up to the last elements of those halves. The parameter out determines whether this function performs an out shuffle or an in shuffle that determines which half of the deck the alternating card is first taken from.

Test Case Input Expected Output
1 [], True []
2 [], False []
3 [0, 1], True [0, 1]
4 [0, 1], False [1, 0]
5 [-4, 0, 1, -1], True [-4, 1, 0, -1]

Please register or login to submit solutions to challenges.