How to resolve the algorithm Perfect shuffle step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Perfect shuffle step by step in the FreeBASIC 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 FreeBASIC programming language
Source code in the freebasic programming language
function is_in_order( d() as uinteger ) as boolean
'tests if a deck is in order
for i as uinteger = lbound(d) to ubound(d)-1
if d(i) > d(i+1) then return false
next i
return true
end function
sub init_deck( d() as uinteger )
for i as uinteger = 1 to ubound(d)
d(i) = i
next i
return
end sub
sub shuffle( d() as uinteger )
'does a faro shuffle of the deck
dim as integer n = ubound(d), i
dim as integer b( 1 to n )
for i = 1 to n/2
b(2*i-1) = d(i)
b(2*i) = d(n/2+i)
next i
for i = 1 to n
d(i) = b(i)
next i
return
end sub
function shufs_needed( size as integer ) as uinteger
dim as uinteger d(1 to size), s = 0
init_deck(d())
do
shuffle(d())
s+=1
if is_in_order(d()) then exit do
loop
return s
end function
dim as uinteger tests(1 to 7) = {8, 24, 52, 100, 1020, 1024, 10000}, i
for i = 1 to 7
print tests(i);" cards require "; shufs_needed(tests(i)); " shuffles."
next i
You may also check:How to resolve the algorithm Function definition step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Kosaraju step by step in the Python programming language
You may also check:How to resolve the algorithm 100 doors step by step in the HicEst programming language
You may also check:How to resolve the algorithm Minimum multiple of m where digital sum equals m step by step in the jq programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the Phixmonti programming language