zvoulgaris

Perfect Shuffle drill

Mar 13th, 2020
380
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Perfect Shuffle
  2.  
  3. function ps(x::Array{<:Any, 1}) # perform a single perfect Shuffle
  4.     n = length(x) # this should be even
  5.     n2 = div(n, 2)
  6.     z = Array{eltype(x)}(undef, n)
  7.  
  8.     for i = 1:n2
  9.         j = 2*i
  10.         z[j-1] = x[i]
  11.         z[j] = x[n2+i]
  12.     end
  13.  
  14.     return z
  15. end
  16.  
  17. function main(x::Array{<:Any, 1}) # perform a series of ps until the original order reappears
  18.     n = 1
  19.     z = ps(x)
  20.  
  21.     while z != x
  22.         z = ps(z)
  23.         n += 1
  24.     end
  25.  
  26.     return n
  27. end
RAW Paste Data