Skip to main content
made it a bijection
Source Link
alyx-brett
  • 1.6k
  • 8
  • 14

#Python, 257 264B broken265B

importfrom itertools as Iimport*
T=lambda i:[list(a)for l in range(i)for a in I.product(range(i-l+3),repeat=l+1)if a[-1]>0and sum(a)==i]==i-l]
def f(N):
 A,i=[[0]],0
 while len(A)<=N:A+=T(i);i+=1
 return A[N]
def g(t):
 A,i=[[0]],0
 while t not in A:A+=T(i);i+=1
 return A.index(t)

To list every sequence, for each integer i, for each length l<i, list every sequence whose sum is exactly i, of lenthlegnth at most i, ordered ascending, e.g; NB thanks to zeroes, this it no longer a bijection. Back to the drawing board...

0 :  01
1 :  12
2 :  20 1
3 :  0 23
4 :  10 12
5 :  31 1
6 :  0 30 1
7 :  1 24
8 :  20 13
9: : 0 01 32
10: 0: 1 2 1
11 :  0 20 12
12 : 1 0 21 1
13 : 1 1 0 1
14 : 2 0 0 0 1
15 : 4 5
16 :  0 4
17 :  1 3
18 :  2 2
19 :  3 1
20 :  0 0 43
21 :  0 1 2
22 :  0 2 1
23 :  1 0 2
24 :  1 1 1
25 :  2 0 1
26 :  0 0 0 2
27 :  0 0 1 1
28 :  0 1 0 1
29 :  1 0 0 1
30 :  0 0 0 0 1

so f(256)=[0, 0, 2, 1, 3]=[9], and g([4,2,0,3])=7121=2254. NB that this is SLOW for large numbers.

#Python, 257 264B broken

import itertools as I
T=lambda i:[list(a)for l in range(i)for a in I.product(range(i-l+3),repeat=l+1)if a[-1]>0and sum(a)==i]
def f(N):
 A,i=[[0]],0
 while len(A)<=N:A+=T(i);i+=1
 return A[N]
def g(t):
 A,i=[[0]],0
 while t not in A:A+=T(i);i+=1
 return A.index(t)

To list every sequence, for each i, list every sequence whose sum is exactly i, of lenth at most i, ordered ascending, e.g; NB thanks to zeroes, this it no longer a bijection. Back to the drawing board...

0:  0
1:  1
2:  2
3:  0 2
4:  1 1
5:  3
6:  0 3
7:  1 2
8:  2 1
9:  0 0 3
10: 0 1 2
11: 0 2 1
12: 1 0 2
13: 1 1 1
14: 2 0 1
15: 4
16: 0 4
17: 1 3
18: 2 2
19: 3 1
20: 0 0 4

so f(256)=[0, 0, 2, 1, 3], and g([4,2,0,3])=7121. NB that this is SLOW for large numbers.

#Python, 257 265B

from itertools import*
T=lambda i:[list(a)for l in range(i)for a in product(range(i-l+3),repeat=l+1)if a[-1]>0and sum(a)==i-l]
def f(N):
 A,i=[[0]],0
 while len(A)<=N:A+=T(i);i+=1
 return A[N]
def g(t):
 A,i=[[0]],0
 while t not in A:A+=T(i);i+=1
 return A.index(t)

To list every sequence, for each integer i, for each length l<i, list every sequence whose sum is exactly i, of legnth at most i, ordered ascending, e.g;

0 :  1
1 :  2
2 :  0 1
3 :  3
4 :  0 2
5 :  1 1
6 :  0 0 1
7 :  4
8 :  0 3
9 :  1 2
10 :  2 1
11 :  0 0 2
12 :  0 1 1
13 :  1 0 1
14 :  0 0 0 1
15 :  5
16 :  0 4
17 :  1 3
18 :  2 2
19 :  3 1
20 :  0 0 3
21 :  0 1 2
22 :  0 2 1
23 :  1 0 2
24 :  1 1 1
25 :  2 0 1
26 :  0 0 0 2
27 :  0 0 1 1
28 :  0 1 0 1
29 :  1 0 0 1
30 :  0 0 0 0 1

so f(256)=[9], and g([4,2,0,3])=2254. NB that this is SLOW for large numbers.

Reflect that N = Nu{0}
Source Link
alyx-brett
  • 1.6k
  • 8
  • 14

#Python, 257B257 264B broken

