RosettaCodeData/Task/Perfect-shuffle/00-TASK.txt

87 lines
5.1 KiB
Plaintext

A perfect shuffle (or [https://en.wikipedia.org/wiki/Faro_shuffle faro/weave shuffle]) means splitting a deck of cards into equal halves, and perfectly interleaving them - so that you end up with the first card from the left half, followed by the first card from the right half, and so on:
<big>
<!-- START OF DIAGRAM -->
::: <div style="display:inline-block;margin:0.5em 1.5em"><tt><span style="background:#8DF">7♠</span> <span style="background:#8DF">8♠</span> <span style="background:#8DF">9♠</span> <span style="background:#FB5">J♠</span> <span style="background:#FB5">Q♠</span> <span style="background:#FB5">K♠</span></tt></div><div style="display:inline-block">&rarr;<div style="display:inline-block;vertical-align:middle;margin:0.5em 1.5em"><tt><span style="background:#8DF">7♠</span>&nbsp; <span style="background:#8DF">8♠</span>&nbsp; <span style="background:#8DF">9♠</span></tt><br><tt>&nbsp;&nbsp;<span style="background:#FB5">J♠</span>&nbsp; <span style="background:#FB5">Q♠</span>&nbsp; <span style="background:#FB5">K♠</span></tt></div></div><div style="display:inline-block">&rarr;<div style="display:inline-block;vertical-align:middle;margin:0.5em 1.5em"><tt><span style="background:#8DF">7♠</span> <span style="background:#FB5">J♠</span> <span style="background:#8DF">8♠</span> <span style="background:#FB5">Q♠</span> <span style="background:#8DF">9♠</span> <span style="background:#Fb5">K♠</span></tt></div></div>
<!-- END OF DIAGRAM -->
</big>
When you repeatedly perform perfect shuffles on an even-sized deck of unique cards, it will at some point arrive back at its original order. How many shuffles this takes, depends solely on the number of cards in the deck - for example for a deck of eight cards it takes three shuffles:
<big>
<!-- START OF DIAGRAM -->
::::: {| style="border-spacing:0.5em 0;border-collapse:separate;margin:0 1em;text-align:right"
|-
| <small>''original:''</small> ||
<tt style="background:#122CF8;color:#B8C0FD;padding:0 0.3em">1</tt>
<tt style="background:#332AD7;color:#C2C0F3;padding:0 0.3em">2</tt>
<tt style="background:#5428B7;color:#CCBFEA;padding:0 0.3em">3</tt>
<tt style="background:#752696;color:#D6BEE0;padding:0 0.3em">4</tt>
<tt style="background:#962576;color:#E0BED6;padding:0 0.3em">5</tt>
<tt style="background:#B72355;color:#EABDCC;padding:0 0.3em">6</tt>
<tt style="background:#D82135;color:#F3BDC3;padding:0 0.3em">7</tt>
<tt style="background:#F92015;color:#FDBDB9;padding:0 0.3em">8</tt>
|-
| <small>''after 1st shuffle:''</small> ||
<tt style="background:#122CF8;color:#B8C0FD;padding:0 0.3em">1</tt>
<tt style="background:#962576;color:#E0BED6;padding:0 0.3em">5</tt>
<tt style="background:#332AD7;color:#C2C0F3;padding:0 0.3em">2</tt>
<tt style="background:#B72355;color:#EABDCC;padding:0 0.3em">6</tt>
<tt style="background:#5428B7;color:#CCBFEA;padding:0 0.3em">3</tt>
<tt style="background:#D82135;color:#F3BDC3;padding:0 0.3em">7</tt>
<tt style="background:#752696;color:#D6BEE0;padding:0 0.3em">4</tt>
<tt style="background:#F92015;color:#FDBDB9;padding:0 0.3em">8</tt>
|-
| <small>''after 2nd shuffle:''</small> ||
<tt style="background:#122CF8;color:#B8C0FD;padding:0 0.3em">1</tt>
<tt style="background:#5428B7;color:#CCBFEA;padding:0 0.3em">3</tt>
<tt style="background:#962576;color:#E0BED6;padding:0 0.3em">5</tt>
<tt style="background:#D82135;color:#F3BDC3;padding:0 0.3em">7</tt>
<tt style="background:#332AD7;color:#C2C0F3;padding:0 0.3em">2</tt>
<tt style="background:#752696;color:#D6BEE0;padding:0 0.3em">4</tt>
<tt style="background:#B72355;color:#EABDCC;padding:0 0.3em">6</tt>
<tt style="background:#F92015;color:#FDBDB9;padding:0 0.3em">8</tt>
|-
| <small>''after 3rd shuffle:''</small> ||
<tt style="background:#122CF8;color:#B8C0FD;padding:0 0.3em">1</tt>
<tt style="background:#332AD7;color:#C2C0F3;padding:0 0.3em">2</tt>
<tt style="background:#5428B7;color:#CCBFEA;padding:0 0.3em">3</tt>
<tt style="background:#752696;color:#D6BEE0;padding:0 0.3em">4</tt>
<tt style="background:#962576;color:#E0BED6;padding:0 0.3em">5</tt>
<tt style="background:#B72355;color:#EABDCC;padding:0 0.3em">6</tt>
<tt style="background:#D82135;color:#F3BDC3;padding:0 0.3em">7</tt>
<tt style="background:#F92015;color:#FDBDB9;padding:0 0.3em">8</tt>
|}
<!-- END OF DIAGRAM -->
</big>
<p style="font-size:115%; margin:1em 0 0.5em 0">'''''The Task'''''</p>
# Write a function that can perform a perfect shuffle on an even-sized list of values.
# Call this function repeatedly to count how many shuffles are needed to get a deck back to its original order, for each of the deck sizes listed under "Test Cases" below.
#* <small>You can use a list of numbers (or anything else that's convenient) to represent a deck; just make sure that all "cards" are unique within each deck.</small>
#* <small>Print out the resulting shuffle counts, to demonstrate that your program passes the test-cases.</small>
<p style="font-size:115%; margin:1em 0 0.5em 0">'''''Test Cases'''''</p>
::::: {| class="wikitable"
|-
! input ''(deck size)'' !! output ''(number of shuffles required)''
|-
| 8 || 3
|-
| 24 || 11
|-
| 52 || 8
|-
| 100 || 30
|-
| 1020 || 1018
|-
| 1024 || 10
|-
| 10000 || 300
|}
<br><br>