Skip to main content
deleted 5985 characters in body
Source Link
pxeger
  • 25.3k
  • 28
  • 36

Don't touch the walls!

Given a multi-dimensional rectangular array of non-negative integers, pad it with the minimal number of zeroes so that only zeroes are "touching the edges" of the array, in every dimension.

The output must remain rectangular, and have the same number of dimensions as the input.

Example

For the input:

[[1, 3, 0], [2, 6, 7], [4, 0, 2], [0, 0, 0]] 

Here is the array again with the elements which are "touching the edge" highlighted:

[[1, 3, 0], [2, 6, 7], [4, 0, 2], [0, 0, 0]] 

The expected output is:

[[0, 0, 0, 0, 0], [0, 1, 3, 0, 0], [0, 2, 6, 7, 0], [0, 4, 0, 2, 0], [0, 0, 0, 0, 0]] 

Notice that:

  • In the first row, even though there was already a zero on the right, we still had to add an extra zero on the right to keep the array properly rectangular
  • The bottom row was already all zeroes, so no extra row needed to be added there

For the 1-dimensional array [3, 4, 9, 0], the edges are [3, 4, 9, 0].


For this 3-dimensional array:

[[[4, 0, 4, 1], [0, 0, 5, 8], [6, 0, 0, 0], [0, 3, 7, 0]], [[4, 9, 8, 5], [0, 6, 4, 0], [0, 0, 0, 3], [0, 9, 6, 1]], [[0, 5, 3, 5], [0, 0, 0, 0], [6, 5, 3, 0], [4, 6, 0, 8]]] 

the edges are:

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

Imagine each inner 2-dimensional array as a cross-section of a 3-dimensional box. The only elements which are not touching the edges are the ones on the inside of the box.

Specification

"Touching the edges" is a hopefully fairly intuitive concept, but here is a specification:

Let's use the same example input array as above; its shape is 4, 3.

Using one-based indexing, we can index any element of the array using two integers in the ranges \$ [1, 4] \$ and \$ [1, 3] \$.

The elements which are touching the edge are those with coordinates of the form 1, y, 4, y, x, 1, or x, 3.

So, formally, we can say, for an element to be "touching the edge", at least one part of its multidimensional indices is either 1 (the minimum possible coordinate), or the maximum possible coordinate (which is the size of the array in the respective dimension).

Rules

Test cases

InputOutput
[[[0]]]
[[[0]]]
[[1]]
[[0, 0, 0],
[0, 1, 0],
[0, 0, 0]]
[3, 4, 9, 0]
[0, 3, 4, 9, 0]
[[1, 3, 0],
[2, 6, 7],
[4, 0, 2],
[0, 0, 0]]
[[0, 0, 0, 0, 0],
[0, 1, 3, 0, 0],
[0, 2, 6, 7, 0],
[0, 4, 0, 2, 0],
[0, 0, 0, 0, 0]]
[[1, 0],
[0, 4]]
[[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 0]]
[[[[0, 0],
[1, 3]],

[[0, 6],
[0, 5]]],


[[[0, 0],
[0, 0]],

[[1, 0],
[2, 0]]]]
[[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]],


[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 3, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 6, 0],
[0, 0, 5, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]],


[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]],


[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]]]

MetaDon't touch the walls!

  • Is this a duplicate?
  • Is this clear enough?
  • Should I restrict this to just two dimensions? I think that would reduce the complexity significantly, but maybe by too much?

Don't touch the walls!

Given a multi-dimensional rectangular array of non-negative integers, pad it with the minimal number of zeroes so that only zeroes are "touching the edges" of the array, in every dimension.

The output must remain rectangular, and have the same number of dimensions as the input.

Example

For the input:

[[1, 3, 0], [2, 6, 7], [4, 0, 2], [0, 0, 0]] 

Here is the array again with the elements which are "touching the edge" highlighted:

[[1, 3, 0], [2, 6, 7], [4, 0, 2], [0, 0, 0]] 

The expected output is:

[[0, 0, 0, 0, 0], [0, 1, 3, 0, 0], [0, 2, 6, 7, 0], [0, 4, 0, 2, 0], [0, 0, 0, 0, 0]] 

Notice that:

  • In the first row, even though there was already a zero on the right, we still had to add an extra zero on the right to keep the array properly rectangular
  • The bottom row was already all zeroes, so no extra row needed to be added there

For the 1-dimensional array [3, 4, 9, 0], the edges are [3, 4, 9, 0].


For this 3-dimensional array:

[[[4, 0, 4, 1], [0, 0, 5, 8], [6, 0, 0, 0], [0, 3, 7, 0]], [[4, 9, 8, 5], [0, 6, 4, 0], [0, 0, 0, 3], [0, 9, 6, 1]], [[0, 5, 3, 5], [0, 0, 0, 0], [6, 5, 3, 0], [4, 6, 0, 8]]] 

the edges are:

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

Imagine each inner 2-dimensional array as a cross-section of a 3-dimensional box. The only elements which are not touching the edges are the ones on the inside of the box.

Specification

"Touching the edges" is a hopefully fairly intuitive concept, but here is a specification:

Let's use the same example input array as above; its shape is 4, 3.

Using one-based indexing, we can index any element of the array using two integers in the ranges \$ [1, 4] \$ and \$ [1, 3] \$.

The elements which are touching the edge are those with coordinates of the form 1, y, 4, y, x, 1, or x, 3.

So, formally, we can say, for an element to be "touching the edge", at least one part of its multidimensional indices is either 1 (the minimum possible coordinate), or the maximum possible coordinate (which is the size of the array in the respective dimension).

Rules

Test cases

InputOutput
[[[0]]]
[[[0]]]
[[1]]
[[0, 0, 0],
[0, 1, 0],
[0, 0, 0]]
[3, 4, 9, 0]
[0, 3, 4, 9, 0]
[[1, 3, 0],
[2, 6, 7],
[4, 0, 2],
[0, 0, 0]]
[[0, 0, 0, 0, 0],
[0, 1, 3, 0, 0],
[0, 2, 6, 7, 0],
[0, 4, 0, 2, 0],
[0, 0, 0, 0, 0]]
[[1, 0],
[0, 4]]
[[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 0]]
[[[[0, 0],
[1, 3]],

[[0, 6],
[0, 5]]],


[[[0, 0],
[0, 0]],

[[1, 0],
[2, 0]]]]
[[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]],


[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 3, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 6, 0],
[0, 0, 5, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]],


[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]],


[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]]]

Meta

  • Is this a duplicate?
  • Is this clear enough?
  • Should I restrict this to just two dimensions? I think that would reduce the complexity significantly, but maybe by too much?
deleted 20 characters in body
Source Link
pxeger
  • 25.3k
  • 28
  • 36

Given a multi-dimensional rectangular array of non-negative integers, pad it with the minimal number of zeroes so that no non-zero elements of the arrayonly zeroes are "touching the edges" of the array, in anyevery dimension.

Rules

Rules

Given a multi-dimensional rectangular array of non-negative integers, pad it with the minimal number of zeroes so that no non-zero elements of the array are "touching the edges" of the array, in any dimension.

Rules

Given a multi-dimensional rectangular array of non-negative integers, pad it with the minimal number of zeroes so that only zeroes are "touching the edges" of the array, in every dimension.

Rules

added 470 characters in body
Source Link
pxeger
  • 25.3k
  • 28
  • 36

Here is the array again with the elements which are "touching the edge" highlighted in boldhighlighted:

[[11, 33, 00], [22, 6, 77], [44, 0, 22], [00, 00, 00]] 

For the 1-dimensional array [3, 4, 9, 0], the edges are [33, 4, 9, 00].

[[[44, 00, 44, 11], [00, 00, 55, 88], [66, 00, 00, 00], [00, 33, 77, 00]], [[44, 99, 88, 55], [00, 6, 4, 00], [00, 0, 0, 33], [00, 99, 66, 11]], [[00, 44, 77, 99], [00, 2, 0, 66], [00, 0, 0, 00], [77, 00, 33, 00]], [[00, 55, 33, 55], [00, 00, 00, 00], [66, 55, 33, 00], [44, 66, 00, 88]]] 

Here is the array again with the elements which are "touching the edge" highlighted in bold:

[[1, 3, 0], [2, 6, 7], [4, 0, 2], [0, 0, 0]] 

For the 1-dimensional array [3, 4, 9, 0], the edges are [3, 4, 9, 0].

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

Here is the array again with the elements which are "touching the edge" highlighted:

[[1, 3, 0], [2, 6, 7], [4, 0, 2], [0, 0, 0]] 

For the 1-dimensional array [3, 4, 9, 0], the edges are [3, 4, 9, 0].

[[[4, 0, 4, 1], [0, 0, 5, 8], [6, 0, 0, 0], [0, 3, 7, 0]], [[4, 9, 8, 5], [0, 6, 4, 0], [0, 0, 0, 3], [0, 9, 6, 1]], [[0, 4, 7, 9], [0, 2, 0, 6], [0, 0, 0, 0], [7, 0, 3, 0]], [[0, 5, 3, 5], [0, 0, 0, 0], [6, 5, 3, 0], [4, 6, 0, 8]]] 
added 78 characters in body
Source Link
pxeger
  • 25.3k
  • 28
  • 36
Loading
added 78 characters in body
Source Link
pxeger
  • 25.3k
  • 28
  • 36
Loading
Source Link
pxeger
  • 25.3k
  • 28
  • 36
Loading