Mind boggling card trick: Difference between revisions

(Add C# implementation)
(2 intermediate revisions by 2 users not shown)
Line 766:
Drumroll...
There were 7!
</pre>
 
=={{header|EasyLang}}==
{{trans|Wren}}
<syntaxhighlight>
proc shuffle . a[] .
for i = len a[] downto 2
r = randint i
swap a[r] a[i]
.
.
func$ a2str a[] .
for v in a[]
r$ &= strchar v & " "
.
return r$
.
R = strcode "R"
B = strcode "B"
for i to 26
pack[] &= R
pack[] &= B
.
shuffle pack[]
#
for i = 1 step 2 to 51
if pack[i] = B
black[] &= pack[i + 1]
else
red[] &= pack[i + 1]
.
discard[] &= pack[i]
.
print "After dealing the cards the state of the stacks is:"
print " Red : " & a2str red[]
print " Black : " & a2str black[]
print " Discard: " & a2str discard[]
for i to len red[]
rp[] &= i
.
for i to len black[]
bp[] &= i
.
shuffle rp[]
shuffle bp[]
n = randint lower len red[] len black[]
len rp[] n
len bp[] n
#
for i to n
h = red[rp[i]]
red[rp[i]] = black[bp[i]]
black[bp[i]] = h
.
print ""
print "After swapping " & n & " cards the state of the stacks is:"
print " Red : " & a2str red[]
print " Black : " & a2str black[]
#
for c in red[]
red += if c = R
.
for c in black[]
black += if c = B
.
print ""
print "The number of red cards in the red stack = " & red
print "The number of black cards in the black stack = " & black
if red = black
print "So the asssertion is correct!"
.
</syntaxhighlight>
{{out}}
<pre>
After dealing the cards the state of the stacks is:
Red : B B B R B B B R R R R R R B
Black : B B B R R B R B B R R B
Discard: R B R B B R B R R B B R R R R R R R R B B B B B R B
 
After swapping 7 cards the state of the stacks is:
Red : R R B R R B B R B R B R R B
Black : B B R B R B B B R B R B
 
The number of red cards in the red stack = 8
The number of black cards in the black stack = 8
So the asssertion is correct!
</pre>
 
Line 890 ⟶ 976:
B in black: 5
</pre>
 
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vbnet">Randomize Timer
Dim Shared As String Deck(52), BlackPile(52), RedPile(52), DiscardPile(52)
Dim Shared As Integer BlP, ReP, DiP
 
Sub Show
Dim As Integer i
Print "Black pile: ";
For i = 0 To BlP-1
Print BlackPile(i);
Next i
Print !"\nRed pile: ";
For i = 0 To ReP-1
Print RedPile(i);
Next i
Print !"\nDiscard pile: ";
For i = 0 To DiP-1
Print DiscardPile(i);
Next i
Print
End Sub
 
Dim As String BlackBunch(52), RedBunch(52)
Dim As Integer i, j, m, x, y, BB, RB, BC, RC
Dim As Integer ub = Ubound(Deck)
 
For i = 0 To (ub/2)-1
Deck(i) = "r"
Deck(i+26) = "b"
Next i
For i = 0 To ub-1
y = Int(Rnd * 51) + 1
Swap Deck(y), Deck(i)
Next i
BlP = 0
ReP = 0
DiP = 0
For i = 0 To ub-1
If Deck(i) = "b" Then
BlackPile(BlP) = Deck(i+1)
BlP += 1
Else
RedPile(ReP) = Deck(i+1)
ReP += 1
End If
DiscardPile(DiP) = Deck(i)
DiP += 1
i += 1
Next i
 
Show
m = BlP
If ReP < m Then m = ReP
x = Int(Rnd * m) + 1
Print "Swap "; x; " cards between the red and black piles."
RB = 0
BB = 0
For i = 0 To x-1
Do
y = Int(Rnd * ReP)
Loop Until RedPile(y) <> "0"
RedBunch(RB) = RedPile(y)
RB += 1
RedPile(y) = "0"
Next i
For i = 0 To x-1
Do
y = Int(Rnd * BlP)
Loop Until BlackPile(y) <> "0"
BlackBunch(BB) = BlackPile(y)
BB += 1
BlackPile(y) = "0"
Next i
RB = 0
For i = 0 To x-1
j = 0
While BlackPile(j) <> "0"
j += 1
Wend
BlackPile(j) = RedBunch(RB)
RB += 1
Next i
BB = 0
For i = 0 To x-1
j = 0
While RedPile(j) <> "0"
j += 1
Wend
RedPile(j) = BlackBunch(BB)
BB += 1
Next i
 
Show
BC = 0
For i = 0 To BlP-1
If BlackPile(i) = "b" Then BC += 1
Next i
RC = 0
For i = 0 To ReP-1
If RedPile(i) = "r" Then RC += 1
Next i
Print "The number of black cards in the black pile is "; BC
Print "The number of red cards in the red pile is "; RC
Print Using "The mathematician's assertion is &correct."; Iif(BC <>RC, "not ", "")
 
Sleep</syntaxhighlight>
{{out}}
<pre>Black pile: rbrbrrrrbrrrrb
Red pile: bbbbrrbrbbbr
Discard pile: bbrrbrrbbrbrrrrbrbbrbbrbbb
Swap 11 cards between the red and black piles.
Black pile: rbbrbrbbbbrbrb
Red pile: rrrrrrbbrrbr
Discard pile: bbrrbrrbbrbrrrrbrbbrbbrbbb
The number of black cards in the black pile is 9
The number of red cards in the red pile is 9
The mathematician's assertion is correct.</pre>
 
=={{header|Go}}==
Line 1,435 ⟶ 1,640:
BBBBBBBB = Black cards in the black pile
true</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
Since jq does not include a PRN generator, an external source of entropy such as /dev/urandom is assumed.
A suitable invocation of jq would be along the lines of:
<pre>
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -nr trick.jq
</pre>
<syntaxhighlight lang="jq">
### Generic utilities
def count(s): reduce s as $x (0; . + 1);
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
 
 
### Pseuo-random numbers
 
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def sample:
if length == 0 # e.g. null or []
then null
else .[length|prn]
end;
 
def knuthShuffle:
length as $n
| if $n <= 1 then .
else {i: $n, a: .}
| until(.i == 0;
.i += -1
| (.i + 1 | prn) as $j
| .a[.i] as $t
| .a[.i] = .a[$j]
| .a[$j] = $t)
| .a
end;
 
 
### Cards
 
def R: "R"; # 82 ASCII
def B: "B"; # 66 ASCII
 
# Create deck, half red, half black and shuffle it.
def deck:
([range(0;26)|R] + [range(0;26)|B]) | knuthShuffle;
 
# Deal from `deck` into three stacks: {black, red, discard}
def deal:
deck as $deck
| reduce range(0; 51; 2) as $i (.;
if $deck[$i] == B
then .black += [$deck[$i+1]]
else .red += [$deck[$i+1]]
end
| .discard += [$deck[$i]] );
 
def proceed:
def p: join(" ");
(.red|length) as $lr
| (.black|length) as $lb
| (.discard|length) as $ld
 
| def displayStacks($discard):
" Red : \($lr|lpad(2)) cards -> \(.red|p)",
" Black : \($lb|lpad(2)) cards -> \(.black|p)",
(select($discard)
| " Discard: \($ld) cards -> \(.discard|p)") ;
 
# Input: {red, black}
def swap($n):
. + { rp: ([range(0; $lr)] | knuthShuffle[0:$n] ),
bp: ([range(0; $lb)] | knuthShuffle[0:$n]) }
| reduce range(0;$n) as $i (.;
.red[.rp[$i]] as $t
| .red[.rp[$i]] = .black[.bp[$i]]
| .black[.bp[$i]] = $t);
 
def epilog:
# Check that the number of black cards in the black stack equals
# the number of red cards in the red stack:
count(select(.red[] == R)) as $rcount
| count(select(.black[] == B)) as $bcount
| "\nThe number of red cards in the red stack = \($rcount)",
"The number of black cards in the black stack = \($bcount)",
if $rcount == $bcount
then "So the assertion is correct!"
else "So the assertion is incorrect!"
end;
 
"After dealing the cards, the stacks are as follows:",
displayStacks(true),
# Swap the same, random, number of cards between the red and black stacks.
( (if $lr < $lb then $lr else $lb end) as $min
| (($min - 1|prn) + 1) as $n
| swap($n)
| "\n\($n) card(s) are to be swapped.",
"The respective zero-based indices of the cards to be swapped are:",
" Red : \(.rp|map(lpad(3))|p)",
" Black : \(.bp|map(lpad(3))|p)",
"\nAfter swapping, the red and black stacks are as follows:",
displayStacks(false),
epilog ) ;
 
deal | proceed
</syntaxhighlight>
{{output}}
<pre>
After dealing the cards, the stacks are as follows:
Red : 12 cards -> R B R R B B B R R B R B
Black : 14 cards -> R B B R B R R R B R B R B R
Discard: 26 cards -> R R B B B R B R B R R B B B R B R B R B B B R B R R
 
1 card(s) are to be swapped.
The respective zero-based indices of the cards to be swapped are:
Red : 6
Black : 5
 
After swapping, the red and black stacks are as follows:
Red : 12 cards -> R B R R B B R R R B R B
Black : 14 cards -> R B B R B B R R B R 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 assertion is correct!
</pre>
 
=={{header|Julia}}==
2,489

edits