# Print arrays from the middle out

This is a code-golf question.

Given integers s and n the task is to output all arrays of length n which take values from -s to s. The only twist is that you must output them in the following order.

• The all zeros array of length n.
• All arrays of length n with elements from -1 to 1 excluding any array you have outputted before.
• All arrays of length n with elements from -2 to 2 excluding any array you have outputted before.
• And so on until you get to all arrays of length n with elements from -s to s excluding any array you have outputted before.

You should output one array per line. They can be space or comma separated.

Here is some non-complying python code that outputs the arrays/lists/tuples in the right order.

import itertools

s =  3
n = 2

oldsofar = set()
newsofar = set()
for i in xrange(s):
for k in itertools.product(range(-i,i+1), repeat = n):
print newsofar - oldsofar
oldsofar = newsofar.copy()
print "***"


Extra glory (and an upvote from me) for answers that perform no set subtraction or equivalent.

• Can we write a function that prints the result? Mar 20, 2016 at 11:44
• @LegionMammal978 I would prefer a complete program. If this is deemed seriously controversial I will give in of course :)
– user9206
Mar 20, 2016 at 11:45
• Is there any required order within each of your bullet points? Mar 20, 2016 at 11:47
• @MartinBüttner No, not at all.
– user9206
Mar 20, 2016 at 12:56

# Jelly, 9 bytes

NRṗµAṀ€Ụị


No list subtraction was used in the making of this post. Try it online!

### How it works

NRṗµAṀ€Ụị  Main link. Arguments: s, n

N          Negate; yield -s.
R         Range; yield [-s, ..., s].
ṗ        Cartesian power; push all vectors of length n of those elements.
A      Compute the absolute values of all vector components.
Ṁ€    Get the maximum component of each vector.
Ụ   Sort the indices of A according to the maximal absolute value of the
corresponding vector's components.
ị  Retrieve the vectors of A at those indices.

• Now it's just getting silly!
– user9206
Mar 20, 2016 at 16:37
• "No lists were harmed in the making of this post" Mar 20, 2016 at 23:30

# MATL, 18 bytes

_G2$:iZ^t!|X>4#SY)  First input is s, second is n This works in current version (15.0.0) of the language. Try it online! ### Explanation _ % take input s implicitly. Negate to obtain -s G % push input s again 2$:    % inclusive range from -s to s
i      % take input n
Z^     % Cartesian power. Gives 2D array, with each result on a row
t!     % duplicate and transpose
|      % absolute value
X>     % maximum of each column
4#S    % sort and push the indices of the sorting
Y)     % apply as row indices into the 2D array. Display implicitly

• 18 bytes is outrageous :)
– user9206
Mar 20, 2016 at 13:07

n#s=[c|b<-[0..s],c<-mapM id$[-b..b]<$[1..n],any((b==).abs)c]


Usage example: 2#2-> [[0,0],[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1],[-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],[-1,-2],[-1,2],[0,-2],[0,2],[1,-2],[1,2],[2,-2],[2,-1],[2,0],[2,1],[2,2]].

How it works:

   b<-[0..s]                           -- loop b through 0 .. s
c<-mapM id$[-b..b]<$[1..n]     -- loop c through all lists of length n
-- made out of the numbers -b .. b
-- ("[-b..b]<\$[1..n]" is "replicate n [-b..b]";
--  "mapM id" is "sequence")
[c|                 ,any((b==).abs)c]  -- keep c if it contains b or -b


Edit: @xnor pointed out that mapM id is sequence.

• mapM id is shorter than sequence.
– xnor
Mar 20, 2016 at 23:31
• @xnor: True. Thanks!
– nimi
Mar 21, 2016 at 0:29

# Mathematica, 83 bytes

Print/@Select[Range[-#,b=#]~Tuples~a,Abs@#~MemberQ~b&]&/@Range[0,a=Input[];Input[]]


To use, put in a script and input n then s on separate lines. Prints each array as a curly-bracketed, comma-delimited list (e.g., {-1, 0, 1}). It works by taking every list of length n with numbers between [-cur..cur], and and printing those which include either -cur or cur. It then repeats this for all cur in [0..s]. (This post contains 19  characters!)

## JavaScript (SpiderMonkey 30+), 134 bytes

(s,n)=>n?[for(a of f(s,n-1))for(i of Array(s*2+1).keys())[i-n,...a]].sort((a,b)=>g(a)-g(b),g=a=>Math.max(...a,-Math.min(...a))):[[]]
`

Uses the cartesian-power-and-sort approach, which I thought of separately, but I was recompiling SpiderMonkey at the time so I unable to answer this before @Dennis.