Pascal's Triangle

Given N; generate Pascal's triangle up to N rows.

Each column should have one space after it unless it is the last column.
All numbers should be right-aligned to the column.

IN

10

OUT

1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1

IN

1

OUT

1

IN

2

OUT

1
1 1

IN

3

OUT

1
1 1
1 2 1

IN

4

OUT

1
1 1
1 2 1
1 3 3 1

IN

5

OUT

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

IN

6

OUT

1
1 1
1 2  1
1 3  3  1
1 4  6  4 1
1 5 10 10 5 1

IN

7

OUT

1
1 1
1 2  1
1 3  3  1
1 4  6  4  1
1 5 10 10  5 1
1 6 15 20 15 6 1

IN

8

OUT

1
1 1
1 2  1
1 3  3  1
1 4  6  4  1
1 5 10 10  5  1
1 6 15 20 15  6 1
1 7 21 35 35 21 7 1

IN

9

OUT

1
1 1
1 2  1
1 3  3  1
1 4  6  4  1
1 5 10 10  5  1
1 6 15 20 15  6  1
1 7 21 35 35 21  7 1
1 8 28 56 70 56 28 8 1
Login to submit solution


161 symbols php failed test 1 Wrong answer
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Tue Jan 13 20:11:00 2015 AndreyM
<?php $a=$n=array();$c=fgets(STDIN);for($i=0;$i<$c;$i++){for($j=0;$j<=$x=count($a);$j++)(!$j||$j==$x)?$n[$j]=1:$n[$j]+=$a[$j-1];echo implode(" ",$n)."\n";$a=$n;}
154 symbols python2 failed test 1 Wrong answer
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Tue Jan 13 22:47:11 2015 beched
n=int(raw_input())
r=[[1]]
for i in xrange(1,n):r.append([int(j>0)*r[i-1][j-1]+int(j<i)*(r[i-1]+[0])[j]for j in xrange(i)]);print ' '.join(map(str,r[-1]))
107 symbols haskell failed test 1 Wrong answer
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Fri Mar 13 15:20:34 2015 YoshikuniJujo
main=interact$unlines.map(unwords.map show).(`take`p).read
p=[1]:map(\l@(_:t)->(1:).(++[1])$zipWith(+)l t)p
83 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 2, in <module> for I in range(input()):print L;L=[[L+[0]][i]+[[0]+L][i] for I in range(L+1)] TypeError: can only concatenate list (not "int") to list

Tue Oct 13 15:13:23 2015 BogdanShevchenko
L=[1]
for I in range(input()):print L;L=[[L+[0]][i]+[[0]+L][i] for I in range(L+1)]
88 symbols python2 failed test 1 Wrong answer
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Tue Oct 13 15:15:58 2015 BogdanShevchenko
L=[1]
for k in range(input()):print L;L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)]
98 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 2, in <module> for k in range(input()):print ' '.join(L);L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)] TypeError: sequence item 0: expected string, int found

Tue Oct 13 15:17:36 2015 BogdanShevchenko
L=[1]
for k in range(input()):print ' '.join(L);L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)]
107 symbols python2 failed test 1 Wrong answer
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Tue Oct 13 15:18:19 2015 BogdanShevchenko
L=[1]
for k in range(input()):print ' '.join(map(str,L));L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)]
255 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 4, in <module> for u in f:m[u]=len(str(max([P[j][u] for j in range(u,d)])))-1 IndexError: list assignment index out of range

Tue Oct 13 15:39:05 2015 BogdanShevchenko
L,P,m,d=[1],[],[],input()
f=range(d)
for k in f:L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)];P+=[L]
for u in f:m[u]=len(str(max([P[j][u] for j in range(u,d)])))-1
for y in f:P[y]=[' '*m[y]+str(P[y][g]) for g in range(len(P[y]))];print ' '.join(P[y])
255 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 4, in <module> for u in f:m[u]=len(str(max([P[u][j] for j in range(u,d)])))-1 IndexError: list index out of range

Tue Oct 13 15:39:52 2015 BogdanShevchenko
L,P,m,d=[1],[],[],input()
f=range(d)
for k in f:L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)];P+=[L]
for u in f:m[u]=len(str(max([P[u][j] for j in range(u,d)])))-1
for y in f:P[y]=[' '*m[y]+str(P[y][g]) for g in range(len(P[y]))];print ' '.join(P[y])
224 symbols python2 failed test 1 Code error

File "./code", line 5 for k in f:print ' '.join([' '*(len(P[d][j])-len(P[k][j])+P[k][j] for j in range(len(P[k]))]) ^ SyntaxError: invalid syntax

Tue Oct 13 16:05:45 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)];P+=[L]
for k in f:P[k]=map(str,P[k])
for k in f:print ' '.join([' '*(len(P[d][j])-len(P[k][j])+P[k][j] for j in range(len(P[k]))])
225 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 5, in <module> for k in f:print ' '.join([' '*(len(P[d][j])-len(P[k][j]))+P[k][j] for j in range(len(P[k]))]) IndexError: list index out of range

Tue Oct 13 16:06:53 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)];P+=[L]
for k in f:P[k]=map(str,P[k])
for k in f:print ' '.join([' '*(len(P[d][j])-len(P[k][j]))+P[k][j] for j in range(len(P[k]))])
225 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 5, in <module> for k in f:print ' '.join([' '*(len(P[j][d])-len(P[j][k]))+P[j][k] for j in range(len(P[k]))]) IndexError: list index out of range

