Challenge
To faro shuffle a deck of playing cards is to split the deck exactly in half, then perfectly interweave the cards, such that the original top and bottom cards are unchanged.
Write a function that accepts a even-numbered list and faro shuffles the indices.
Example
Faro shuffling the list: ['ace', 'two', 'three', 'four', 'five', 'six']
will give ['ace', 'four', 'two', 'five', 'three', 'six']
Good luck, happy coding!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (19)
Haskell:
i feel in my bones there's wholly point-free way to do it but can't quite get there right now
I managed to get it point free!
I remember looking for the
splitAt
function when I was writing my first answer, not sure how I missed it in hoogle.Also, I've never seen
Control.Arrow
before (I'm pretty new to Haskell). Seems useful.Bit of practice with
flatMap
; I hadn't used it before - but I'm going to start now!EDIT:
So I had the bright idea to do a screencast of me solving the problem and posting it on youtube... any feedback is welcome! And if it goes well, maybe I'll do more of them in the future :) We'll see! I haven't done youtube before, so this is my first video on there!
youtu.be/srT3yqFsgCQ
Nicely done on the video! Very clear, and nicely put together. :-)
Thanks!
A bit of golf:
It works like this:
.map((_,i)
: "i" will be the index of the current element, we don't need to work with the value herei/2 + true * (a.length / 2 + 0.5)
. Thanks to coercion, "true" will be translated to1
, hence we will fetch the(a.length / 2 + 0.5)+i/2
th indexi/2 + false * (a.length / 2 + 0.5)
, translated toi/2 + 0 * (a.length / 2 + 0.5)
(hence,i/2
)The return will then be the following:
Taking into account that the input is an array, you could save some bytes by replacing
[...a]
with justa
(for golf purposes).Apart from that, the solution seems really specific to the problem, and doesn't work for different arrays. For example:
Thanks a lot for the input! The problem was in the
2.5
usage, which had to be replaced witha.length/2-.5
. I updated my answer!You can shorten the index computation to:
Hmm I should try that IRL, how much does a deck of cards Go for nowadays?
shuffle.go
shuffle_test.go
Basically using a recusive function picking one element from each half everytime until both are empty.
Python (golfing, and assuming we want to mutate the input list)
The idea is to use as index
We can however factor
//2
so we getThen we can use right shift
>>
to get rid of parenthesis to the finalA solution just returning the shuffled array instead could be:
Perl solution. Tests stolen from Donald Feury.
I wanted to do it simple -
String
s andflat_map()
s, but here's a bit more involved solution, with generics, trait bounds, and an awesome case for the itertools crate :)Oh, it's been a while since I've had bandwidth to participate. Today's was nice & relaxing, though. Just what I need before a flight.
Here's mine:
Full version w/ tests at gist.github.com/kerrishotts/bde389...
python