How to resolve the algorithm Perfect shuffle step by step in the Dyalect programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Perfect shuffle step by step in the Dyalect 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 Dyalect programming language
Source code in the dyalect programming language
func shuffle(arr) {
if arr.Length() % 2 != 0 {
throw @InvalidValue(arr.Length())
}
var half = arr.Length() / 2
var result = Array.Empty(arr.Length())
var (t, l, r) = (0, 0, half)
while l < half {
result[t] = arr[l]
result[t+1] = arr[r]
l += 1
r += 1
t += 2
}
result
}
func arrayEqual(xs, ys) {
if xs.Length() != ys.Length() {
return false
}
for i in xs.Indices() {
if xs[i] != ys[i] {
return false
}
}
return true
}
func shuffleThrough(original) {
var copy = original.Clone()
while true {
copy = shuffle(copy)
yield copy
if arrayEqual(original, copy) {
break
}
}
}
for input in yields { 8, 24, 52, 100, 1020, 1024, 10000} {
var numbers = [1..input]
print("\(input) cards: \(shuffleThrough(numbers).Length())")
}
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Haskell programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Arturo programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the PostScript programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the D programming language