帕斯卡三角形作为二维列表


11

创建一个Pascal三角形,它是一个嵌套列表,在未使用的位置包含零。

在输出数组中,帕斯卡三角形的数字用零分隔,并在每一侧用零填充,以使它们居中。例如,最下面一行(最后一个子数组)的左右两侧不得为零;倒数第二个子数组的每一侧都有一个零填充,依此类推。

这是输入的输出5

[[0,0,0,0,1,0,0,0,0],
[0,0,0,1,0,1,0,0,0],
[0,0,1,0,2,0,1,0,0],
[0,1,0,3,0,3,0,1,0],
[1,0,4,0,6,0,4,0,1]]

与往常一样,以字节数最少的解决方案为准。


5
重复。不幸的是,仅更改输出格式不会改变挑战。如果您仍然需要帮助,请尝试在Stack Overflow上发帖。
GamrCorps '16

2
好吧,还有额外的零。
CalculatorFeline

该程序将打印出您想要的代码(Python 3):print("def pascal(n):\n #make the nested list\n a=[[0 for i in range(2*n+1)] for j in range(n+1)] #make the list\n a[0][n]=1 #add the initial 1\n for i in range(1,n+1):\n for j in range(2*n+1):\n a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(2*n+1)] #the main part\n return a")
CalculatorFeline

1
@CatsAreFluffy多余的零将替换上一次迭代中的空格-从功能上讲,这是完全相同的问题。
ricdesi's

2
我可以使用本机数组表示语法作为我的语言,还是格式不可协商?

Answers:


3

Mathematica,70 68字节

NestList[ListConvolve[{1,0,1},#,2]&,Join[#,{1},#],#2]&[0~Table~#,#]&

与MATL解决方案相似。


3

Mathematica,48个字节

CellularAutomaton[{#+#3&@@#&,{},1},{{1},0},#-1]&

CellularAutomation 太棒了。


2

果冻,12个字节

NR¬ṙ-,1S$³Ð¡

在这里尝试。

说明

                   This is a list of functions, each operating on the input, n:
NR                 Get the range [-n -n+1 ... 0 ... n-1 n].
  ¬                Logical NOT the entire range: [0 0 ... 1 ... 0 0].
         ³Ð¡       Repeat n times, and cumulate the results:
   ṙ-,1                Rotate by both -1 and 1
       S               Sum the results.
        $              (Joins the above two functions)

1

Haskell,66个字节

q n|d<-0<$[2..n]=scanl(\(s:t)_->zipWith(+)(0:s:t)$t++[0])(d++1:d)d

用法示例:q 4-> [[0,0,0,1,0,0,0],[0,0,1,0,1,0,0],[0,1,0,2,0,1,0],[1,0,3,0,3,0,1]]

这个怎么运作:

d <- 0<$[2..n]                      -- bind d to a list of (length n)-1 zeros
scanl                               -- build a list
                         (d++1:d)   -- starting with  [d ++ 1 ++ d]
      \(s:t)_                    d  -- by combining the previous element with the
                                    -- elements of d, but ignoring them, i.e.
                                    -- build a list of (length d) by repeatedly
                                    -- modifying the start element by
          zipWith(+)                -- adding element-wise
                    (0:s:t)         -- the previous element prepended by 0  
                           t++[0]   -- and the tail of the previous element
                                    -- followed by a 0 

1

Python 3中,172个 158 133字节的

def p(n):
 x=2*n+1;y=range
 a=[[0]*x]*n;a[0][n]=1
 for i in y(1,n+1):
  for j in y(x):a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(x)]
 return a

不断进步


1
这还没打完,对吗?

嗯是的 这(略少打高尔夫球)是由我在问题上留下评论的程序打印的。
CalculatorFeline

1

MATL24 22 21字节

tEq:=Gq:"t5BX+8L)]N$v

编辑(2016年5月20日):从该语言的18.0.0版本开始,上面的代码需要进行一些更改才能运行。下面的链接包括那些修改

在线尝试!

这使用循环将每个新行压入堆栈。从前一行开始计算新行,[1,0,1]并使用卷积并仅保留所需大小。循环之后,所有行都串联到一个2D数组中,显示出来。2D数组在MATL中显示为列对齐的数字表。

t           % implicit input n. Duplicate
Eq          % 2*n-1
:           % range [1,2,...,2*n-1]
=           % gives [0,0,...1,...0,0]. This is the first row
Gq:         % range [1,2,...,n-1]
"           % for each. Repeat n-1 times
  t         %   duplicate latest row. This duplicate will become the next row
  5B        %   push array [1,0,1] (5 converted to binary)
  X+        %   convolution
  8L        %   predefined literal [2,-1i]. Used for indexing
  )         %   apply that index: remove one element at each end
]           % end for each
N$v         % concatenate all rows into a 2D array. Implicitly display

0

JavaScript中,152个 146字节

f=i=>[...Array(i)].map((x,j)=>(z=[...Array(i*2-1)].map((_,k)=>+!!~[i-j,i+j].indexOf(k+1)),y=j?z.map((_,k)=>_||(k&&(k+1 in y)?y[k-1]+y[k+1]:_)):z))


0

严重的是33个字节

╩╜r`╣;lD0nkdZΣ`M╜rRZ`i0nkd@;)kΣ`M

在线尝试

我相对确定至少可以删除其中的7个字节,因此我将等待发布说明,直到进一步打高尔夫球为止。


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.