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.
-
1\$\begingroup\$ Can we write a function that prints the result? \$\endgroup\$LegionMammal978– LegionMammal9782016年03月20日 11:44:19 +00:00Commented 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\$user9206– user92062016年03月20日 11:45:30 +00:00Commented Mar 20, 2016 at 11:45
-
\$\begingroup\$ Is there any required order within each of your bullet points? \$\endgroup\$Martin Ender– Martin Ender2016年03月20日 11:47:47 +00:00Commented Mar 20, 2016 at 11:47
-
\$\begingroup\$ @MartinBüttner No, not at all. \$\endgroup\$user9206– user92062016年03月20日 12:56:19 +00:00Commented Mar 20, 2016 at 12:56
5 Answers 5
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.
-
\$\begingroup\$ Now it's just getting silly! \$\endgroup\$user9206– user92062016年03月20日 16:37:15 +00:00Commented Mar 20, 2016 at 16:37
-
2\$\begingroup\$ "No lists were harmed in the making of this post" \$\endgroup\$Dennis van Gils– Dennis van Gils2016年03月20日 23:30:25 +00:00Commented 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.
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
-
1\$\begingroup\$ 18 bytes is outrageous :) \$\endgroup\$user9206– user92062016年03月20日 13:07:42 +00:00Commented Mar 20, 2016 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
.
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.