5
\$\begingroup\$

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

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

9 Answers 9

4
\$\begingroup\$

Octave, 35 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Is this a builtin in Octave? \$\endgroup\$ Oct 21, 2017 at 16:21
  • \$\begingroup\$ @EriktheOutgolfer It belongs to the image processing package. \$\endgroup\$
    – rahnema1
    Oct 21, 2017 at 16:24
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Explanation please? \$\endgroup\$ Oct 21, 2017 at 18:15
  • \$\begingroup\$ @EriktheOutgolfer Added \$\endgroup\$
    – Luis Mendo
    Oct 21, 2017 at 18:39
2
\$\begingroup\$

Jelly, 13 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I will NEVER learn how to use Jelly... \$\endgroup\$
    – RedClover
    Oct 21, 2017 at 16:45
1
\$\begingroup\$

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!

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

05AB1E, 15 bytes

2F2F¤¸².׫R}ø}»

Try it online!

Yes, this output format is valid.

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

05AB1E, 33 28 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¤¸«]»
\$\endgroup\$
0
\$\begingroup\$

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')))
\$\endgroup\$
0
\$\begingroup\$

Python 2, 94 89 86 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!

\$\endgroup\$

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.