35
\$\begingroup\$

Take a matrix of positive integers as input, and make it explode!


The way you explode a matrix is by simply adding zeros around every element, including the outside borders.

Input/output formats are optional as always!

Test cases:

1
-----
0 0 0
0 1 0
0 0 0
--------------

1 4
5 2
-----
0 0 0 0 0
0 1 0 4 0
0 0 0 0 0
0 5 0 2 0
0 0 0 0 0
--------------

1 4 7
-----
0 0 0 0 0 0 0
0 1 0 4 0 7 0
0 0 0 0 0 0 0
--------------

6
4
2
-----
0 0 0
0 6 0
0 0 0
0 4 0
0 0 0
0 2 0
0 0 0
\$\endgroup\$

32 Answers 32

1
2
0
\$\begingroup\$

Python 2, 92 bytes

def n(a):
	l,g='0 ',len(a[0])*2+1;print l*g
	for i in a:print l+' 0 '.join(i)+' 0';print l*g

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 11 8 bytes

2Fε€0Ć}ø

Try it online or verify all test cases.

Explanation:

2F      # Loop 2 times:
  ε     #  Map each row of the matrix to:
        #  (which uses the (implicit) input-matrix in the first iteration)
   €0   #   Add a leading 0 before each item in the list
     Ć  #   Enclose the list, appending its own first item (to add a trailing 0 as well)
  }ø    #  After the map: zip/transpose; swapping rows/column
        # (after the loop, the result is output implicitly)
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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