Trim that distracting background off!
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...
Task & Rules
Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.
- It is allowed to take the width and the height of the matrix as input as well.
- The input is guaranteed to contain at least one $1$.
- You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.
Example
$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$
Test cases
Input | Output
[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)
[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)
[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)
[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)
code-golf array-manipulation matrix binary-matrix
add a comment |Â
up vote
3
down vote
favorite
Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...
Task & Rules
Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.
- It is allowed to take the width and the height of the matrix as input as well.
- The input is guaranteed to contain at least one $1$.
- You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.
Example
$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$
Test cases
Input | Output
[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)
[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)
[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)
[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)
code-golf array-manipulation matrix binary-matrix
1
This feels very familiar; was it in the Sandbox for a while?
â Shaggy
29 mins ago
2
Possible duplicate of Remove surrounding zeroes of a 2d array.
â Neil
27 mins ago
A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
â Shaggy
13 mins ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...
Task & Rules
Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.
- It is allowed to take the width and the height of the matrix as input as well.
- The input is guaranteed to contain at least one $1$.
- You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.
Example
$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$
Test cases
Input | Output
[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)
[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)
[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)
[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)
code-golf array-manipulation matrix binary-matrix
Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...
Task & Rules
Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.
- It is allowed to take the width and the height of the matrix as input as well.
- The input is guaranteed to contain at least one $1$.
- You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.
Example
$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$
Test cases
Input | Output
[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)
[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)
[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)
[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)
[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)
code-golf array-manipulation matrix binary-matrix
code-golf array-manipulation matrix binary-matrix
edited 55 mins ago
asked 1 hour ago
Mr. Xcoder
31k758195
31k758195
1
This feels very familiar; was it in the Sandbox for a while?
â Shaggy
29 mins ago
2
Possible duplicate of Remove surrounding zeroes of a 2d array.
â Neil
27 mins ago
A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
â Shaggy
13 mins ago
add a comment |Â
1
This feels very familiar; was it in the Sandbox for a while?
â Shaggy
29 mins ago
2
Possible duplicate of Remove surrounding zeroes of a 2d array.
â Neil
27 mins ago
A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
â Shaggy
13 mins ago
1
1
This feels very familiar; was it in the Sandbox for a while?
â Shaggy
29 mins ago
This feels very familiar; was it in the Sandbox for a while?
â Shaggy
29 mins ago
2
2
Possible duplicate of Remove surrounding zeroes of a 2d array.
â Neil
27 mins ago
Possible duplicate of Remove surrounding zeroes of a 2d array.
â Neil
27 mins ago
A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
â Shaggy
13 mins ago
A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
â Shaggy
13 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
Octave, 57 56 bytes
Here find
does the heavy lifting: It finds
the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.
Thanks @beaker and @AndrasDeak for -1 byte!
@(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2
Try it online!
add a comment |Â
up vote
0
down vote
Retina 0.8.2, 83 bytes
+`^0+ö|ö0+$
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
(ö?)*0*(.*1)0*
$#1 $.2
Try it online! Explanation:
+`^0+ö|ö0+$
Delete leading and trailing zero rows.
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
Remove all 0
s on the lines above the last. Remove all the 1
s two, but change the digit underneath on the next line to a 1
in that case. This bitwise or's the rows together.
(ö?)*0*(.*1)0*
$#1 $.2
Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.
add a comment |Â
up vote
0
down vote
Japt, 16 bytes
[UUy]
î=ðd)ÃÂaZÃÂÃÂ
Try it
Explanation
[ ] :Create an array containing
U : The input and
Uy : The input transposed
n :Reassign to U
î :Map each Z
ð : Indices of elements where
d : Any element is truthy (not zero)
= ) : Reassign to Z
ÃÂ : Last element
a : Absolute difference with
ZÃÂ : First element
ÃÂ : Minus 1
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Octave, 57 56 bytes
Here find
does the heavy lifting: It finds
the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.
Thanks @beaker and @AndrasDeak for -1 byte!
@(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2
Try it online!
add a comment |Â
up vote
2
down vote
Octave, 57 56 bytes
Here find
does the heavy lifting: It finds
the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.
Thanks @beaker and @AndrasDeak for -1 byte!
@(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Octave, 57 56 bytes
Here find
does the heavy lifting: It finds
the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.
Thanks @beaker and @AndrasDeak for -1 byte!
@(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2
Try it online!
Octave, 57 56 bytes
Here find
does the heavy lifting: It finds
the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.
Thanks @beaker and @AndrasDeak for -1 byte!
@(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2
Try it online!
edited 50 mins ago
answered 1 hour ago
flawr
25.8k562178
25.8k562178
add a comment |Â
add a comment |Â
up vote
0
down vote
Retina 0.8.2, 83 bytes
+`^0+ö|ö0+$
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
(ö?)*0*(.*1)0*
$#1 $.2
Try it online! Explanation:
+`^0+ö|ö0+$
Delete leading and trailing zero rows.
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
Remove all 0
s on the lines above the last. Remove all the 1
s two, but change the digit underneath on the next line to a 1
in that case. This bitwise or's the rows together.
(ö?)*0*(.*1)0*
$#1 $.2
Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.
add a comment |Â
up vote
0
down vote
Retina 0.8.2, 83 bytes
+`^0+ö|ö0+$
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
(ö?)*0*(.*1)0*
$#1 $.2
Try it online! Explanation:
+`^0+ö|ö0+$
Delete leading and trailing zero rows.
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
Remove all 0
s on the lines above the last. Remove all the 1
s two, but change the digit underneath on the next line to a 1
in that case. This bitwise or's the rows together.
(ö?)*0*(.*1)0*
$#1 $.2
Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Retina 0.8.2, 83 bytes
+`^0+ö|ö0+$
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
(ö?)*0*(.*1)0*
$#1 $.2
Try it online! Explanation:
+`^0+ö|ö0+$
Delete leading and trailing zero rows.
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
Remove all 0
s on the lines above the last. Remove all the 1
s two, but change the digit underneath on the next line to a 1
in that case. This bitwise or's the rows together.
(ö?)*0*(.*1)0*
$#1 $.2
Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.
Retina 0.8.2, 83 bytes
+`^0+ö|ö0+$
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
(ö?)*0*(.*1)0*
$#1 $.2
Try it online! Explanation:
+`^0+ö|ö0+$
Delete leading and trailing zero rows.
+1`((.)*).(ö(?<-2>.)*).(?<=(1)ö.*|(.))
$1$3$4$5
Remove all 0
s on the lines above the last. Remove all the 1
s two, but change the digit underneath on the next line to a 1
in that case. This bitwise or's the rows together.
(ö?)*0*(.*1)0*
$#1 $.2
Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.
answered 12 mins ago
Neil
77k744173
77k744173
add a comment |Â
add a comment |Â
up vote
0
down vote
Japt, 16 bytes
[UUy]
î=ðd)ÃÂaZÃÂÃÂ
Try it
Explanation
[ ] :Create an array containing
U : The input and
Uy : The input transposed
n :Reassign to U
î :Map each Z
ð : Indices of elements where
d : Any element is truthy (not zero)
= ) : Reassign to Z
ÃÂ : Last element
a : Absolute difference with
ZÃÂ : First element
ÃÂ : Minus 1
add a comment |Â
up vote
0
down vote
Japt, 16 bytes
[UUy]
î=ðd)ÃÂaZÃÂÃÂ
Try it
Explanation
[ ] :Create an array containing
U : The input and
Uy : The input transposed
n :Reassign to U
î :Map each Z
ð : Indices of elements where
d : Any element is truthy (not zero)
= ) : Reassign to Z
ÃÂ : Last element
a : Absolute difference with
ZÃÂ : First element
ÃÂ : Minus 1
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Japt, 16 bytes
[UUy]
î=ðd)ÃÂaZÃÂÃÂ
Try it
Explanation
[ ] :Create an array containing
U : The input and
Uy : The input transposed
n :Reassign to U
î :Map each Z
ð : Indices of elements where
d : Any element is truthy (not zero)
= ) : Reassign to Z
ÃÂ : Last element
a : Absolute difference with
ZÃÂ : First element
ÃÂ : Minus 1
Japt, 16 bytes
[UUy]
î=ðd)ÃÂaZÃÂÃÂ
Try it
Explanation
[ ] :Create an array containing
U : The input and
Uy : The input transposed
n :Reassign to U
î :Map each Z
ð : Indices of elements where
d : Any element is truthy (not zero)
= ) : Reassign to Z
ÃÂ : Last element
a : Absolute difference with
ZÃÂ : First element
ÃÂ : Minus 1
edited 45 secs ago
answered 16 mins ago
Shaggy
17.4k21663
17.4k21663
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%2f174832%2ftrim-that-distracting-background-off%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
1
This feels very familiar; was it in the Sandbox for a while?
â Shaggy
29 mins ago
2
Possible duplicate of Remove surrounding zeroes of a 2d array.
â Neil
27 mins ago
A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
â Shaggy
13 mins ago