Enumerating all 3-tuples

Chris Angelico rosuav at gmail.com
Fri Mar 9 21:14:19 EST 2018


On Sat, Mar 10, 2018 at 12:13 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> The Russian mathematician Cantor came up with a *pairing function* that
> encodes a pair of integers into a single one. For example, he maps the
> coordinate pairs to integers as follows:
>
> 1,1  ->  1
> 2,1  ->  2
> 1,2  ->  3
> 3,1  ->  4
> 2,2  ->  5
>
> and so forth. He does this by writing out the coordinates in a grid:
>
> 1,1  1,2  1,3  1,4  ...
> 2,1  2,2  2,3  2,4  ...
> 3,1  3,2  3,3  3,4  ...
> 4,1  4,2  4,3  4,4  ...
> ...
>
> and then iterating over them along the diagonals, starting from the top
> corner.

The diagonals all have a constant sum. You catch the item with a sum
of 2, and then the two with a sum of 3, then the three with a sum of
4, etc. Can that pattern be extended to three dimensions?

(1, 1, 1)
(1, 1, 2) (1, 2, 1) (2, 1, 1)
(1, 1, 3) (1, 2, 2) (1, 3, 1) (2, 1, 2) (2, 2, 1) (3, 1, 1)
etc

Not sure if that helps or not.

ChrisA



More information about the Python-list mailing list