Mind boggling card trick: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Lua}}: added Lua solution)
m (syntax highlighting fixup automation)
Line 36:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">//import random
V n = 52
V Black = ‘Black’
Line 73:
print(‘Yeha! The mathematicians assertion is correct.’)
E
print(‘Whoops - The mathematicians (or my card manipulations) are flakey’)</langsyntaxhighlight>
 
{{out}}
Line 84:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">use AppleScript version "2.5" -- OS X 10.11 (El Capitan) or later
use framework "Foundation"
use framework "GameplayKit" -- For randomising functions.
Line 175:
return text 1 thru -2 of join(output, linefeed)
end task
return task()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Test 1: Assertion is true
Red pile: 3♦️, 2♦️, 9♣️, 6♣️, 3♥️, 9♥️, A♠️, 3♠️, 2♥️, Q♠️, 7♥️, K♥️
Black pile: Q♥️, J♣️, 7♦️, 4♦️, 8♦️, 5♥️, K♣️, 10♠️, 10♣️, 7♣️, 2♣️, 9♦️, 5♣️, 6♦️
Line 206:
Black pile: 2♦️, 2♥️, 8♣️, 9♣️, 7♥️, 3♣️, Q♠️, 10♠️, 8♥️, 10♣️, K♣️, 5♠️, 4♦️, 3♥️, 6♥️
Discards: 7♦️, J♥️, 7♣️, 4♠️, 6♦️, 4♣️, 7♠️, Q♥️, J♠️, Q♦️, 6♠️, J♣️, A♥️
9♦️, K♦️, J♦️, 3♠️, 5♣️, Q♣️, A♣️, K♠️, 8♠️, 9♥️, A♠️, 6♣️, A♦️"</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">;==========================================================
; create cards
cards := [], suits := ["♠","♥","♦","♣"], num := 0
Line 334:
SB_SetText(countB " Face Down Black Cards", 2)
return
;==========================================================</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Line 467:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 495:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">deck = ([:black, :red] * 26 ).shuffle
black_pile, red_pile, discard = [] of Symbol, [] of Symbol, [] of Symbol
 
Line 513:
puts "The magician predicts there will be #{black_pile.count( :black )} red cards in the other pile.
Drumroll...
There were #{red_pile.count( :red )}!"</langsyntaxhighlight>
{{out}}
<pre>The magician predicts there will be 7 red cards in the other pile.
Line 521:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
//Be boggled? Nigel Galloway: September 19th., 2018
let N=System.Random()
Line 528:
printf "A well shuffled deck -> "; List.iter (printf "%s ") fN; printfn ""
fN |> List.chunkBySize 2 |> List.groupBy List.head |> List.iter(fun(n,n')->printfn "The %s pile contains %s" n (fG n'))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 537:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors combinators.extras formatting fry
generalizations io kernel math math.ranges random sequences
sequences.extras ;
Line 589:
test-assertion ; ! step 4
 
MAIN: main</langsyntaxhighlight>
A run:
{{out}}
Line 644:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 721:
fmt.Println("So the asssertion is incorrect!")
}
}</langsyntaxhighlight>
 
