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 non-negative integers to triplets.

An alternative approach gives more orderly sequences using a variable base 
number construction

class Tupilator(object):
        def __init__(self,degree=3):
                self.i = 0
                self.n = 2
                self.degree = degree

        def next(self):
                x = self.i
                v = []
                a =v.append
                n = self.n
                if not x: a(0)
                while x>0:
                        x, d = divmod(x,n)
                        a(d)
                if sum(v)==self.degree*(n-1):
                        self.n += 1
                pad = self.degree - len(v)
                if pad>0:
                        v += pad*[0]
                self.i += 1
                return tuple(v)

if __name__=='__main__':
        t = Tupilator()
        for z in xrange(100):
                print z, t.next()

0 (0, 0, 0)
1 (1, 0, 0)
2 (0, 1, 0)
3 (1, 1, 0)
4 (0, 0, 1)
5 (1, 0, 1)
6 (0, 1, 1)
7 (1, 1, 1)
8 (2, 2, 0)
9 (0, 0, 1)
10 (1, 0, 1)
11 (2, 0, 1)
12 (0, 1, 1)
13 (1, 1, 1)
14 (2, 1, 1)
15 (0, 2, 1)
16 (1, 2, 1)
17 (2, 2, 1)
18 (0, 0, 2)
19 (1, 0, 2)
20 (2, 0, 2)
21 (0, 1, 2)
22 (1, 1, 2)
23 (2, 1, 2)
24 (0, 2, 2)
25 (1, 2, 2)
26 (2, 2, 2)
27 (3, 2, 1)
28 (0, 3, 1)
29 (1, 3, 1)
30 (2, 3, 1)
31 (3, 3, 1)
32 (0, 0, 2)
33 (1, 0, 2)
34 (2, 0, 2)
35 (3, 0, 2)
36 (0, 1, 2)
37 (1, 1, 2)
38 (2, 1, 2)
39 (3, 1, 2)
40 (0, 2, 2)
........
80 (0, 1, 3)
81 (1, 1, 3)
82 (2, 1, 3)
83 (3, 1, 3)
84 (4, 1, 3)
85 (0, 2, 3)
86 (1, 2, 3)
87 (2, 2, 3)
88 (3, 2, 3)
89 (4, 2, 3)
90 (0, 3, 3)
91 (1, 3, 3)
92 (2, 3, 3)
93 (3, 3, 3)
94 (4, 3, 3)
95 (0, 4, 3)
96 (1, 4, 3)
97 (2, 4, 3)
98 (3, 4, 3)
99 (4, 4, 3)




--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to