Mind boggling card trick: Difference between revisions

Undo revision 273670 by Wherrera (talk)
(Undo revision 273670 by Wherrera (talk))
Line 622:
There are 8 black cards in the black card pile:
there are 8 red cards in the red card pile.
</pre>
 
 
=={{header|Julia==
{{trans|Perl 6}}
<lang julia>
const rbdeck = split(repeat('R', 26) * repeat('B', 26), "")
shuffledeck() = shuffle(rbdeck)
 
function deal!(deck, dpile, bpile, rpile)
while !isempty(deck)
if (topcard = pop!(deck)) == "R"
push!(rpile, pop!(deck))
else
push!(bpile, pop!(deck))
end
push!(dpile, topcard)
end
end
 
function swap!(rpile, bpile, nswapping)
rpick = sort(randperm(length(rpile))[1:nswapping])
bpick = sort(randperm(length(bpile))[1:nswapping])
rrm = rpile[rpick]; brm = bpile[bpick]
deleteat!(rpile, rpick); deleteat!(bpile, bpick)
append!(rpile, brm); append!(bpile, rrm)
end
 
function mindbogglingcardtrick(verbose=true)
prif(cond, txt) = (if(cond) println(txt) end)
deck = shuffledeck()
prif(verbose, "Shuffled deck is: $deck")
 
dpile, rpile, bpile = [], [], []
deal!(deck, dpile, bpile, rpile)
 
prif(verbose, "Before swap:")
prif(verbose, "Discard pile: $dpile")
prif(verbose, "Red card pile: $rpile")
prif(verbose, "Black card pile: $bpile")
# swap a the same random number of random
# cards between the red and black piles
amount = rand(1:min(length(rpile), length(bpile)))
prif(verbose, "Swapping a random number of cards: $amount will be swapped.")
swap!(rpile, bpile, amount)
prif(verbose, "Red pile after swaps: $rpile")
prif(verbose, "Black pile after swaps: $bpile")
println("There are $(sum(map(x->x=="B", bpile))) black cards in the black card pile:")
println("there are $(sum(map(x->x=="R", rpile))) red cards in the red card pile.")
prif(verbose, "")
end
 
mindbogglingcardtrick()
 
for _ in 1:10
mindbogglingcardtrick(false)
end
</lang> {{output}} <pre>
Shuffled deck is: SubString{String}["R", "R", "B", "R", "R", "B", "B", "B", "B", "R", "R", "B", "R", "B", "R", "R", "R", "R", "R", "B",
"B", "B", "R", "R", "B", "B", "B", "B", "B", "R", "R", "B", "B", "B", "B", "B", "R", "B", "B", "B", "R", "R", "B", "R", "R", "B", "R",
"R", "B", "R", "R", "R"]
Before swap:
Discard pile: Any["R", "R", "R", "B", "R", "R", "B", "B", "B", "B", "B", "R", "B", "B", "R", "B", "B", "R", "R", "B", "B", "R", "B",
"B", "R", "R"]
Red card pile: Any["R", "B", "R", "B", "R", "B", "R", "R", "R", "B", "B", "R"]
Black card pile: Any["R", "B", "R", "B", "B", "R", "B", "B", "B", "R", "R", "R", "B", "R"]
Swapping a random number of cards: 4 will be swapped.
Red pile after swaps: Any["B", "B", "R", "B", "R", "R", "B", "R", "R", "B", "R", "R"]
Black pile after swaps: Any["B", "R", "B", "R", "B", "B", "B", "R", "R", "B", "R", "R", "R", "B"]
There are 7 black cards in the black card pile:
there are 7 red cards in the red card pile.
There are 6 black cards in the black card pile:
there are 6 red cards in the red card pile.
There are 6 black cards in the black card pile:
there are 6 red cards in the red card pile.
There are 6 black cards in the black card pile:
there are 6 red cards in the red card pile.
There are 8 black cards in the black card pile:
there are 8 red cards in the red card pile.
There are 4 black cards in the black card pile:
there are 4 red cards in the red card pile.
There are 6 black cards in the black card pile:
there are 6 red cards in the red card pile.
There are 5 black cards in the black card pile:
there are 5 red cards in the red card pile.
There are 5 black cards in the black card pile:
there are 5 red cards in the red card pile.
There are 6 black cards in the black card pile:
there are 6 red cards in the red card pile.
There are 5 black cards in the black card pile:
there are 5 red cards in the red card pile.
</pre>
 
4,105

edits