Jump to content

Mind boggling card trick: Difference between revisions

no edit summary
(Promoted to full task status.)
No edit summary
Line 845:
Swapping 2
Yeha! The mathematicians assertion is correct.</pre>
 
=={{header|R}}==
<lang R>
magictrick<-function(){
deck=c(rep("B",26),rep("R",26))
deck=sample(deck,52)
blackpile=character(0)
redpile=character(0)
discardpile=character(0)
while(length(deck)>0){
if(deck[1]=="B"){
blackpile=c(blackpile,deck[2])
deck=deck[-2]
}else{
redpile=c(redpile,deck[2])
deck=deck[-2]
}
discardpile=c(discardpile,deck[1])
deck=deck[-1]
}
cat("After the deal the state of the piles is:","\n","Black pile:",blackpile,"\n","Red pile:",redpile,"\n","Discard pile:",discardpile,"\n","\n")
X=sample(1:min(length(redpile),length(blackpile)),1)
if(X==1){s=" is"}else{s="s are"}
cat(X," card",s," being swapped.","\n","\n",sep="")
redindex=sample(1:length(redpile),X)
blackindex=sample(1:length(blackpile),X)
redbunch=redpile[redindex]
redpile=redpile[-redindex]
blackbunch=blackpile[blackindex]
blackpile=blackpile[-blackindex]
redpile=c(redpile,blackbunch)
blackpile=c(blackpile,redbunch)
cat("After the swap the state of the piles is:","\n","Black pile:",blackpile,"\n","Red pile:",redpile,"\n","\n")
cat("There are ", length(which(blackpile=="B")), " black cards in the black pile.","\n",
"There are ", length(which(redpile=="R")), " red cards in the red pile.","\n",sep="")
if(length(which(blackpile=="B"))==length(which(redpile=="R"))){
cat("The assertion is true!")
}
}
</lang>
{{Out}}
<pre>
After the deal the state of the piles is:
Black pile: B B R R B R B R R B B
Red pile: R B B B B B R B R R R B R B B
Discard pile: R R R R B R R R B R R B R B R R B R B R B B B R B B
10 cards are being swapped.
 
After the swap the state of the piles is:
Black pile: R B B B B R R R R B R
Red pile: B B R B B B R B B B B R B R R
There are 5 black cards in the black pile.
There are 5 red cards in the red pile.
The assertion is true!
</pre>
 
=={{header|REXX}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.