Tue Oct 13 16:07:41 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)];P+=[L]
for k in f:P[k]=map(str,P[k])
for k in f:print ' '.join([' '*(len(P[j][d])-len(P[j][k]))+P[j][k] for j in range(len(P[k]))])
227 symbols python2 failed test 1 Wrong answer
1  1
1  2  1
1  3  3   1
1  4  6   4   1
1  5 10  10   5   1
1  6 15  20  15   6   1
1  7 21  35  35  21   7   1
1  8 28  56  70  56  28   8  1
1  9 36  84 126 126  84  36  9  1
1 10 45 120 210 252 210 120 45 10 1
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Tue Oct 13 16:10:15 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)];P+=[L]
for k in f:P[k]=map(str,P[k])
for k in f:print ' '.join([' '*(len(P[d-1][j])-len(P[k][j]))+P[k][j] for j in range(len(P[k]))])
217 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 4, in <module> for k in f:print ' '.join([' '*(len(str(P[d-1][j]))-len(str(P[k][j])))+str(P[k][j]) for j in range(len(str(P[k])))]) IndexError: list index out of range

Tue Oct 13 16:14:19 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:P+=[L];L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)]
for k in f:print ' '.join([' '*(len(str(P[d-1][j]))-len(str(P[k][j])))+str(P[k][j]) for j in range(len(str(P[k])))])
217 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 4, in <module> for k in f:print ' '.join([' '*(len(str(P[d-1][j]))-len(str(P[k][j])))+str(P[k][j]) for j in range(len(str(P[k])))]) IndexError: list index out of range

Tue Oct 13 16:15:47 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:P+=[L];L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)]
for k in f:print ' '.join([' '*(len(str(P[d-1][j]))-len(str(P[k][j])))+str(P[k][j]) for j in range(len(str(P[k])))])
217 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 4, in <module> for k in f:print ' '.join([' '*(len(str(P[d-1][j]))-len(str(P[k][j])))+str(P[k][j]) for j in range(len(str(P[k])))]) IndexError: list index out of range

Tue Oct 13 16:17:49 2015 BogdanShevchenko
L,P,d=[1],[],input()
f=range(d)
for k in f:P+=[L];L=[(L+[0])[i]+([0]+L)[i] for i in range(len(L)+1)]
for k in f:print ' '.join([' '*(len(str(P[d-1][j]))-len(str(P[k][j])))+str(P[k][j]) for j in range(len(str(P[k])))])
221 symbols python2 failed test 1 Code error

Traceback (most recent call last): File "./code", line 3, in <module> for k in f:P+=[(P[k]+[0])[i]+([0]+P[k])[i] for i in range(len(P[k])+1)] TypeError: object of type 'int' has no len()

Tue Oct 13 16:22:23 2015 BogdanShevchenko
P,d=[[1]],input()
f=range(d)
for k in f:P+=[(P[k]+[0])[i]+([0]+P[k])[i] for i in range(len(P[k])+1)]
for k in f:P[k]=map(str,P[k]);print ' '.join([' '*(len(str(P[d-1][j]))-len(P[k][j]))+P[k][j] for j in range(len(P[k]))])
185 symbols bash failed test 1 Wrong answer

                      
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Sun Jul 24 17:10:44 2016 hidefromkgb
n=`cat`;p=1;for i in `seq $n`;do c=1+;for j in `seq $n`;do [ $p = 1 ]&&c+=0+||c+=`echo $p|cut -d+ -f-2|bc`+;p=${p#*+};done;echo ${c//+/ };p=$c;done|rev|column -to\ |rev|sed 's/  *0.*//'
147 symbols bash failed test 1 Wrong answer
1 0 0 0 0 0 0 0 0 0 0
1
1
1
1
1
1
1
1
1
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Sun Jul 24 17:13:17 2016 hidefromkgb
n=`cat`;p=1;for i in `seq $n`;do c=1+;for j in `seq $n`;do [ $p = 1 ]&&c+=0+||c+=`echo $p|cut -d+ -f-2|bc`+;p=${p#*+};done;echo ${c//+/ };p=$c;done
168 symbols bash failed test 1 Wrong answer

                      
instead of
1
1 1
1 2  1
1 3  3  1
1 4  6  4   1
1 5 10 10   5   1
1 6 15 20  15   6  1
1 7 21 35  35  21  7  1
1 8 28 56  70  56 28  8 1
1 9 36 84 126 126 84 36 9 1
Sun Jul 24 17:29:07 2016 hidefromkgb
n=`cat`;p=0+0;for i in `seq $n`;do c=1+;for j in `seq $n`;do c+=`echo $p|cut -d+ -f-2|bc`+;p=${p#*+};done;echo ${c//+/ };p=$c;done|rev|column -to\ |rev|sed 's/  *0.*//'

View all solutions