mirror of
https://github.com/jbranchaud/til
synced 2026-07-06 01:00:35 +00:00
Add Generate Permutations Of All Valid 9-ball Racks as a Math TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||||
|
|
||||||
_1807 TILs and counting..._
|
_1808 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
|
|
||||||
@@ -60,6 +60,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
* [Linux](#linux)
|
* [Linux](#linux)
|
||||||
* [LLM](#llm)
|
* [LLM](#llm)
|
||||||
* [Mac](#mac)
|
* [Mac](#mac)
|
||||||
|
* [Math](#math)
|
||||||
* [Mise](#mise)
|
* [Mise](#mise)
|
||||||
* [MongoDB](#mongodb)
|
* [MongoDB](#mongodb)
|
||||||
* [MySQL](#mysql)
|
* [MySQL](#mysql)
|
||||||
@@ -771,6 +772,10 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [View All Windows Of The Current App](mac/view-all-windows-of-the-current-app.md)
|
- [View All Windows Of The Current App](mac/view-all-windows-of-the-current-app.md)
|
||||||
- [Write System Clipboard To A File](mac/write-system-clipboard-to-a-file.md)
|
- [Write System Clipboard To A File](mac/write-system-clipboard-to-a-file.md)
|
||||||
|
|
||||||
|
### Math
|
||||||
|
|
||||||
|
- [Generate Permutations Of All Valid 9-ball Racks](math/generate-permutations-of-all-valid-9-ball-racks.md)
|
||||||
|
|
||||||
### Mise
|
### Mise
|
||||||
|
|
||||||
- [Create Umbrella Task For All Test Tasks](mise/create-umbrella-task-for-all-test-tasks.md)
|
- [Create Umbrella Task For All Test Tasks](mise/create-umbrella-task-for-all-test-tasks.md)
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
# Generate Permutations Of All Valid 9-ball Racks
|
||||||
|
|
||||||
|
I wanted to produce a full listing of all valid rack arrangements for the game
|
||||||
|
of [9-ball](https://en.wikipedia.org/wiki/Nine-ball). The constraints on how a
|
||||||
|
9-ball rack can be arranged are, first, that the 1 ball must be placed at the
|
||||||
|
head of the diamond and, second, that the 9 ball must be placed at the center of
|
||||||
|
the diamond. After that, all other balls (2 through 8) can be placed in any
|
||||||
|
arrangement.
|
||||||
|
|
||||||
|
Because each of those seven remaining balls can be arranged in distinct
|
||||||
|
orderings where each ball is placed once, this is a
|
||||||
|
[_permutation_](https://en.wikipedia.org/wiki/Permutation) problem.
|
||||||
|
|
||||||
|
> In elementary combinatorics, the k-permutations, or partial permutations, are
|
||||||
|
> the ordered arrangements of k distinct elements selected from a set. When k is
|
||||||
|
> equal to the size of the set, these are the permutations in the previous
|
||||||
|
> sense.
|
||||||
|
|
||||||
|
For this problem, the seven distinct elements can be arranged into `7!` (seven
|
||||||
|
factorial) unique permutations. That is, 5040 permutations.
|
||||||
|
|
||||||
|
I can use [Ruby's `Array#permutations`
|
||||||
|
method](https://docs.ruby-lang.org/en/4.0/Array.html#method-i-permutation) to
|
||||||
|
enumerate these 5040 permutations like so:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
[2,3,4,5,6,7,8].permutation.map do |perm|
|
||||||
|
[1, *perm[0..2], 9, *perm[3..7]]
|
||||||
|
end.to_a
|
||||||
|
=> [[1, 2, 3, 4, 9, 5, 6, 7, 8],
|
||||||
|
[1, 2, 3, 4, 9, 5, 6, 8, 7],
|
||||||
|
[1, 2, 3, 4, 9, 5, 7, 6, 8],
|
||||||
|
[1, 2, 3, 4, 9, 5, 7, 8, 6],
|
||||||
|
[1, 2, 3, 4, 9, 5, 8, 6, 7],
|
||||||
|
[1, 2, 3, 4, 9, 5, 8, 7, 6],
|
||||||
|
[1, 2, 3, 4, 9, 6, 5, 7, 8],
|
||||||
|
...
|
||||||
|
[1, 8, 7, 6, 9, 5, 3, 2, 4],
|
||||||
|
[1, 8, 7, 6, 9, 5, 3, 4, 2],
|
||||||
|
[1, 8, 7, 6, 9, 5, 4, 2, 3],
|
||||||
|
[1, 8, 7, 6, 9, 5, 4, 3, 2]]
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user