从中间打印阵列


10

这是一个代码问题。

给定整数s和n,任务是输出长度为n的所有数组,这些数组的值从-s到s。唯一的不足是必须按以下顺序输出它们。

  • 长度为n的全零数组。
  • 长度为n且元素从-1到1的所有数组,不包括您之前输出的任何数组。
  • 长度为n的所有数组,元素从-2到2,不包括您之前输出的任何数组。
  • 依此类推,直到获得长度为n的所有数组,并包含从-s到s的元素(不包括之前输出的任何数组)。

您应该每行输出一个数组。它们可以以空格或逗号分隔。

这是一些不兼容的python代码,它们以正确的顺序输出数组/列表/元组。

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 "***"

对于没有设定减法或等效减法的答案,要有额外的荣耀(和我的支持)。


1
我们可以编写一个打印结果的函数吗?
LegionMammal978 '16

@ LegionMammal978我想要一个完整的程序。如果您认为这有争议,我当然会给出:)

您的每个要点内是否有任何必需的顺序?
Martin Ender

@MartinBüttner不,一点也不。

Answers:


6

果冻,9个字节

NRṗµAṀ€Ụị

在制作此帖子时未使用列表减法。在线尝试!

怎么运行的

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.

现在变得越来越傻了!

2
“在发布此帖子时,列表没有受到损害”
Dennis van Gils,

6

MATL,18字节

_G2$:iZ^t!|X>4#SY)

第一个输入为s,第二个输入为n

这适用于该语言的当前版本(15.0.0)

在线尝试!

说明

_      % 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
18个字节

4

Haskell,61 60字节

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

用法示例: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]]

怎么运行的:

   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

编辑:@xnor指出mapM idsequence


mapM id比短sequence
xnor

@xnor:是的。谢谢!
nimi

2

Mathematica,83个字节

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

为了使用,放在一个脚本,并输入n然后s在不同的行。将每个数组打印为一个用花括号括起来的逗号分隔列表(例如{-1, 0, 1})。它的工作方式是取每个长度列表,长度n在之间的数字[-cur..cur],并打印包含-cur或的数字cur。然后重复此为所有cur[0..s](此帖子包含19个字符!)


1

JavaScript(SpiderMonkey 30 +),134个字节

(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))):[[]]

使用我分开考虑的笛卡尔乘方和排序方法,但是当时我正在重新编译SpiderMonkey,因此我无法在@Dennis之前回答。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.