# Pascal's Triangle as a 2-dimensional list

Create a Pascal's Triangle that is a nested list and contains zeros in the unused spots.

In the output array, the numbers of Pascal's Triangle are separated by zeroes and padded by zeroes on each side so that they are centered. For example, the bottom row (last sub-array) must have no zeroes on the left and the right; the second-last sub-array has one zero padding on each side, and so on.

Here is the output for input 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]]


As usual, the solution with the fewest bytes wins.

• Duplicate of this. Just changing the output format does not change the challenge, unfortunately. Try posting on Stack Overflow if you still need help with this. – GamrCorps Feb 29 '16 at 4:44
• Well, there are the extra zeros. – CalculatorFeline Feb 29 '16 at 4:46
• This program prints the one you want (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 Feb 29 '16 at 4:53
• @CatsAreFluffy The extra zeroes just replace the spaces in the previous iteration - this is functionally the exact same problem. – ricdesi Feb 29 '16 at 14:42
• Can I use the native array representation syntax for my language, or is the format non-negotiable? – cat Feb 29 '16 at 15:46

# Mathematica, 70 68 bytes

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


Similar to the MATL solution.

# Mathematica, 48 bytes

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


CellularAutomation is fantastic.

# Jelly, 12 bytes

NR¬ṙ-,1S$³Ð¡  Try it here. ### Explanation  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)


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


Usage example: 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]].

How it works:

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  # Python 3, 172158 133 bytes 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  Keeps getting better • this isn't fully golfed yet, right? – cat Feb 29 '16 at 15:41 • Um, yeah. This (in a slightly less golfed form) is printed by a program I left a a comment on the question. – CalculatorFeline Feb 29 '16 at 16:16 # MATL, 2422 21 bytes tEq:=Gq:"t5BX+8L)]N$v


EDIT (May 20, 2016): as of version 18.0.0 of the language, the above code needs a few changes to run. The link below includes those modifications

Try it online!

This uses a loop to push each new row onto the stack. A new row is computed from the previous row applying convolution with [1,0,1] and keeping only the desired size. After the loop, all rows are concatenated into a 2D array, which is displayed. 2D array are displayed in MATL as column-aligned numeric tables.

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  # Javascript, 152 146 bytes 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))  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 ) ) F=i=>JSON.stringify(f(+i)).replace(/],|^.|.(?=.$)/g,'$&\n') I.oninput() <input id=I onInput="O.innerHTML=F(I.value)" value=5><pre id=O> ## Seriously, 33 bytes ╩╜r╣;lD0nkdZΣM╜rRZi0nkd@;)kΣM  Try it online I'm relatively certain at least 7 or so of those bytes can be shaved off, so I'm going to wait to post an explanation until I'm done golfing this further. # PHP, 106 bytes for(;$r++<$a=$argn;)for($c=-$a;++$c<$a;)$t[$r][$c]=$r>1|$c?$t[$r-1][$c-1]+$t[$r-1][$c+1]?:0:1;print_r($t);


Try it online!