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):
newsofar.add(k)
print newsofar - oldsofar
oldsofar = newsofar.copy()
print "***"
Extra glory (and an upvote from me) for answers that perform no set subtraction or equivalent.