shortest possible python pascal triangle code

3

1

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?

Thank you for your audience.

Johannes Maria Frank

Posted 2013-03-20T13:18:18.433

Reputation: 159

Question was closed 2013-03-29T05:41:30.013

In the list of answers to Generate Pascal's triangle you may find additional ideas.

– Howard – 2013-03-20T13:51:20.757

The shortest solution I know of is 68 bytes, which would have scored a 8.3. But apparently a 63 byte solution exists.

– primo – 2013-03-20T13:51:59.347

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 – 2013-03-20T13:53:34.903

@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 – 2013-03-20T13:57:27.160

@primo, ok. Still, using that with the even better Python answer there (oh, I see it's yours) equals the 8.25 best score in python2.

– Peter Taylor – 2013-03-20T14:04:01.860

3Interesting. 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 – 2013-03-20T14:28:24.947

Having seem this thread on the mother meta I know this is not going to be what you want to hear, but ... with the very limited exception of the "Tip" series of questions (python version) we don't actually answer "Help me to golf this particular code" questions here. We pose open challenges and compete on the answers. And, yeah, that's a little different from most Stack Exchange sites, But in any case, welcome. Please stay around.

– dmckee --- ex-moderator kitten – 2013-03-29T05:41:20.623

Answers

3

This is a bit shorter, but still no good:

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

And this one comes in at a score of 8.28 (credit to @primo):

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

Michael Wild

Posted 2013-03-20T13:18:18.433

Reputation: 131

1Thank you, I learned a lot. Replacing the loop with a exec"..;"*count will server for future challenges. Also I like your int.add in replacement of lambda x,y:x+y – Johannes Maria Frank – 2013-03-20T21:19:39.343

I like to say sorry not looking at the duplicate thread. I posted this one at stackoverflow and they recomended this site. So I posted it again, but forgot to do a thorough search. Thank you for your answers. – Johannes Maria Frank – 2013-03-20T21:28:38.040