{{out}}
Line 769:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import System.Random (randomRIO)
import Data.List (partition)
import Data.Monoid ((<>))
Line 836:
replaceAt i c l =
let (a, b) = splitAt i l
in a ++ c : drop 1 b</langsyntaxhighlight>
{{Out}}
<pre>Discarded: RRRRRBBBBBRBBBRBBRBRRBRRRB
Line 848:
=={{header|J}}==
The trick verb returns 0 if the magician's proposition fails, otherwise 1.
<syntaxhighlight lang="j">
<lang J>
NB. A failed assertion looks like this
assert 0
|assertion failure: assert
| assert 0
</langsyntaxhighlight>
We find that in three runs the trick works.
<syntaxhighlight lang="j">
<lang J>
shuffle =: {~ ?~@:#
f =: (,&.> {:)~
Line 903:
black pile BrrBrrrBBrr
red pile rBrBrBBrBBBBBBB
</syntaxhighlight>
</lang>
 
=={{header|Javascript}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,068:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Discarded: BRBRBRRRRBBBRBBBRBBBBBRRBR
Line 1,080:
=={{header|Julia}}==
{{trans|Raku}}
<langsyntaxhighlight lang="julia">const rbdeck = split(repeat('R', 26) * repeat('B', 26), "")
shuffledeck() = shuffle(rbdeck)
 
Line 1,131:
mindbogglingcardtrick(false)
end
</langsyntaxhighlight>{{output}}<pre>
Shuffled deck is: SubString{String}["B", "B", "R", "B", "B", "R", "B", "R", "B", "R", "B", "B", "B", "R", "B", "R", "R", "B", "R", "R",
"R", "R", "R", "R", "B", "R", "B", "B", "B", "R", "R", "B", "B", "R", "B", "B", "R", "R", "B", "R", "R", "R", "R", "B", "B", "R", "B",
Line 1,170:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.2.61
 
import java.util.Random
Line 1,231:
println("So the asssertion is incorrect!")
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,279:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- support:
function T(t) return setmetatable(t, {__index=table}) end
table.range = function(t,n) local s=T{} for i=1,n do s[i]=i end return s end
Line 1,329:
-- demo:
math.randomseed(os.time())
for i = 1,3 do cardtrick() end</langsyntaxhighlight>
{{out}}
<pre>1. DECK : RBBBRBRBRBBBBRRRRBRBBBRRBRRBRRBRBBRRBBRRBBRRRBBRBRBR
Line 1,371:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">s = RandomSample@Flatten[{Table[0, 26], Table[1, 26]}]
g = Take[s, {1, -1, 2}]
d = Take[s, {2, -1, 2}]
Line 1,386:
finala = Join[a[[1]], b[[2]]]
finalb = Join[b[[1]], a[[2]]]
Count[finala, 1] == Count[finalb, 0]</langsyntaxhighlight>
 
{{output}}
Line 1,408:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat, strutils
 
type Color {.pure.} = enum Red = "R", Black = "B"
Line 1,460:
echo "So the asssertion is correct."
else:
echo "So the asssertion is incorrect."</langsyntaxhighlight>
 
{{out}}
Line 1,485:
Trying several non-random deck orderings, in addition to a shuffled one. Predictably, the trick always works.
{{trans|Raku}}
<langsyntaxhighlight lang="perl">sub trick {
my(@deck) = @_;
my $result .= sprintf "%-28s @deck\n", 'Starting deck:';
Line 1,528:
print trick(@deck);
@deck = sort { -1 + 2*int(rand 2) } @deck; # poor man's shuffle
print trick(@deck);</langsyntaxhighlight>
{{out}}
<pre>Starting deck: R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B R B
Line 1,559:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">52</span><span style="color: #0000FF;">,</span>
Line 1,594:
<span style="color: #004080;">string</span> <span style="color: #000000;">correct</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nr</span><span style="color: #0000FF;">==</span><span style="color: #000000;">nb</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"correct"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"**INCORRECT**"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d r in red, %d b in black - assertion %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">correct</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,610:
=={{header|Python}}==
The code is layed out to follow the task decription, leading to some deviations from the PEP8 ''guidelines''
<langsyntaxhighlight lang="python">import random
 
## 1. Cards
Line 1,658:
print('Yeha! The mathematicians assertion is correct.')
else:
print('Whoops - The mathematicians (or my card manipulations) are flakey')</langsyntaxhighlight>
 
A run.
Line 1,676:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ stack ] is discards ( --> s )
[ stack ] is red-card ( --> s )
[ stack ] is black-card ( --> s )
Line 1,710:
else [ say "false." ] cr cr ] is task ( --> )
 
5 times task</langsyntaxhighlight>
 
{{out}}
Line 1,735:
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
magictrick<-function(){
deck=c(rep("B",26),rep("R",26))
Line 1,775:
}
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,798:
{{works with|Rakudo|2018.08}}
 
<syntaxhighlight lang="raku" perl6line># Generate a shuffled deck
my @deck = shuffle;
put 'Shuffled deck: ', @deck;
Line 1,844:
@r[@ri] = @bs;
@b[@bi] = @rs;
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>Shuffled deck: B B B R B R R R B B R R R B R B R B R R R B R B B R R B B R R B R B R R R R B R R B B B B B B R R B B B
Line 1,877:
Extra coding was added to keep ''singularities'' &nbsp; (opposite of a plural) &nbsp; to keep the English gooder (''sic''), &nbsp; as well as
<br>adding commas to larger numbers.
<langsyntaxhighlight lang="rexx">/*REXX pgm mimics a boggling card trick; separates cards into 3 piles based on color ···*/
parse arg trials # shuffs seed . /*obtain optional arguments from the CL*/
if trials=='' | trials=="," then trials= 1000 /*Not specified? Then use the default.*/
Line 1,926:
/*"blacks" " " " */ Bc= Bc word(B, B?); B= delword(B, B?, 1) /* " " */
end /*?($)*/
R=R Bc; B=B Rc; return /*add swapped cards to piles.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,933:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">deck = ([:black, :red] * 26 ).shuffle
black_pile, red_pile, discard = [], [], []
 
Line 1,952:
Drumroll...
There were #{red_pile.count( :red )}!"
</syntaxhighlight>
</lang>
{{out}}
<pre>The magician predicts there will be 5 red cards in the other pile.
Line 1,961:
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">extern crate rand; // 0.5.5
use rand::Rng;
use std::iter::repeat;
Line 2,020:
println!("Number of black cards in black stack: {}", num_black);
println!("Number of red cards in red stack: {}", num_red);
}</langsyntaxhighlight>
 
{{out}}
Line 2,031:
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
 
Line 2,102:
} else {
System.print("So the asssertion is incorrect!")
}</langsyntaxhighlight>
 
{{out}}
Line 2,127:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">cards:=[1..52].pump(List,"isEven","toInt").shuffle(); // red==1
stacks:=T(List(),List()); // black stack [0], red stack [1]
blkStk,redStk := stacks;
Line 2,144:
else println("Boo, different stack lenghts");
 
fcn redBlack(cards){ cards.pump(String,fcn(c){ c and "R " or "B " }) }</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits