Anonymous user
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, it just uses a "deck" of numbers
<br>cards. Odd numbers represent red cards, even numbers represent black cards.
<br>This could've been possibly simplified by using negative and positive numbers.▼
▲
<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. Code was also added to allow repeatability by specifying a
<br>''seed'' value for the '''random''' BIF.
A new deck of cards is always created ''as if the playing cards were manufactured and put into a box'', that is,
<br>13 spades (<big>♠</big>) in a row (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K), 13
hearts (<big>♥</big>), 13 clubs (<big>♣</big>), 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. One shuffle swaps two random
<br>cards, two shuffles swaps four random cards ···
Extra coding was added to keep ''singularities'' (opposite of a plural) to keep the English gooder (''sic''), 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 .
if trials=='' | trials=="," then trials=
if #=='' | #=="," then #=
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'
"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. */
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.*/
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: $=
if $==0 then 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= when using the default inputs:}}
<pre>
Correctness of the mathematician's assertion: 100% (out of
</pre>
|