Can this program be shortened? Measuring program-length?

r.e.s. r_e_s_01 at ZZZyahoo.com
Thu Jul 10 21:03:24 EDT 2008


"r.e.s." <r_e_s_01 at ZZZyahoo.com> wrote ...
> <wolfram.hinderer at googlemail.com> wrote ...
>> "r.e.s." <r_e_s... at ZZZyahoo.com> wrote:
>>> Can the following program be shortened? ...
>>>
>>> def h(n,m):
>>>  E=n,
>>>  while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
>>>  return n
>>> h(9,9)
>>>
>> 
>> Some ideas...
>> 
>> # h is your version
>> def h(n,m):
>> E=n,
>> while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
>> return n
>> 
>> def g(n,m):
>> E=n,
>> while E*m:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
               ^
               g
>> return n
>> 
>> def f(n,m):
>> E=n,
>> while E*m:n=h(n+1,m-1);E=(E[0]>0)*(E[0]-1,)*n+E[1:]
               ^
               f
>> return n
>> 
>> def e(n,m):
>> E=[n]
>> while E*m:n=h(n+1,m-1);E[:1]=(E[0]>0)*[E[0]-1]*n
               ^
               e
>> return n
>> 
>> # some tests
>> print h(1,1), h(2,1), h(0,2)
>> print g(1,1), g(2,1), g(0,2)
>> print f(1,1), f(2,1), f(0,2)
>> print e(1,1), e(2,1), e(0,2)
> 
> Very instructive! Thank you for the "step-by-step".

I forgot to mention the obvious typos. Thanks again.



More information about the Python-list mailing list