11
\$\begingroup\$

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.

Martin Ender
198k67 gold badges455 silver badges997 bronze badges
asked Mar 20, 2016 at 11:36
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Can we write a function that prints the result? \$\endgroup\$ Commented Mar 20, 2016 at 11:44
  • \$\begingroup\$ @LegionMammal978 I would prefer a complete program. If this is deemed seriously controversial I will give in of course :) \$\endgroup\$ Commented Mar 20, 2016 at 11:45
  • \$\begingroup\$ Is there any required order within each of your bullet points? \$\endgroup\$ Commented Mar 20, 2016 at 11:47
  • \$\begingroup\$ @MartinBüttner No, not at all. \$\endgroup\$ Commented Mar 20, 2016 at 12:56

5 Answers 5

6
\$\begingroup\$

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.
answered Mar 20, 2016 at 15:29
\$\endgroup\$
2
  • \$\begingroup\$ Now it's just getting silly! \$\endgroup\$ Commented Mar 20, 2016 at 16:37
  • 2
    \$\begingroup\$ "No lists were harmed in the making of this post" \$\endgroup\$ Commented Mar 20, 2016 at 23:30
6
\$\begingroup\$

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
answered Mar 20, 2016 at 11:58
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 18 bytes is outrageous :) \$\endgroup\$ Commented Mar 20, 2016 at 13:07
4
\$\begingroup\$

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.

answered Mar 20, 2016 at 17:43
\$\endgroup\$
2
  • \$\begingroup\$ mapM id is shorter than sequence. \$\endgroup\$ Commented Mar 20, 2016 at 23:31
  • \$\begingroup\$ @xnor: True. Thanks! \$\endgroup\$ Commented Mar 21, 2016 at 0:29
2
\$\begingroup\$

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!)

answered Mar 20, 2016 at 11:53
\$\endgroup\$
1
\$\begingroup\$

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.

answered Mar 20, 2016 at 19:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.