import itertools as I
T=lambda i:[map[list(str,a)for l in range(1,i+1i)for a in I.product(range(1,i-l+3),repeat=lrepeat=l+1)if a[-1]>0and sum(a)==i]
def f(N):
 A,i=[]i=[[0]],0
 while len(A)<N<=N:i+=1;A+=TA+=T(i);i+=1
 return A[N-1]A[N]
def g(t):
 A,i=[]i=[[0]],0
 while t not in A:i+=1;A+=TA+=T(i);i+=1
 return A.index(t)+1

To list every sequence, for each i, list every sequence whose sum is exactly i, of lenth at most i, ordered ascending, e.g; NB thanks to zeroes, this it no longer a bijection. Back to the drawing board...

0:  0
1:  1
2:  2
3:  1,0 12
4:  31 1
5:  1, 23
6:  2,0 13
7:  1, 1, 12
8:  42 1
9:  1,0 0 3
10: 2,0 1 2
11: 3,0 2 1
12: 1, 1,0 2
13: 1, 2,1 1
14: 2, 1,0 1
15: 1,4
16: 1,0 4
17: 1, 3
18: 2 2
19: 3 1
20: 0 0 4

so f(500256)=[2=[0, 10, 2, 1, 1, 1, 1]3], and g([4,62,2]0,3])=2092=7121. NB that this is SLOW for large numbers.

#Python, 257B

import itertools as I
T=lambda i:[map(str,a)for l in range(1,i+1)for a in I.product(range(1,i-l+3),repeat=l)if sum(a)==i]
def f(N):
 A,i=[],0
 while len(A)<N:i+=1;A+=T(i)
 return A[N-1]
def g(t):
 A,i=[],0
 while t not in A:i+=1;A+=T(i)
 return A.index(t)+1

To list every sequence, for each i, list every sequence whose sum is exactly i, ordered ascending, e.g;

1:  1
2:  2
3:  1, 1
4:  3
5:  1, 2
6:  2, 1
7:  1, 1, 1
8:  4
9:  1, 3
10: 2, 2
11: 3, 1
12: 1, 1, 2
13: 1, 2, 1
14: 2, 1, 1
15: 1, 1, 1, 1

so f(500)=[2, 1, 2, 1, 1, 1, 1], and g([4,6,2])=2092. NB that this is SLOW for large numbers.

#Python, 257 264B broken

import itertools as I
T=lambda i:[list(a)for l in range(i)for a in I.product(range(i-l+3),repeat=l+1)if a[-1]>0and sum(a)==i]
def f(N):
 A,i=[[0]],0
 while len(A)<=N:A+=T(i);i+=1
 return A[N]
def g(t):
 A,i=[[0]],0
 while t not in A:A+=T(i);i+=1
 return A.index(t)

To list every sequence, for each i, list every sequence whose sum is exactly i, of lenth at most i, ordered ascending, e.g; NB thanks to zeroes, this it no longer a bijection. Back to the drawing board...

0:  0
1:  1
2:  2
3:  0 2
4:  1 1
5:  3
6:  0 3
7:  1 2
8:  2 1
9:  0 0 3
10: 0 1 2
11: 0 2 1
12: 1 0 2
13: 1 1 1
14: 2 0 1
15: 4
16: 0 4
17: 1 3
18: 2 2
19: 3 1
20: 0 0 4

so f(256)=[0, 0, 2, 1, 3], and g([4,2,0,3])=7121. NB that this is SLOW for large numbers.

Source Link
alyx-brett
  • 1.6k
  • 8
  • 14

#Python, 257B

import itertools as I
T=lambda i:[map(str,a)for l in range(1,i+1)for a in I.product(range(1,i-l+3),repeat=l)if sum(a)==i]
def f(N):
 A,i=[],0
 while len(A)<N:i+=1;A+=T(i)
 return A[N-1]
def g(t):
 A,i=[],0
 while t not in A:i+=1;A+=T(i)
 return A.index(t)+1

To list every sequence, for each i, list every sequence whose sum is exactly i, ordered ascending, e.g;

1:  1
2:  2
3:  1, 1
4:  3
5:  1, 2
6:  2, 1
7:  1, 1, 1
8:  4
9:  1, 3
10: 2, 2
11: 3, 1
12: 1, 1, 2
13: 1, 2, 1
14: 2, 1, 1
15: 1, 1, 1, 1

so f(500)=[2, 1, 2, 1, 1, 1, 1], and g([4,6,2])=2092. NB that this is SLOW for large numbers.