How to resolve the algorithm Perfect shuffle step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Perfect shuffle step by step in the F# programming language

Table of Contents

Problem Statement

A perfect shuffle (or faro/weave shuffle) means splitting a deck of cards into equal halves, and perfectly interleaving them - so that you end up with the first card from the left half, followed by the first card from the right half, and so on:

When you repeatedly perform perfect shuffles on an even-sized deck of unique cards, it will at some point arrive back at its original order. How many shuffles this takes, depends solely on the number of cards in the deck - for example for a deck of eight cards it takes three shuffles:

The TaskTest Cases

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perfect shuffle step by step in the F# programming language

Source code in the fsharp programming language

let perfectShuffle xs =
  let h = (List.length xs) / 2
  xs
  |> List.mapi (fun i x->(if i<h then i * 2 else ((i-h) * 2) + 1), x)
  |> List.sortBy fst
  |> List.map snd

let orderCount n =
  let xs = [1..n]
  let rec spin count ys =
    if xs=ys then count
    else ys |> perfectShuffle |> spin (count + 1)
  xs |> perfectShuffle |> spin 1

[ 8; 24; 52; 100; 1020; 1024; 10000 ] |> List.iter (fun n->n |> orderCount |> printfn "%d %d" n)


  

You may also check:How to resolve the algorithm Temperature conversion step by step in the LIL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Empty program step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Sokoban step by step in the 11l programming language
You may also check:How to resolve the algorithm Brace expansion step by step in the Go programming language