Mind boggling card trick: Difference between revisions

→‎{{header|REXX}}: added a pluralizer and a commatizer to the program, also added more information to the "results" output sentence, allowed the specification of the number of shuffles, created a (new) deck of cards as if they came from a new box.
(→‎{{header|REXX}}: added a pluralizer and a commatizer to the program, also added more information to the "results" output sentence, allowed the specification of the number of shuffles, created a (new) deck of cards as if they came from a new box.)
Line 658:
=={{header|REXX}}==
Programming notes:   This REXX version uses a neat trick that the '''Python''' entry uses:   instead of using a deck of
<br>cards, &nbsp; it just uses a "deck" of numbers. &nbsp;that Oddcorrespond numbersto representthe redorder of playing cards, eventhat numberscame representout blackof a new box of cards.
<br>cards. &nbsp; Odd numbers represent red cards, even numbers represent black cards.
<br>This could've been possibly simplified by using negative and positive numbers.
 
<br>This could've been possibly simplified by using negative and positive numbers., &nbsp; ''or'' &nbsp; more accurately, &nbsp; the suits and
<br>a pip, but it would've taken more program logic to determine the color of the suits in a very succinct and efficient way.
 
Also, code was added to perform any number of trials. &nbsp; Code was also added to allow repeatability by specifying a
<br>''seed'' &nbsp; value for the &nbsp; '''random''' &nbsp; BIF.
 
A new deck of cards is always created &nbsp; &nbsp; ''as if the playing cards were manufactured and put into a box'', &nbsp; &nbsp; that is,
The size of the card deck can also be specified.
<br>13 spades (<big>♠</big>) in a row &nbsp; (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K), &nbsp; &nbsp; 13
hearts (<big>♥</big>), &nbsp; &nbsp; 13 clubs (<big>♣</big>), &nbsp; &nbsp; and 13 diamonds (<big>♦</big>).
<br>(Various card playing manufacturers would arrange a new playing deck differently, but that isn't a concern.)
 
The number of cards in the deck and also the number of shuffles can be specified. &nbsp; One shuffle swaps two random
<br>cards, two shuffles swaps four random cards ···
 
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.
<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= 500 1000 /*Not specified? Then use the default.*/
if #=='' | #=="," then #= 52 52 /* " " " " " " */
if shuffs=='' | shuffs=="," then shuffs= #%2 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /*if integer, use this as a RANDOM seed*/
ok=0 /*the number of "expected" good trials.*/
Line 679 ⟶ 692:
end /*trials*/ /*#: is the number of cards in the deck*/
pc= (100*ok/trials)'%' /*calculate the % asserted correctly.*/
say "Correctness of the mathematician's assertion:" pc ' (out of' trials "commas(trials).",
"trial"s(trials)') using a deck of ' commas(#) ,
" card"s(#)', and doing ' commas(shuffs) ' shuffle's(shuffs).
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: return random(1, word( arg(1) #, 1) ) /*gen a random number from 1 ──► arg. */
createcommas: @.=;parse arg _; do j=length(_)-3 to do j=1 forby #-3; _=insert(',', @.j=_, j); end /*j*/; return return_
create: @.=; k=0; do j=1 by 4 for #; k=k+1; @.k= j; if k//13==0 then j=j+1; end; return
isRed: return arg(1) // 2 /*if arg(1) is odd, the card is RED.*/
shuffles: do j=1 for #; x=?if arg(1); y=?();=1 parsethen valuereturn @.xarg(3); @.y withreturn @.yword( @.x;arg(2) 's', end;1) return/*pluralizer.*/
shuffle: do j=1 for shuffs; x=?(); y=?(); parse value @.x @.y with @.y @.x; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
count: Rn=0; Bn=0; do j=1 for words(R); Rn=Rn+ isRed(word(R,j)) ; end
Line 699 ⟶ 716:
return /*discard pile not used.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
swap: $= ?( min( words(R), words(B) ) ); Rc=; Bc= /*ensure we can swap $ cards.*/
if $==0 then return do $ /*$: A ispile thehas numberno ofcards? swaps.return*/
do ?($) /*$: is the number of swaps.*/
R?= ?( words(R) ) /*a random card in RED pile.*/
B?= ?( words(B) ) /*" " " " BLACK " */
/* "reds" to be swapped.*/ Rc= Rc word(R, R?); R= delword(R, R?, 1) /*del card*/
/*"blacks" " " " */ Bc= Bc word(B, B?); B= delword(B, B?, 1) /* " " */
end /*?($)*/
R=R Bc; B=B Rc; return /*add swapped cards to piles.*/</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Correctness of the mathematician's assertion: 100% (out of 50010,000 trials) using a deck of 52 cards, and doing 26 shuffles.
</pre>