Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
Can we write a function that prints the result? – LegionMammal978 Mar 20 at 11:44
    
@LegionMammal978 I would prefer a complete program. If this is deemed seriously controversial I will give in of course :) – Lembik Mar 20 at 11:45
    
Is there any required order within each of your bullet points? – Martin Büttner Mar 20 at 11:47
    
@MartinBüttner No, not at all. – Lembik Mar 20 at 12:56
up vote 7 down vote accepted

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.
   µ       Begin a new, monadic link. Argument: L (list of vectors)
    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.
share|improve this answer
    
Now it's just getting silly! – Lembik Mar 20 at 16:37
2  
"No lists were harmed in the making of this post" – Dennis van Gils Mar 20 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
share|improve this answer
1  
18 bytes is outrageous :) – Lembik Mar 20 at 13:07

Haskell, 61 60 bytes

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.

share|improve this answer
    
mapM id is shorter than sequence. – xnor Mar 20 at 23:31
    
@xnor: True. Thanks! – nimi Mar 21 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!)

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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