# shortest possible python pascal triangle code [duplicate]

the contest is already finished, so I don't think it's cheating to ask: I am trying to optimize this small contest: pascal triangle I started with 4.72 and worked myself up to 7.48. The best made a 9.05 but in perl, python2 best score is 8.25. Unfortunately they don't let you look at what others did. My current solution is the following:

l=[1]
print 1
while(len(l)<31):
print' '.join(map(str,l+[1]))
l=[1]+map(lambda x,y:x+y,l,l[1:]+[1])


Now I tried to get rid of the loop in using list comprehension, but I am too new to python to get this done. Can anyone give me a hint for nested list comprehension shortening this code further down?

## marked as duplicate by Peter Taylor, Toto, grc, beary605, dmckeeMar 29 '13 at 5:41

• In the list of answers to Generate Pascal's triangle you may find additional ideas. – Howard Mar 20 '13 at 13:51
• The shortest solution I know of is 68 bytes, which would have scored a 8.3. But apparently a 63 byte solution exists. – primo Mar 20 '13 at 13:51
• There's a 63-byte Python solution in the duplicate thread, but that's taking input from stdin, so hard-coding 31 would save 5 for a score of 8.55. – Peter Taylor Mar 20 '13 at 13:53
• @PeterTaylor The only problem with that solution, is that you'd need to replace print x with print' '.join(map(str,x)) for a loss of 18 bytes. – primo Mar 20 '13 at 13:57
• Interesting. I must have posted that before I registered. That could have been made a byte shorter, actually: a=[];exec"a=map(sum,zip(a,[0]+a))+[1];print a;"*input() – primo Mar 20 '13 at 14:28

This is a bit shorter, but still no good:

l=[1]
while(len(l)<32):
print' '.join(map(str,l))

l=[1];exec"print' '.join(map(str,l));l=map(sum,zip([0]+l,l+[0]));"*31