41
\$\begingroup\$

This is a 2-dimensional version of this question.

Given a non-empty 2-dimensional array/matrix containing only non-negative integers:

$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & 0 & 1 & 0 \\ {\color{Red}0} & {\color{Red}0} & 0 & 0 & 1 \\ {\color{Red}0} & {\color{Red}0} & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} $$

Output the array with surrounding zeroes removed, i.e. the largest contiguous subarray without surrounding zeroes:

$$ \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix} $$

Examples:

$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & 0 & 1 & 0 \\ {\color{Red}0} & {\color{Red}0} & 0 & 0 & 1 \\ {\color{Red}0} & {\color{Red}0} & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \end{bmatrix} $$

Input:
[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 0, 0, 0, 0]]

Output:
[[0, 1, 0], [0, 0, 1], [1, 1, 1]]

$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 0 & 0 & 3 \\ {\color{Red}0} & 0 & 0 & 0 \\ {\color{Red}0} & 5 & 0 & 0 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 0 & 0 & 3 \\ 0 & 0 & 0 \\ 5 & 0 & 0 \end{bmatrix} $$

Input:
[[0, 0, 0, 0], [0, 0, 0, 3], [0, 0, 0, 0], [0, 5, 0, 0], [0, 0, 0, 0]]

Output:
[[0, 0, 3], [0, 0, 0], [5, 0, 0]]

$$ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \mapsto \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} $$

Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} \end{bmatrix} $$

Input:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Output:
[]

$$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \\ 1 & 1 & 1 & 1 \\ {\color{Red}0} & {\color{Red}0} & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 1 & 1 & 1 & 1 \end{bmatrix} $$

Input:
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]]

Output:
[[1, 1, 1, 1]]

$$ \begin{bmatrix} {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \\ {\color{Red}0} & 1 & {\color{Red}0} & {\color{Red}0} \end{bmatrix} \mapsto \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} $$

Input:
[[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]]

Output:
[[1], [1], [1]]

$$ \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix} \mapsto \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 1 & 1 \end{bmatrix} $$

Input:
[[1, 1, 1, 1], [1, 2, 3, 1], [1, 1, 1, 1]]

Output:
[[1, 1, 1, 1], [1, 2, 3, 1], [1, 1, 1, 1]]
\$\endgroup\$
8
  • 3
    \$\begingroup\$ @MattH Nothing is difficult in a non-esoteric language. :) Just difficult to make it short. \$\endgroup\$
    – DELETE_ME
    Jul 27, 2018 at 16:20
  • 1
    \$\begingroup\$ Can we give a falsey output instead of an empty matrix, for the last test case? \$\endgroup\$
    – Sundar R
    Jul 27, 2018 at 18:21
  • 1
    \$\begingroup\$ Also, if output can be a non-square matrix, please add a test case for that. \$\endgroup\$
    – Sundar R
    Jul 27, 2018 at 18:44
  • 1
    \$\begingroup\$ A test case that broke my earlier submission: [[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0]] (the result having a width/height of 1) \$\endgroup\$ Jul 27, 2018 at 20:24
  • 1
    \$\begingroup\$ Hey, is it possible to add the test case $$\begin{bmatrix}1&1&1&1\\1&2&3&1\\1&1&1&1\end{bmatrix}$$ \$\endgroup\$
    – Beta Decay
    Jul 28, 2018 at 10:35

33 Answers 33

1
2
1
\$\begingroup\$

05AB1E (legacy), 13 bytes

2Fζ2FRDv¬O_i¦

Try it online or verify all test cases.

Explanation:

2F                    # Loop two times:
  ζ                   #  Zip/transpose; swapping rows/columns
                      #  (takes the input-matrix implicitly in the first iteration)
   2F                 #  Inner loop two times:
     R                #  Reverse the rows
      Dv              #  Inner loop over the rows:
        ¬             #   Get the first row (without popping the matrix)
         O_i          #   If the row consists only of 0s:
            ¦         #    Remove this first row from the matrix
                      # (output the result implicitly)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The 2nd test case should output [[0, 0, 3], [0, 0, 0], [5, 0, 0]]. \$\endgroup\$
    – Shaggy
    Oct 28, 2018 at 21:58
  • \$\begingroup\$ @Shaggy Almost 5 months later, but it's fixed now (and 1 byte smaller in the process). xD \$\endgroup\$ Feb 12, 2019 at 14:08
1
\$\begingroup\$

Attache, 40 bytes

Fixpoint[{Flip!_[N[0=Sum!_@0]...#_]}//4]

Try it online! Same business as below, just a bit smarter, splitting the process into four steps instead of two.

Alternatives

47 bytes: Fixpoint[{Reverse=>Tr!_[N[0=Sum!_@0]...#_]}//4]

48 bytes: Fixpoint[{MatrixRotate!_[N[0=Sum!_@0]...#_]}//4]


Attache, 68 bytes

Fixpoint[{n.=Dim@_@-1Tr[{_@1!in~-n'0or Sum!_@0}\Enumerate@_<:0]}//2]

Try it online!

Twice: This removes any first or last row whose sum is 0, then transposes the array. Then, this process is repeated until the result does not change.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 100 bytes

f=(a,i=4,b=a[0])=>b&&i?/[1-9]/.test(b)?f([...b].map((c,j)=>a.map(d=>d.pop())),i-1):f(a.slice(1),i):a

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.