3 deleted 1305 characters in body
source | link

Not tested. See the version below.


Python 3, 179 bytes

from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):
  a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];
  {sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)

(some newlines/spaces added to avoid readers having to scroll the code)

Try it online!


A direct port of my Jelly answer (with some modifications, see "note" section below)

[user202729@archlinux golf]$ printf '%s' "from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}==^{0,*s}andor-print(a)" > a.py
[user202729@archlinux golf]$ wc -c a.py 
179177 a.py
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 23, 24, 25], 10)' 2>/dev/null
[1, 4, 1, 1, 1, 1, 1, 7, 7, 1]
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)
TypeError: bad operand type for unary -: 'NoneType'

real    0m3.090s125s
user    0m3.077s119s
sys     0m0.007s004s
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31], 15)' 2>/dev/null
[3, 1, 2, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 2, 1]
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)
TypeError: bad operand type for unary -: 'NoneType'

real    11m3011m36.697s093s
user    11m3011m33.130s941s
sys     0m0.291s387s
[user202729@archlinux golf]$ 

Not tested. See the version below.


Python 3, 179 bytes

from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):
  a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];
  {sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)

(some newlines/spaces added to avoid readers having to scroll the code)

Try it online!


A direct port of my Jelly answer (with some modifications, see "note" section below)

[user202729@archlinux golf]$ printf '%s' "from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)" > a.py
[user202729@archlinux golf]$ wc -c a.py 
179 a.py
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 23, 24, 25], 10)'
[1, 4, 1, 1, 1, 1, 1, 7, 7, 1]
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)
TypeError: bad operand type for unary -: 'NoneType'

real    0m3.090s
user    0m3.077s
sys     0m0.007s
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31], 15)'
[3, 1, 2, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 2, 1]
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)
TypeError: bad operand type for unary -: 'NoneType'

real    11m30.697s
user    11m30.130s
sys     0m0.291s
[user202729@archlinux golf]$ 

(some newlines/spaces added to avoid readers having to scroll the code)

A direct port of my Jelly answer (with some modifications, see "note" section below)

[user202729@archlinux golf]$ printf '%s' "from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}^{0,*s}or-print(a)" > a.py
[user202729@archlinux golf]$ wc -c a.py
177 a.py
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 23, 24, 25], 10)' 2>/dev/null
[1, 4, 1, 1, 1, 1, 1, 7, 7, 1]

real    0m3.125s
user    0m3.119s
sys     0m0.004s
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31], 15)' 2>/dev/null
[3, 1, 2, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 2, 1]

real    11m36.093s
user    11m33.941s
sys     0m0.387s
[user202729@archlinux golf]$ 
2 deleted 36 characters in body
source | link

Python 3, 177 bytes

from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):
  a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];
  {sum(a[p//n:p%n+1])for p in range(n*n)}^{0,*s}or-print(a)

Try it online!

Not tested. See the version below.


Python 3, 179 bytes

Try it online!Try it online!

 

Python 3, 179 bytes

Try it online!

Python 3, 177 bytes

from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):
  a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];
  {sum(a[p//n:p%n+1])for p in range(n*n)}^{0,*s}or-print(a)

Try it online!

Not tested. See the version below.


Python 3, 179 bytes

Try it online!

 
1
source | link

Python 3, 179 bytes

from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):
  a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];
  {sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)

(some newlines/spaces added to avoid readers having to scroll the code)

Try it online!

A direct port of my Jelly answer (with some modifications, see "note" section below)

Local run result:

[user202729@archlinux golf]$ printf '%s' "from itertools import*
s,n=eval(input())
for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)" > a.py
[user202729@archlinux golf]$ wc -c a.py 
179 a.py
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 23, 24, 25], 10)'
[1, 4, 1, 1, 1, 1, 1, 7, 7, 1]
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)
TypeError: bad operand type for unary -: 'NoneType'

real    0m3.090s
user    0m3.077s
sys     0m0.007s
[user202729@archlinux golf]$ time python a.py<<<'([1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31], 15)'
[3, 1, 2, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 2, 1]
Traceback (most recent call last):
  File "a.py", line 3, in <module>
    for[*t]in combinations(s[:-2],n-2):a=[*map(int.__sub__,t+s[-2:],[0,*t,s[-2]])];{sum(a[p//n:p%n+1])for p in range(n*n)}=={0,*s}and-print(a)
TypeError: bad operand type for unary -: 'NoneType'

real    11m30.697s
user    11m30.130s
sys     0m0.291s
[user202729@archlinux golf]$ 

I heard that itertools is verbose, but my best combinations implementation is even more verbose:

c=lambda s,n,p:s and c(s[1:],n-1,p+s[:1])+c(s[1:],n,p)or[]if n else[p]

Note.

  • Using division/modulo in a[p//n:p%n+1] takes about 2x longer, but saves some bytes.
  • This is a bit different from the Jelly answer -- the Jelly answer iterates backwards.
  • Thanks to combinations returning an iterator, this is more memory-friendly.