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.
In the list of answers to Generate Pascal's triangle you may find additional ideas.
– Howard – 2013-03-20T13:51:20.757The 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.347There'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
withprint' '.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.8603Interesting. 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.947Having 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