Add files via upload
This commit is contained in:
parent
53800fff2a
commit
dbe56cea9c
@ -112,7 +112,7 @@
|
||||
"\n",
|
||||
" collapse('BBBBBrrrrBBBB') == 'BrB'\n",
|
||||
" \n",
|
||||
"From now on I'll use the term *hand* rather than *abstract hand* for things like `'BBBBBrrrrBBBB'`, and I'll use the terms *sequence* or *seq* for the collapsed version, `'BrB'`.\n",
|
||||
"From now on I'll just say *hand* rather than *abstract hand* for `'BBBBBrrrrBBBB'`, and I'll use the term *sequence* (or the abbreviation *seq*) for the collapsed version, `'BrB'`.\n",
|
||||
"\n",
|
||||
"# Properly Ordered Hands\n",
|
||||
"\n",
|
||||
@ -248,18 +248,15 @@
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Fraction(51083, 83895)"
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"51083/83895\n"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"orderable_probability(6)"
|
||||
"print(orderable_probability(6))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -308,14 +305,14 @@
|
||||
"\n",
|
||||
"That looks good, but if we want to get to 13-card hands, we would have to handle 4<sup>13</sup> = 67,108,864 `deals`, which would take a while. I can speed things up by taking advantage of two key properties of orderable sequences:\n",
|
||||
"\n",
|
||||
"1. **An orderable sequence can have at most 7 runs.** We know that a properly ordered hand can have at most 4 runs. But a move can reduce the number of runs by only 3 at the most: one run can be reduced when we remove the block (if the cards on either side of the block are the same), and two more can be reduced when we replace the block (if the left and right ends of the block match the suits to the left and right of the new position). Here's an example of moving a block [bracketed] to reduce the number of runs from 6 to 3:\n",
|
||||
"1. **An orderable sequence can have at most 7 runs.** We know that a properly ordered hand can have at most 4 runs. But a move can reduce the number of runs by only 3 at the most: one run can be reduced when we remove the block (if the cards on either side of the block are the same), and two more can be reduced when we re-insert the block (if the left and right ends of the block match the suits to the left and right of the new position). Here's an example of moving a block [bracketed] to reduce the number of runs from 6 to 3:\n",
|
||||
"\n",
|
||||
" bRB[bR]B => b[bR]RBB = bRB\n",
|
||||
" \n",
|
||||
"2. **Adding a suit to the end of an unorderable sequence can't make it orderable.** Even after we move a block, you can't make an unordered hand ordered by inserting one suit.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"I'll redefine `deals(N)` to hold only orderable hands, redefine `orderable(seq)` to immediately reject sequences longer than 7, and redefine `orderable_probability(N)` to just add up the probabalities in `deals(N)`:"
|
||||
"I'll redefine `deals(N)` to hold only orderable hands, redefine `orderable(seq)` to immediately reject sequences longer than 7, and redefine `orderable_probability(N)` to just add up the probabilities in `deals(N)`, since everything there is orderable."
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -381,8 +378,8 @@
|
||||
"11: 1.9% = 22673450197/1219690678500\n",
|
||||
"12: 0.7% = 1751664923/238130084850\n",
|
||||
"13: 0.3% = 30785713171/11112737293000\n",
|
||||
"CPU times: user 16.6 s, sys: 337 ms, total: 17 s\n",
|
||||
"Wall time: 19.3 s\n"
|
||||
"CPU times: user 15 s, sys: 245 ms, total: 15.2 s\n",
|
||||
"Wall time: 16.3 s\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -425,9 +422,9 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"So we looked at over 7 million hands, but only 4373 different collapsed sequences. And once we hit `N` = 7, we've seen all the sequences we're ever going to see. From `N` = 8 and up, almost all the computation goes into computing the probability of each hand, and collapsing the hand into a sequence, not into deciding the orderability of each sequence.\n",
|
||||
"So we looked at over a million hands, but only 1540 different collapsed sequences. And once we hit `N` = 7, we've seen all the sequences we're ever going to see. From `N` = 8 and up, almost all the computation goes into computing the probability of each hand, and collapsing the hand into a sequence, not into deciding the orderability of each sequence.\n",
|
||||
"\n",
|
||||
"We save a lot of space in the `deals(N)` caches. Instead of storing all 4<sup>13</sup> hands for `deals(13)`, the output above says that just 0.3% of the hands are orderable, so we reduced the cache size by a factor of 300 or so.\n",
|
||||
"We also save a lot of space in the `deals(N)` caches. Instead of storing all 4<sup>13</sup> hands for `deals(13)`, the output above says that just 0.3% of the hands are orderable, so we reduced the cache size by a factor of 300 or so.\n",
|
||||
"\n",
|
||||
"# Unit Tests\n",
|
||||
"\n",
|
||||
|
Loading…
Reference in New Issue
Block a user