Expand an Array

Expand an array to all directions by duplicating the outer elements

You will be given two inputs: an array and the expansion degree
For example if I give you [[1,2,3][4,5,6][7,8,9]] and 3, you must output:

1 1 1 1 2 3 3 3 3
1 1 1 1 2 3 3 3 3
1 1 1 1 2 3 3 3 3
1 1 1 1 2 3 3 3 3
4 4 4 4 5 6 6 6 6
7 7 7 7 8 9 9 9 9
7 7 7 7 8 9 9 9 9
7 7 7 7 8 9 9 9 9
7 7 7 7 8 9 9 9 9


As you can see the given array is in the center and all the outer elements have expanded:
1 -> 3 times up, 3 times left and also to every position of the upper left corner
2-> 3 times up
5-> is not an outer element
etc

here are some more test cases to make things clearer

Input   [[4,5,6]],0

Output
4 5 6

Input  [[1]],2

Output

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

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

Output
1 1 1 2 3 3 3
1 1 1 2 3 3 3
1 1 1 2 3 3 3
1 1 1 2 3 3 3
1 1 1 2 3 3 3

Input  [[1,2,3][11,22,33][111,222,333][1111,2222,3333]],1

Output
1       1       2       3       3
1       1       2       3       3
11      11      22      33      33
111     111     222     333     333
1111    1111    2222    3333    3333
1111    1111    2222    3333    3333


You can use as many whitespaces as you like.
But the output has to be a mXn array just like it is shown here.
You cannot output a list of lists.

This is Shortest answer on bytes wins

• Is output formatted like this valid? (output is in input) Oct 21, 2017 at 15:56
• @H.PWiz Yes,it is.
– user72269
Oct 21, 2017 at 15:58
• "You cannot output a list of lists." seems like an unnecessary output restriction to me. Oct 21, 2017 at 16:19
• Probably so, because the stringize part of my answer takes 41 chars, while the "actual" part only takes 19 chars. Oct 21, 2017 at 16:23
• Test cases are not an acceptable alternative to a clear specification.
– user45941
Oct 23, 2017 at 12:26

Octave, 35 bytes

@(a,x)padarray(a,[x x],'replicate')


Try it online!

• Is this a builtin in Octave? Oct 21, 2017 at 16:21
• @EriktheOutgolfer It belongs to the image processing package. Oct 21, 2017 at 16:24

Matlab, 27 bytes

@(a,n)padarray(a,[n,n],'r')


The key over the Octave solution is that Matlab accepts partial option strings when they are unambiguous.

MATL, 10 bytes

th2Y5I4$Ya  Port of @rahnema1's Octave answer. Try it online! Explanation t % Implicit input: number, n. Duplicate h % Concatenate. Gives an array [n,n] 2Y5 % Push 'replicate' (predefined literal). Will be used as an input flag I % Push 3. Will be used as equivalent to an input flag 4$     % Specify that the next function will use 4 inputs
Ya     % Call 'padarray' with 4 inputs. This takes the array as implicit
% input. Number 3 as third input is interpreted as the input flag
% 'both'. So the function pads the array (first input) by n in each
% dimension (second input) by replicating the border (third input) in
% both directions (fourth input). Implicit display

• Explanation please? Oct 21, 2017 at 18:15
• @EriktheOutgolfer Added Oct 21, 2017 at 18:39

Jelly, 13 bytes

;ṫ¥0$¡Ṛ$⁺Zµ⁺G


Try it online!

• I will NEVER learn how to use Jelly... Oct 21, 2017 at 16:45

Python 2, 138 113 bytes

-9 bytes thanks to @JonathanFrech
-16 bytes thanks to @Lynn

a,n=input()
for e in a[:1]*n+a+n*a[-1:]:print' '.join('%*s'%(len(max(sum(a,[]))),n)for n in e[:1]*n+e+n*e[-1:])


Try it online!

• 129 bytes; using string formatting to pad the output. Oct 21, 2017 at 22:53
• – Lynn
Oct 22, 2017 at 2:02

05AB1E, 15 bytes

2F2F¤¸².×«R}ø}»


Try it online!

Yes, this output format is valid.

05AB1E, 3328 23 bytes

F2Fø2FR¤¸«]øεD€gZs-ú}ø»


Try it online! 15 10 bytes of actual code, the rest is formatting the output. Explanation:

       ¤                Copy last element
¸               Turn it into a list
«              Concatenate
2F                  Repeat twice
R                 Reverse list each time
2F                     Repeat twice
ø                    Transpose list each time
F                       Repeat the given number of times
]             Close all the loops
ø            Transpose the array
D          Duplicate
€g        Take length of each number
Z       Get the maximum
s-     Subtract each length from the maximum
ú    Pad with that number of spaces
ε       }   For each row (column originally)
ø  Transpose back
» Join with spaces and newlines


Formatting costs 2 bytes if I copy @ErikTheOutgolfer's output format for 12 bytes:

F2Fø2FR¤¸«]»


Python 2.7, 74 characters

Pretty straightforward: uses numpy padding and regex to force a return string without brackets. This code returns an anonymous lambda.

import numpy,re;lambda a,n:re.sub('[]', '',str(numpy.pad(a,n,'edge')))


Python 2, 948986 82 bytes

a,n=input()
for l in a[:1]*n+a+n*a[-1:]:print' '.join(map(str,l[:1]*n+l+n*l[-1:]))


Try it online!