Mind boggling card trick: Difference between revisions

Added Easylang
(New post.)
(Added Easylang)
(4 intermediate revisions by 4 users not shown)
Line 492:
 
Result: 5 successes out of 5 simulations</pre>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
 
public class MindBogglingCardTrick
{
public static void Main(string[] args)
{
List<char> cards = new List<char>();
cards.AddRange(Enumerable.Repeat('R', 26));
cards.AddRange(Enumerable.Repeat('B', 26));
Shuffle(cards);
 
List<char> redPile = new List<char>();
List<char> blackPile = new List<char>();
List<char> discardPile = new List<char>();
 
for (int i = 0; i < 52; i += 2)
{
if (cards[i] == 'R')
{
redPile.Add(cards[i + 1]);
}
else
{
blackPile.Add(cards[i + 1]);
}
discardPile.Add(cards[i]);
}
 
Console.WriteLine("A sample run.\n");
Console.WriteLine("After dealing the cards the state of the piles is:");
Console.WriteLine($" Red : {redPile.Count} cards -> {string.Join(",", redPile)}");
Console.WriteLine($" Black : {blackPile.Count} cards -> {string.Join(",", blackPile)}");
Console.WriteLine($" Discard: {discardPile.Count} cards -> {string.Join(",", discardPile)}");
 
Random random = new Random();
int minimumSize = Math.Min(redPile.Count, blackPile.Count);
int choice = random.Next(1, minimumSize + 1);
 
List<int> redIndexes = Enumerable.Range(0, redPile.Count).ToList();
List<int> blackIndexes = Enumerable.Range(0, blackPile.Count).ToList();
Shuffle(redIndexes);
Shuffle(blackIndexes);
List<int> redChosenIndexes = redIndexes.Take(choice).ToList();
List<int> blackChosenIndexes = blackIndexes.Take(choice).ToList();
 
Console.WriteLine($"\nNumber of cards are to be swapped: {choice}");
Console.WriteLine("The respective zero-based indices of the cards to be swapped are:");
Console.WriteLine($" Red : {string.Join(", ", redChosenIndexes)}");
Console.WriteLine($" Black: {string.Join(", ", blackChosenIndexes)}");
 
for (int i = 0; i < choice; i++)
{
char temp = redPile[redChosenIndexes[i]];
redPile[redChosenIndexes[i]] = blackPile[blackChosenIndexes[i]];
blackPile[blackChosenIndexes[i]] = temp;
}
 
Console.WriteLine($"\nAfter swapping cards the state of the red and black piles is:");
Console.WriteLine($" Red : {string.Join(", ", redPile)}");
Console.WriteLine($" Black: {string.Join(", ", blackPile)}");
 
int redCount = redPile.Count(ch => ch == 'R');
int blackCount = blackPile.Count(ch => ch == 'B');
 
Console.WriteLine($"\nThe number of red cards in the red pile: {redCount}");
Console.WriteLine($"The number of black cards in the black pile: {blackCount}");
if (redCount == blackCount)
{
Console.WriteLine("So the assertion is correct.");
}
else
{
Console.WriteLine("So the assertion is incorrect.");
}
}
 
private static void Shuffle<T>(List<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
</syntaxhighlight>
{{out}}
<pre>
A sample run.
 
After dealing the cards the state of the piles is:
Red : 13 cards -> B,R,R,R,R,B,R,R,R,B,R,R,B
Black : 13 cards -> R,R,B,B,R,B,R,B,B,B,B,B,B
Discard: 26 cards -> R,R,B,R,B,R,R,B,R,B,R,R,B,B,R,B,R,B,B,B,B,B,R,B,R,R
 
Number of cards are to be swapped: 3
The respective zero-based indices of the cards to be swapped are:
Red : 9, 8, 1
Black: 1, 11, 5
 
After swapping cards the state of the red and black piles is:
Red : B, B, R, R, R, B, R, R, B, R, R, R, B
Black: R, B, B, B, R, R, R, B, B, B, B, R, B
 
The number of red cards in the red pile: 8
The number of black cards in the black pile: 8
So the assertion is correct.
 
</pre>
 
 
=={{header|C++}}==
Line 540 ⟶ 662:
std::cout << " Red : " << std::setw(2) << red_pile.size() << " cards -> "; print_vector<char>(red_pile);
std::cout << " Black : " << std::setw(2) << black_pile.size() << " cards -> "; print_vector<char>(black_pile);
std::cout << " Discard: " << std::setw(2) << discard_pile.size() << " cards -> "; print_vector<char>(discard_pile);
<< " cards -> "; print_vector<char>(discard_pile);
 
const int32_t minimum_size = std::min(red_pile.size(), black_pile.size());
Line 595 ⟶ 718:
}
</syntaxhighlight>
{{ out }}
<pre>
A sample run.
Line 642 ⟶ 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 766 ⟶ 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 2,267 ⟶ 2,596:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var R = 82 // ASCII 'R'
2,056

edits