Mind boggling card trick: Difference between revisions

Added Go
m (added the --category-- of ---Puzzles--- to this draft task, closest thing to a card trick.)
(Added Go)
Line 132:
R in red: 5
B in black: 5
</pre>
 
=={{header|Go}}==
<lang go>package main
 
import (
"fmt"
"math/rand"
"time"
)
 
func main() {
// Create pack, half red, half black and shuffle it.
var pack [52]byte
for i := 0; i < 26; i++ {
pack[i] = 'R'
pack[26+i] = 'B'
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(52, func(i, j int) {
pack[i], pack[j] = pack[j], pack[i]
})
 
// Deal from pack into 3 stacks.
var red, black, discard []byte
for i := 0; i < 51; i += 2 {
switch pack[i] {
case 'B':
black = append(black, pack[i+1])
case 'R':
red = append(red, pack[i+1])
}
discard = append(discard, pack[i])
}
lr, lb, ld := len(red), len(black), len(discard)
fmt.Println("After dealing the cards the state of the stacks is:")
fmt.Printf(" Red : %2d cards -> %c\n", lr, red)
fmt.Printf(" Black : %2d cards -> %c\n", lb, black)
fmt.Printf(" Discard: %2d cards -> %c\n", ld, discard)
 
// Swap the same, random, number of cards between the red and black stacks.
min := lr
if lb < min {
min = lb
}
n := 1 + rand.Intn(min)
rp := rand.Perm(lr)[:n]
bp := rand.Perm(lb)[:n]
fmt.Printf("\n%d card(s) are to be swapped.\n\n", n)
fmt.Println("The respective zero-based indices of the cards(s) to be swapped are:")
fmt.Printf(" Red : %2d\n", rp)
fmt.Printf(" Black : %2d\n", bp)
for i := 0; i < n; i++ {
red[rp[i]], black[bp[i]] = black[bp[i]], red[rp[i]]
}
fmt.Println("\nAfter swapping, the state of the red and black stacks is:")
fmt.Printf(" Red : %c\n", red)
fmt.Printf(" Black : %c\n", black)
 
// Check that the number of black cards in the black stack equals
// the number of red cards in the red stack.
rcount, bcount := 0, 0
for _, c := range red {
if c == 'R' {
rcount++
}
}
for _, c := range black {
if c == 'B' {
bcount++
}
}
 
fmt.Println("\nThe number of red cards in the red stack =", rcount)
fmt.Println("The number of black cards in the black stack =", bcount)
if rcount == bcount {
fmt.Println("So the asssertion is correct!")
} else {
fmt.Println("So the asssertion is incorrect!")
}
}</lang>
 
{{out}}
First sample run:
<pre>
After dealing the cards the state of the stacks is:
Red : 15 cards -> [B R R R R B R B R B B R B B B]
Black : 11 cards -> [R B R B B R R B B B B]
Discard: 26 cards -> [R R R R B B B B B B B R R R B B B R R R R R R B R R]
 
8 card(s) are to be swapped.
 
The respective zero-based indices of the cards(s) to be swapped are:
Red : [10 2 11 14 12 3 9 5]
Black : [ 7 10 3 0 5 8 6 1]
 
After swapping, the state of the red and black stacks is:
Red : [B R B B R B R B R R B B R B R]
Black : [B B R R B B B B R B R]
 
The number of red cards in the red stack = 7
The number of black cards in the black stack = 7
So the asssertion is correct!
</pre>
 
Second sample run:
<pre>
After dealing the cards the state of the stacks is:
Red : 12 cards -> [B B R B R R R B B B R B]
Black : 14 cards -> [B R R R B R R B B R R B R R]
Discard: 26 cards -> [R R B B B R R B B B R R R B B B R B B B R B R R R B]
 
2 card(s) are to be swapped.
 
The respective zero-based indices of the cards(s) to be swapped are:
Red : [ 1 6]
Black : [11 12]
 
After swapping, the state of the red and black stacks is:
Red : [B B R B R R R B B B R B]
Black : [B R R R B R R B B R R B R R]
 
The number of red cards in the red stack = 5
The number of black cards in the black stack = 5
So the asssertion is correct!
</pre>
 
9,490

edits