Stagger, stack, sum
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
Inspired by this Stack Overflow question.
The challenge
Input
An array of square matrices containing non-negative integers.
Output
A square matrix built from the input matrices as follows.
Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.
For clarity, consider the following example input matrices ($N=2$, $P=3$):
3 5
4 10
6 8
12 11
2 0
9 1
- Start with the first input matrix.
- Shift the second input matrix NâÂÂ1 steps down and NâÂÂ1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.
Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with
0
to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is3 5 0
4 16 8
0 12 11For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1The ouput is the $((NâÂÂ1)P+1)times((NâÂÂ1)P+1)$ matrix obtained after including the last input matrix.
Additional rules and clarifications
$N$ and $P$ are positive integers.- You can optionally take $N$ and $P$ as additional inputs.
- Input and output can be taken by any reasonable means. Their format is flexible as usual.
- Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
- Shortest code in bytes wins.
Test cases:
In each case, input matrices are shown first, then the output.
$N=2$, $P=3$:
3 5
4 10
6 8
12 11
2 0
9 1
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1$N=2$, $P=1$:
3 5
4 10
3 5
4 10$N=1$, $P=4$:
4
7
23
5
39$N=3$, $P=2$:
11 11 8
6 8 12
11 0 4
4 1 13
9 19 11
13 4 2
11 11 8 0 0
6 8 12 0 0
11 0 8 1 13
0 0 9 19 11
0 0 13 4 2$N=2$, $P=4$:
14 13
10 0
13 20
21 3
9 22
0 8
17 3
19 16
14 13 0 0 0
10 13 20 0 0
0 21 12 22 0
0 0 0 25 3
0 0 0 19 16
code-golf number array-manipulation integer
add a comment |Â
up vote
7
down vote
favorite
Inspired by this Stack Overflow question.
The challenge
Input
An array of square matrices containing non-negative integers.
Output
A square matrix built from the input matrices as follows.
Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.
For clarity, consider the following example input matrices ($N=2$, $P=3$):
3 5
4 10
6 8
12 11
2 0
9 1
- Start with the first input matrix.
- Shift the second input matrix NâÂÂ1 steps down and NâÂÂ1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.
Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with
0
to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is3 5 0
4 16 8
0 12 11For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1The ouput is the $((NâÂÂ1)P+1)times((NâÂÂ1)P+1)$ matrix obtained after including the last input matrix.
Additional rules and clarifications
$N$ and $P$ are positive integers.- You can optionally take $N$ and $P$ as additional inputs.
- Input and output can be taken by any reasonable means. Their format is flexible as usual.
- Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
- Shortest code in bytes wins.
Test cases:
In each case, input matrices are shown first, then the output.
$N=2$, $P=3$:
3 5
4 10
6 8
12 11
2 0
9 1
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1$N=2$, $P=1$:
3 5
4 10
3 5
4 10$N=1$, $P=4$:
4
7
23
5
39$N=3$, $P=2$:
11 11 8
6 8 12
11 0 4
4 1 13
9 19 11
13 4 2
11 11 8 0 0
6 8 12 0 0
11 0 8 1 13
0 0 9 19 11
0 0 13 4 2$N=2$, $P=4$:
14 13
10 0
13 20
21 3
9 22
0 8
17 3
19 16
14 13 0 0 0
10 13 20 0 0
0 21 12 22 0
0 0 0 25 3
0 0 0 19 16
code-golf number array-manipulation integer
How long is your MATL solution for this?
â Giuseppe
1 hour ago
@Arnauld Thanks! Corrected
â Luis Mendo
30 mins ago
@Giuseppe I havenâÂÂt tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
â Luis Mendo
28 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Inspired by this Stack Overflow question.
The challenge
Input
An array of square matrices containing non-negative integers.
Output
A square matrix built from the input matrices as follows.
Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.
For clarity, consider the following example input matrices ($N=2$, $P=3$):
3 5
4 10
6 8
12 11
2 0
9 1
- Start with the first input matrix.
- Shift the second input matrix NâÂÂ1 steps down and NâÂÂ1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.
Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with
0
to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is3 5 0
4 16 8
0 12 11For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1The ouput is the $((NâÂÂ1)P+1)times((NâÂÂ1)P+1)$ matrix obtained after including the last input matrix.
Additional rules and clarifications
$N$ and $P$ are positive integers.- You can optionally take $N$ and $P$ as additional inputs.
- Input and output can be taken by any reasonable means. Their format is flexible as usual.
- Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
- Shortest code in bytes wins.
Test cases:
In each case, input matrices are shown first, then the output.
$N=2$, $P=3$:
3 5
4 10
6 8
12 11
2 0
9 1
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1$N=2$, $P=1$:
3 5
4 10
3 5
4 10$N=1$, $P=4$:
4
7
23
5
39$N=3$, $P=2$:
11 11 8
6 8 12
11 0 4
4 1 13
9 19 11
13 4 2
11 11 8 0 0
6 8 12 0 0
11 0 8 1 13
0 0 9 19 11
0 0 13 4 2$N=2$, $P=4$:
14 13
10 0
13 20
21 3
9 22
0 8
17 3
19 16
14 13 0 0 0
10 13 20 0 0
0 21 12 22 0
0 0 0 25 3
0 0 0 19 16
code-golf number array-manipulation integer
Inspired by this Stack Overflow question.
The challenge
Input
An array of square matrices containing non-negative integers.
Output
A square matrix built from the input matrices as follows.
Let $N times N$ be the size of each input matrix, and $P$ the number of input matrices.
For clarity, consider the following example input matrices ($N=2$, $P=3$):
3 5
4 10
6 8
12 11
2 0
9 1
- Start with the first input matrix.
- Shift the second input matrix NâÂÂ1 steps down and NâÂÂ1 steps right, so that its upper-left entry coincides with the lower-right entry of the previous one.
Imagine the second, shifted matrix as if it were stacked on top of the first. Sum the two values at the coincident entry. Write the other values, and fill the remaining entries with
0
to get a $(2N-1)times(2N-1)$ matrix. With the example input, the result so far is3 5 0
4 16 8
0 12 11For each remaining input matrix, stagger it so that its upper-left coincides with the lower-right of the accumulated result matrix so far. In the example, including the third input matrix gives
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1The ouput is the $((NâÂÂ1)P+1)times((NâÂÂ1)P+1)$ matrix obtained after including the last input matrix.
Additional rules and clarifications
$N$ and $P$ are positive integers.- You can optionally take $N$ and $P$ as additional inputs.
- Input and output can be taken by any reasonable means. Their format is flexible as usual.
- Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
- Shortest code in bytes wins.
Test cases:
In each case, input matrices are shown first, then the output.
$N=2$, $P=3$:
3 5
4 10
6 8
12 11
2 0
9 1
3 5 0 0
4 16 8 0
0 12 13 0
0 0 9 1$N=2$, $P=1$:
3 5
4 10
3 5
4 10$N=1$, $P=4$:
4
7
23
5
39$N=3$, $P=2$:
11 11 8
6 8 12
11 0 4
4 1 13
9 19 11
13 4 2
11 11 8 0 0
6 8 12 0 0
11 0 8 1 13
0 0 9 19 11
0 0 13 4 2$N=2$, $P=4$:
14 13
10 0
13 20
21 3
9 22
0 8
17 3
19 16
14 13 0 0 0
10 13 20 0 0
0 21 12 22 0
0 0 0 25 3
0 0 0 19 16
code-golf number array-manipulation integer
code-golf number array-manipulation integer
edited 30 mins ago
asked 1 hour ago
Luis Mendo
72.9k885284
72.9k885284
How long is your MATL solution for this?
â Giuseppe
1 hour ago
@Arnauld Thanks! Corrected
â Luis Mendo
30 mins ago
@Giuseppe I havenâÂÂt tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
â Luis Mendo
28 mins ago
add a comment |Â
How long is your MATL solution for this?
â Giuseppe
1 hour ago
@Arnauld Thanks! Corrected
â Luis Mendo
30 mins ago
@Giuseppe I havenâÂÂt tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
â Luis Mendo
28 mins ago
How long is your MATL solution for this?
â Giuseppe
1 hour ago
How long is your MATL solution for this?
â Giuseppe
1 hour ago
@Arnauld Thanks! Corrected
â Luis Mendo
30 mins ago
@Arnauld Thanks! Corrected
â Luis Mendo
30 mins ago
@Giuseppe I havenâÂÂt tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
â Luis Mendo
28 mins ago
@Giuseppe I havenâÂÂt tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
â Luis Mendo
28 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
R, 88 81 bytes
function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o
Try it online!
Takes a list
of matrices, A
, N
, and P
.
Builds the requisite matrix of zeros o
and adds elementwise the contents of A
to the appropriate submatrices in o
.
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 104 bytes
Takes input as (n,p,a)
.
(n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))
Try it online!
How?
Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:
$$w=(n-1)times(p-1)+n$$
For each cell at $(x,y)$, we compute:
$$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$
where undefined cells are replaced with zeros.
add a comment |Â
up vote
0
down vote
Python 2, 124 bytes
def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a
Try it online!
add a comment |Â
up vote
0
down vote
Jelly, 20 bytes
õ;â¬0Zðâº0á¹Â+á¹ÂU$}á¹ÂU+â¸õ/
Try it online!
Bah, Jelly has an attitude today...
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
R, 88 81 bytes
function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o
Try it online!
Takes a list
of matrices, A
, N
, and P
.
Builds the requisite matrix of zeros o
and adds elementwise the contents of A
to the appropriate submatrices in o
.
add a comment |Â
up vote
2
down vote
R, 88 81 bytes
function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o
Try it online!
Takes a list
of matrices, A
, N
, and P
.
Builds the requisite matrix of zeros o
and adds elementwise the contents of A
to the appropriate submatrices in o
.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
R, 88 81 bytes
function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o
Try it online!
Takes a list
of matrices, A
, N
, and P
.
Builds the requisite matrix of zeros o
and adds elementwise the contents of A
to the appropriate submatrices in o
.
R, 88 81 bytes
function(A,N,P,o=0*diag(P*(N-1)+1))for(i in 1:P)o[x,x]=o[x<-1:N+i-1,x]+A[[i]];o
Try it online!
Takes a list
of matrices, A
, N
, and P
.
Builds the requisite matrix of zeros o
and adds elementwise the contents of A
to the appropriate submatrices in o
.
edited 1 min ago
answered 33 mins ago
Giuseppe
15.1k31051
15.1k31051
add a comment |Â
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 104 bytes
Takes input as (n,p,a)
.
(n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))
Try it online!
How?
Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:
$$w=(n-1)times(p-1)+n$$
For each cell at $(x,y)$, we compute:
$$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$
where undefined cells are replaced with zeros.
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 104 bytes
Takes input as (n,p,a)
.
(n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))
Try it online!
How?
Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:
$$w=(n-1)times(p-1)+n$$
For each cell at $(x,y)$, we compute:
$$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$
where undefined cells are replaced with zeros.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 104 bytes
Takes input as (n,p,a)
.
(n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))
Try it online!
How?
Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:
$$w=(n-1)times(p-1)+n$$
For each cell at $(x,y)$, we compute:
$$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$
where undefined cells are replaced with zeros.
JavaScript (ES6), 104 bytes
Takes input as (n,p,a)
.
(n,p,a)=>[...Array(n--+n*~-p)].map((_,y,r)=>r.map((_,x)=>a.map((a,i)=>s+=(a[y-i*n]||0)[x-i*n]|0,s=0)|s))
Try it online!
How?
Redimensioning matrices filled with a default constant ($0$ in that case) is neither very easy nor very short in JS, so we just build a square matrix with the correct width $w$ right away:
$$w=(n-1)times(p-1)+n$$
For each cell at $(x,y)$, we compute:
$$s_x,y=sum_i=0^p-1a_i(x-itimes (n-1),y-itimes (n-1))$$
where undefined cells are replaced with zeros.
edited 16 mins ago
answered 42 mins ago
Arnauld
65.9k583278
65.9k583278
add a comment |Â
add a comment |Â
up vote
0
down vote
Python 2, 124 bytes
def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a
Try it online!
add a comment |Â
up vote
0
down vote
Python 2, 124 bytes
def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2, 124 bytes
def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a
Try it online!
Python 2, 124 bytes
def f(m,N,P):w=~-N*P+1;a=[w*[0]for _ in' '*w];g=0;exec"x=g/N/N*~-N;a[x+g/N%N][x+g%N]+=m[x][g/N%N][g%N];g+=1;"*P*N*N;return a
Try it online!
answered 12 mins ago
ovs
17.5k21058
17.5k21058
add a comment |Â
add a comment |Â
up vote
0
down vote
Jelly, 20 bytes
õ;â¬0Zðâº0á¹Â+á¹ÂU$}á¹ÂU+â¸õ/
Try it online!
Bah, Jelly has an attitude today...
add a comment |Â
up vote
0
down vote
Jelly, 20 bytes
õ;â¬0Zðâº0á¹Â+á¹ÂU$}á¹ÂU+â¸õ/
Try it online!
Bah, Jelly has an attitude today...
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Jelly, 20 bytes
õ;â¬0Zðâº0á¹Â+á¹ÂU$}á¹ÂU+â¸õ/
Try it online!
Bah, Jelly has an attitude today...
Jelly, 20 bytes
õ;â¬0Zðâº0á¹Â+á¹ÂU$}á¹ÂU+â¸õ/
Try it online!
Bah, Jelly has an attitude today...
answered 1 min ago
Erik the Outgolfer
29.6k42899
29.6k42899
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173445%2fstagger-stack-sum%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
How long is your MATL solution for this?
â Giuseppe
1 hour ago
@Arnauld Thanks! Corrected
â Luis Mendo
30 mins ago
@Giuseppe I havenâÂÂt tried it in MATL. For the test cases I used the MATLAB code from my answer in the linked question
â Luis Mendo
28 mins ago