diff --git a/ipynb/Orderable Cards.ipynb b/ipynb/Orderable Cards.ipynb
index 82bbabf..dbc77d5 100644
--- a/ipynb/Orderable Cards.ipynb
+++ b/ipynb/Orderable Cards.ipynb
@@ -14,7 +14,7 @@
"\n",
"# Brute Force Versus Cleverness\n",
"\n",
- "The first thing to decide is how many `N`-card hands are there? If there are only a few, I could just enumerate them all and count. If there are a lot, I'll have to be more clever. The answer is (52 choose `N`), so we have:\n",
+ "The first thing to decide is how many `N`-card hands are there? If there are only a few, I could just enumerate them all and count how many are orderable. If there are a lot, I'll have to be more clever. The answer is (52 choose `N`), so we have:\n",
"\n",
"- 6 cards: 20,358,520 hands\n",
"- 13 cards: 635,013,559,600 hands \n",
@@ -28,11 +28,13 @@
"- 6 cards: 4,096 abstract hands\n",
"- 13 cards: 67,108,864 abstract hands\n",
"\n",
- "That's a big improvement. \n",
+ "That's a big improvement! \n",
+ "\n",
+ "Now let's start coding. There are two red suits and two black suits, so I'll represent the four suits as `'rbRB'`. (I also considered using `'♠️♥️♦️♣️'`.) I'll represent an abstract hand as a string of suits: `'rrBrbr'` is a 6-card hand. \n",
"\n",
"# Deals: Hands and their Probabilities\n",
"\n",
- "There are two red suits and two black suits, so I'll represent the four suits with the characters `'rbRB'`. (I also considered using `'♠️♥️♦️♣️'`.) I'll represent an abstract hand as a string of suits: `'rrBrbr'` is a 6-card hand. I'll define `deals(N)` to return a dict of all possible abstract hands of length `N`, each mapped to the probability of the hand. \n",
+ "I'll define `deals(N)` to return a dict of all possible abstract hands of length `N`, each mapped to the probability of the hand. \n",
"\n",
"With actual hands, every hand has the same probability, because every card is equally likely to be the next card dealt. But with abstract hands, the probability of the next suit depends on how many cards of that suit have already been dealt. If I've already dealt the 12 cards `'rrrrrrrrrrrr'`, then the probability of the next card being an `'r'` is 1/40, and the probability of it being a `'b'` is 13/40. So as I build up the abstract hands, I'll need to keep track of the number of remaining cards of each suit.\n",
"\n",
@@ -109,7 +111,7 @@
"\n",
"# More Abstraction: Collapsed Runs\n",
"\n",
- "Now for a second abstraction: the idea that an abstract hand can be *collapsed* by replacing a run of cards of the same suit with a single card, so that `'BBBBBrrrrBBBB'` collapses to `'BrB'`."
+ "Now for a second abstraction: an abstract hand can be *collapsed* by replacing a run of cards of the same suit with a single card, so that `'BBBBBrrrrBBBB'` collapses to `'BrB'`."
]
},
{
@@ -156,7 +158,7 @@
"\n",
"# Properly Ordered Hands\n",
"\n",
- "A hand is considered properly `ordered` if *\"the cards of a given suit are grouped together and, if possible, such that no suited groups of the same color are adjacent.\"* I was initially confused about the meaning of *\"if possible\";* Matt Ginsberg confirmed it means *\"if it is possible to separate the colors in any number of moves\"*, and thus that the hand `'BBBbbb'` is properly ordered, because it is not possible to separate the two black suits, while `'BBBbbR'` is not properly ordered, because the red card could have been inserted between the two black runs.\n",
+ "A hand is considered properly `ordered` if *\"the cards of a given suit are grouped together and, if possible, such that no suited groups of the same color are adjacent.\"* I was initially confused about the meaning of *\"if possible\";* Matt Ginsberg confirmed it means *\"if it is possible to separate the colors in any number of moves\"*, and thus that the hand `'BBBbbb'` is properly ordered, because it is not possible to separate the two black suits, while `'BBBbbR'` is not properly ordered, because the red card could be inserted between the two black runs.\n",
"\n",
"A hand is properly ordered if and only if its collapsed sequence is properly ordered, and a sequence is properly ordered if each suit appears only once, and either all the colors are the same, or suits of the same color don't appear adjacent to each other."
]
@@ -233,7 +235,7 @@
"source": [
"# Moving Cards to Make a Hand Ordered\n",
"\n",
- "I won't try to be clever; I'm content to use brute force. I'll say that a collapsed sequence is `orderable` if any of the possible `moves` of a block of cards makes the hand `ordered`. I'll find all possible `moves`, by finding all possible `splits` of the cards into a middle block of cards flanked by (possibly empty) left and right sequences; then all possible `inserts` of the block back into the rest of the cards. I'll define `orderable_probability(N)` to give the probability that a random `N`-card hand is orderable.\n",
+ "I won't try to be clever here; the abstractions got us down to a small enough number of abstract hands that I can use brute force from here on. I'll say that a collapsed sequence is `orderable` if any of the possible `moves` of a block of cards makes the hand `ordered`. I'll find all possible `moves`, by finding all possible `splits` of the cards into a middle block of cards flanked by (possibly empty) left and right sequences; then all possible `inserts` of the block back into the rest of the cards. I'll define `orderable_probability(N)` to give the probability that a random `N`-card hand is orderable.\n",
"Since many hands will collapse to the same sequence, I'll throw a `lru_cache` onto `orderable` so that it won't have to repeat computations."
]
},
@@ -277,7 +279,7 @@
"source": [
"# First Answer\n",
"\n",
- "Here's our answer for 6 cards:"
+ "Here's the answer for 6 cards:"
]
},
{
@@ -303,7 +305,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "And an easier-to-read answer for everthing up to `N` = 6 cards:"
+ "And an easier-to-read answer for everything up to 6 cards:"
]
},
{
@@ -361,18 +363,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "# Getting to `N` = 13\n",
+ "# Getting to `N` = 13: Less Brute Force\n",
"\n",
- "So far so good, but if we want to get to 13-card hands, we would have to handle 413 = 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",
+ "So far so good, but if we want to get to 13-card hands, we would have to handle 413 = 67,108,864 `deals`, which would take a long time. But after playing with sequences for a while, I discovered two key properties that can speed things up:\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 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",
+ "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 at most 3: 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 match the surrounding suits). 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.** To show that, take an unordered sequence, and see what happens if you take the extra suit and insert it anywhere in the sequence. If the sequence was unordered because it repeats a suit, adding a suit can't fix that. If it was unordered because suits of the same color are adjacent, then adding a suit of the other color *could* fix that: `'bBR'` could be fixed by inserting a `'r'` to get `'brBR'`. But here's the catch: `'bBR'` is not unorderable. And if we are going to insert a new suit between two others, that means that the original sequence must have had at most three suits (because when we add one, we can't get more than four suits in an ordered sequence), and *every* three-suit sequence is orderable. And if that argument doesn't convince you, below I collect all the unorderable sequences up to length 7 (which we know is the longest length we need to look at) and show that no matter what suit you add, the result is unorderable.\n",
- "\n",
- "\n",
- "\n"
+ "2. **Adding a suit to the end of an unorderable sequence can't make it orderable.** To show that, take an unordered sequence, and see what happens if you take the extra suit and insert it anywhere in the sequence. If the sequence was unordered because it repeats a suit, adding a suit can't fix that. If it was unordered because suits of the same color are adjacent, then adding a suit of the other color *could* fix that: `'bBR'` could be fixed by inserting a `'r'` to get `'brBR'`. But here's the catch: `'bBR'` is not unorderable. And if we are going to insert a new suit between two others, that means that the original sequence must have had at most three suits (because when we add one, we can't get more than four suits in an ordered sequence), and *every* three-suit sequence is orderable. And if that argument doesn't convince you, below I look at all the sequences from `deals(7)` (which we know is the longest length we need to look at) and show that if a sequence is not orderable, then no matter what suit you add to it, the result is not orderable:"
]
},
{
@@ -383,19 +382,17 @@
},
"outputs": [],
"source": [
- "seqs = {collapse(hand) for N in range(1, 8) for hand in deals(N)}\n",
- "unorderable = {seq for seq in seqs if not orderable(seq)}\n",
- "\n",
- "for seq in unorderable:\n",
- " for suit in suits:\n",
- " assert not orderable(seq + suit)"
+ "for seq in {collapse(hand) for hand in deals(7)}:\n",
+ " if not orderable(seq):\n",
+ " for suit in suits:\n",
+ " assert not orderable(seq + suit)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "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 in there is now orderable."
+ "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 they're all orderable now."
]
},
{
@@ -461,8 +458,8 @@
"11: 1.9% = 22673450197/1219690678500\n",
"12: 0.7% = 1751664923/238130084850\n",
"13: 0.3% = 30785713171/11112737293000\n",
- "CPU times: user 16.8 s, sys: 402 ms, total: 17.2 s\n",
- "Wall time: 19.5 s\n"
+ "CPU times: user 16 s, sys: 343 ms, total: 16.3 s\n",
+ "Wall time: 18.2 s\n"
]
}
],
@@ -474,7 +471,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "# Cache Sizes\n",
+ "# Caches\n",
"\n",
"Let's look at the cache for `orderable(seq)`:"
]
@@ -507,11 +504,11 @@
"source": [
"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 also save a lot of space in the `deals(N)` caches. Instead of storing all 413 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 413 hands for `deals(13)`, the `report` above says that just 0.3% of the hands are orderable, so we reduced the cache size by a factor of 300.\n",
"\n",
"# Unit Tests\n",
"\n",
- "To gain confidence in these answers, here are some unit tests. Before declaring my answers definitively correct, I would want a lot more tests, and some independent code reviews."
+ "To gain confidence in this project, here are some unit tests. Before declaring my answers definitively correct, I would want a lot more tests, and some independent code reviews."
]
},
{
@@ -557,7 +554,7 @@
" 'BB....', '.BB...', '..BB..', '...BB.', '....BB']\n",
" assert splits('123') == [('', '1', '23'), ('', '12', '3'), ('', '123', ''),\n",
" ('1', '2', '3'), ('1', '23', ''), ('12', '3', '')]\n",
- " assert orderable('bBr') # move 'r' after 'b'\n",
+ " assert orderable('bBr') # move 'r' between 'bB'\n",
" assert orderable('bBrbRBr') # move 'bRB' after first 'b' to get 'bbRBBrr'\n",
" assert orderable('bBrbRBrb') is False\n",
" return True\n",