Perfect shuffle: Difference between revisions
Content added Content deleted
ReeceGoding (talk | contribs) (→{{header|R}}: Gave new solution. Cleaned up formatting of older one.) |
|||
Line 1,660: | Line 1,660: | ||
=={{header|R}}== |
=={{header|R}}== |
||
===Matrix solution=== |
|||
<lang R> |
<lang R>wave.shuffle <- function(n) { |
||
wave.shuffle <- function(n) { |
|||
deck <- 1:n ## create the original deck |
deck <- 1:n ## create the original deck |
||
new.deck <- c(matrix(data = deck, ncol = 2, byrow = TRUE)) ## shuffle the deck once |
new.deck <- c(matrix(data = deck, ncol = 2, byrow = TRUE)) ## shuffle the deck once |
||
Line 1,677: | Line 1,676: | ||
test <- sapply(test.values, wave.shuffle) ## apply the wave.shuffle function on each element |
test <- sapply(test.values, wave.shuffle) ## apply the wave.shuffle function on each element |
||
names(test) <- test.values ## name the result |
names(test) <- test.values ## name the result |
||
test ## print the result out |
test ## print the result out</lang> |
||
{{out}} |
|||
> test |
<pre>> test |
||
8 24 52 100 1020 1024 10000 |
8 24 52 100 1020 1024 10000 |
||
3 11 8 30 1018 10 300 |
3 11 8 30 1018 10 300</pre> |
||
===Sequence solution=== |
|||
</lang> |
|||
The previous solution exploits R's matrix construction; This solution exploits its array indexing. |
|||
<lang R>#A strict reading of the task description says that we need a function that does exactly one shuffle. |
|||
pShuffle<-function(deck) |
|||
{ |
|||
n<-length(deck)#Assumed even (as in task description). |
|||
new<-array(n)#Maybe not as general as it could be, but the task said to use whatever was convenient. |
|||
new[seq(from=1,to=n,by=2)]<-deck[seq(from=1,to=n/2,by=1)] |
|||
new[seq(from=2,to=n,by=2)]<-deck[seq(from=1+n/2,to=n,by=1)] |
|||
new |
|||
} |
|||
task2<-function(deck) |
|||
{ |
|||
new<-deck |
|||
count<-0 |
|||
repeat |
|||
{ |
|||
new<-pShuffle(new) |
|||
count<-count+1 |
|||
if(all(new==deck)){break} |
|||
} |
|||
cat("It takes",count,"shuffles of a deck of size",length(deck),"to return to the original deck.","\n") |
|||
invisible(count)#For the unit tests. The task wanted this printed so we only return it invisibly. |
|||
} |
|||
#Tests - All done in one line. |
|||
mapply(function(x,y) task2(1:x)==y,c(8,24,52,100,1020,1024,10000),c(3,11,8,30,1018,10,300))</lang> |
|||
{{out}} |
|||
<pre>> mapply(function(x,y) task2(1:x)==y,c(8,24,52,100,1020,1024,10000),c(3,11,8,30,1018,10,300)) |
|||
It takes 3 shuffles of a deck of size 8 to return to the original deck. |
|||
It takes 11 shuffles of a deck of size 24 to return to the original deck. |
|||
It takes 8 shuffles of a deck of size 52 to return to the original deck. |
|||
It takes 30 shuffles of a deck of size 100 to return to the original deck. |
|||
It takes 1018 shuffles of a deck of size 1020 to return to the original deck. |
|||
It takes 10 shuffles of a deck of size 1024 to return to the original deck. |
|||
It takes 300 shuffles of a deck of size 10000 to return to the original deck. |
|||
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |