Re: Enumerating all 3-tuples

2018-03-21 Thread Robin Becker
On 21/03/2018 05:14, Steven D'Aprano wrote: On Tue, 13 Mar 2018 23:56:37 +0100, Denis Kasak wrote: [...] The triples can be viewed as a pair of a pair and a natural number: (1,1),1 (1,1),2 (1,1),3 ... (2,1),1 (2,1),2 (2,1),3 ... (1,2),1 (1,2),2 (1,2),3 ... [...] This leads fairly naturally

Re: Enumerating all 3-tuples

2018-03-20 Thread Steven D'Aprano
On Tue, 13 Mar 2018 23:56:37 +0100, Denis Kasak wrote: [...] > The triples can be viewed as a pair of a pair and a natural number: > > (1,1),1 (1,1),2 (1,1),3 ... > (2,1),1 (2,1),2 (2,1),3 ... > (1,2),1 (1,2),2 (1,2),3 ... [...] > This leads fairly naturally to the implementation. > >

Re: Enumerating all 3-tuples

2018-03-15 Thread Denis Kasak
On 2018-03-13 23:56, Denis Kasak wrote: On 2018-03-10 02:13, Steven D'Aprano wrote: But I've stared at this for an hour and I can't see how to extend the result to three coordinates. I can lay out a grid in the order I want: 1,1,1 1,1,2 1,1,3 1,1,4 ... 2,1,1 2,1,2 2,1,3 2,1,4

Re: Enumerating all 3-tuples (Posting On Python-List Prohibited)

2018-03-15 Thread Ben Bacarisse
Lawrence D’Oliveiro writes: > On Thursday, March 15, 2018 at 2:56:24 PM UTC+13, Ben Bacarisse wrote: >> >> Lawrence D’Oliveiro writes: >> >>> On Wednesday, March 14, 2018 at 2:18:24 PM UTC+13, Ben Bacarisse wrote: >>> Lawrence D’Oliveiro writes: The

Re: Enumerating all 3-tuples (Posting On Python-List Prohibited)

2018-03-14 Thread Ben Bacarisse
Lawrence D’Oliveiro writes: > On Wednesday, March 14, 2018 at 2:18:24 PM UTC+13, Ben Bacarisse wrote: >> Lawrence D’Oliveiro writes: >> >> The original problem -- triples of natural numbers -- is >> not particularly hard, but the general problem -- enumerating n-tuples

Re: Enumerating all 3-tuples

2018-03-14 Thread Denis Kasak
On 2018-03-10 02:13, Steven D'Aprano wrote: But I've stared at this for an hour and I can't see how to extend the result to three coordinates. I can lay out a grid in the order I want: 1,1,1 1,1,2 1,1,3 1,1,4 ... 2,1,1 2,1,2 2,1,3 2,1,4 ... 1,2,1 1,2,2 1,2,3 1,2,4 ...

Re: Enumerating all 3-tuples (Posting On Python-List Prohibited)

2018-03-13 Thread Ben Bacarisse
Lawrence D’Oliveiro writes: > On Tuesday, March 13, 2018 at 1:58:48 PM UTC+13, Ben Bacarisse wrote: >> Of course you can always generate n-tuples of N and then map these to >> n-tuples of the intended sequence but that seems inelegant. > > This whole discussion seems to

Re: Enumerating all 3-tuples

2018-03-13 Thread Robin Becker
On 13/03/2018 11:14, Steven D'Aprano wrote: On Mon, 12 Mar 2018 13:17:15 +, Robin Becker wrote: It's possible to generalize the cantor pairing function to triples, but that may not give you what you want. Effectively you can generate an arbitrary number of triples using an iterative

Re: Enumerating all 3-tuples

2018-03-13 Thread Antoon Pardon
On 10-03-18 02:13, Steven D'Aprano wrote: > I am trying to enumerate all the three-tuples (x, y, z) where each of x, > y, z can range from 1 to ∞ (infinity). > > This is clearly unhelpful: > > for x in itertools.count(1): > for y in itertools.count(1): > for z in itertools.count(1): >

Re: Enumerating all 3-tuples

2018-03-13 Thread Paul Moore
On 13 March 2018 at 11:01, Steven D'Aprano wrote: > On Sat, 10 Mar 2018 11:15:49 +, Paul Moore wrote: > >> On 10 March 2018 at 02:18, MRAB wrote: > [...] >>> This might help, although the order they come out might not be what

Re: Enumerating all 3-tuples

2018-03-13 Thread Steven D'Aprano
On Mon, 12 Mar 2018 13:17:15 +, Robin Becker wrote: > It's possible to generalize the cantor pairing function to triples, but > that may not give you what you want. Effectively you can generate an > arbitrary number of triples using an iterative method. My sample code > looked like this > >

Re: Enumerating all 3-tuples (Posting On Python-List Prohibited)

2018-03-13 Thread Steven D'Aprano
On Sun, 11 Mar 2018 01:40:01 +, Ben Bacarisse wrote: > I'm sure deep recursion is not needed, it's just tricky translating from > a lazy language when one is not familiar with all the iterator > facilities in Python. For example, I couldn't find an append operation > that returns an

Re: Enumerating all 3-tuples

2018-03-13 Thread Steven D'Aprano
On Sat, 10 Mar 2018 11:15:49 +, Paul Moore wrote: > On 10 March 2018 at 02:18, MRAB wrote: [...] >> This might help, although the order they come out might not be what you >> want: >> >> def triples(): >> for total in itertools.count(1): >> for i in

Re: Enumerating all 3-tuples

2018-03-13 Thread Robin Becker
On 12/03/2018 18:05, Chris Angelico wrote: On Tue, Mar 13, 2018 at 2:54 AM, Robin Becker wrote: On 12/03/2018 13:17, Robin Becker wrote: An alternative approach gives more orderly sequences using a variable base number construction 4 (0, 0, 1) 9 (0, 0, 1) 18 (0, 0, 2) 32

Re: Enumerating all 3-tuples (Posting On Python-List Prohibited)

2018-03-12 Thread Ben Bacarisse
Lawrence D’Oliveiro writes: > On Sunday, March 11, 2018 at 2:40:16 PM UTC+13, Ben Bacarisse wrote: >> It would be nice to avoid relying on any value-based ordering. > > I don’t see why. The set of elements has to have the same cardinality > as the set of natural numbers,

Re: Enumerating all 3-tuples

2018-03-12 Thread Chris Angelico
On Tue, Mar 13, 2018 at 5:42 AM, Skip Montanaro wrote: 4 (0, 0, 1) 9 (0, 0, 1) 18 (0, 0, 2) 32 (0, 0, 2) >> >> I spy duplicates. > > I didn't realize we'd started playing "I Spy." > We're actually playing Calvinball, but don't tell the others.

Re: Enumerating all 3-tuples

2018-03-12 Thread Skip Montanaro
>>> 4 (0, 0, 1) >>> 9 (0, 0, 1) >>> 18 (0, 0, 2) >>> 32 (0, 0, 2) > > I spy duplicates. I didn't realize we'd started playing "I Spy ." ​ ​ Skip -- https://mail.python.org/mailman/listinfo/python-list

Re: Enumerating all 3-tuples

2018-03-12 Thread Chris Angelico
On Tue, Mar 13, 2018 at 2:54 AM, Robin Becker wrote: > On 12/03/2018 13:17, Robin Becker wrote: > An alternative approach gives more orderly sequences using a variable base > number construction > >> 4 (0, 0, 1) >> 9 (0, 0, 1) >> 18 (0, 0, 2) >> 32 (0, 0, 2) I spy

Re: Enumerating all 3-tuples

2018-03-12 Thread Robin Becker
On 12/03/2018 13:17, Robin Becker wrote: It's possible to generalize the cantor pairing function to triples, but that may not give you what you want. Effectively you can generate an arbitrary number of triples using an iterative method. My sample code looked like this ct mapping of

Re: Enumerating all 3-tuples

2018-03-12 Thread Elliott Roper
On 10 Mar 2018, Paul Moore wrote (in article): > On 10 March 2018 at 02:18, MRAB wrote: > > On 2018-03-10 01:13, Steven D'Aprano wrote: > > > > > > I am trying to enumerate all the three-tuples (x, y, z) where each of

Re: Enumerating all 3-tuples

2018-03-12 Thread Robin Becker
It's possible to generalize the cantor pairing function to triples, but that may not give you what you want. Effectively you can generate an arbitrary number of triples using an iterative method. My sample code looked like this import math def cantor_pair(k1,k2): return

Re: Enumerating all 3-tuples (Posting On Python-List Prohibited)

2018-03-10 Thread Ben Bacarisse
Lawrence D’Oliveiro writes: > On Sunday, March 11, 2018 at 3:24:05 AM UTC+13, Ben Bacarisse wrote: >> Unfortunately my Python is not up to using iterators well enough to >> avoid a "maximum recursion depth exceeded" error. > > My solution only needed recursion up to a

Re: Enumerating all 3-tuples

2018-03-10 Thread Ben Bacarisse
bartc writes: > On 10/03/2018 20:06, Ben Bacarisse wrote: >>> On 10/03/2018 14:23, Ben Bacarisse wrote: Off topic: I knocked up this Haskell version as a proof-of-concept: import Data.List pn n l = pn' n (map (:[]) l) where pn'

Re: Enumerating all 3-tuples

2018-03-10 Thread bartc
On 10/03/2018 20:06, Ben Bacarisse wrote: bartc writes: [repost as original seems to have gone to email; my newsreader has somehow acquired a 'Reply' button where 'Followup' normally goes.] [I thought it was intended but my reply bounced.] On 10/03/2018 14:23, Ben

Re: Enumerating all 3-tuples

2018-03-10 Thread Ben Bacarisse
bartc writes: > [repost as original seems to have gone to email; my newsreader has > somehow acquired a 'Reply' button where 'Followup' normally goes.] [I thought it was intended but my reply bounced.] > On 10/03/2018 14:23, Ben Bacarisse wrote: >> Ben Bacarisse

Re: Enumerating all 3-tuples

2018-03-10 Thread bartc
[repost as original seems to have gone to email; my newsreader has somehow acquired a 'Reply' button where 'Followup' normally goes.] On 10/03/2018 14:23, Ben Bacarisse wrote: Ben Bacarisse writes: Off topic: I knocked up this Haskell version as a proof-of-concept:

Re: Enumerating all 3-tuples

2018-03-10 Thread Ben Bacarisse
Sorry for following up to myself again... Ben Bacarisse writes: >> ... But I think that is an easier way (no code yet though!) unless >> you are set on one particular enumeration: consider the triple as a pair >> one element of which runs over the enumeration of pairs you

Re: Enumerating all 3-tuples

2018-03-10 Thread Ben Bacarisse
Ben Bacarisse writes: > Steven D'Aprano writes: > >> I am trying to enumerate all the three-tuples (x, y, z) where each of x, >> y, z can range from 1 to ∞ (infinity). > ... But I think that is an easier way (no code yet though!)

Re: Enumerating all 3-tuples

2018-03-10 Thread Paul Moore
On 10 March 2018 at 02:18, MRAB wrote: > On 2018-03-10 01:13, Steven D'Aprano wrote: >> >> I am trying to enumerate all the three-tuples (x, y, z) where each of x, >> y, z can range from 1 to ∞ (infinity). >> >> This is clearly unhelpful: >> >> for x in

Re: Enumerating all 3-tuples

2018-03-09 Thread Ben Bacarisse
Steven D'Aprano writes: > I am trying to enumerate all the three-tuples (x, y, z) where each of x, > y, z can range from 1 to ∞ (infinity). > > This is clearly unhelpful: > > for x in itertools.count(1): > for y in itertools.count(1): > for z in

Re: Enumerating all 3-tuples

2018-03-09 Thread MRAB
On 2018-03-10 01:13, Steven D'Aprano wrote: I am trying to enumerate all the three-tuples (x, y, z) where each of x, y, z can range from 1 to ∞ (infinity). This is clearly unhelpful: for x in itertools.count(1): for y in itertools.count(1): for z in itertools.count(1):

Re: Enumerating all 3-tuples

2018-03-09 Thread Chris Angelico
On Sat, Mar 10, 2018 at 12:13 PM, Steven D'Aprano 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 ->

Re: Enumerating all 3-tuples

2018-03-09 Thread bartc
On 10/03/2018 01:13, Steven D'Aprano wrote: I am trying to enumerate all the three-tuples (x, y, z) where each of x, y, z can range from 1 to ∞ (infinity). This is clearly unhelpful: for x in itertools.count(1): for y in itertools.count(1): for z in itertools.count(1):

Enumerating all 3-tuples

2018-03-09 Thread Steven D'Aprano
I am trying to enumerate all the three-tuples (x, y, z) where each of x, y, z can range from 1 to ∞ (infinity). This is clearly unhelpful: for x in itertools.count(1): for y in itertools.count(1): for z in itertools.count(1): print(x, y, z) as it never advances beyond