Can this program be shortened? Measuring program-length?
r.e.s.
r_e_s_01 at ZZZyahoo.com
Thu Jul 10 20:46:00 EDT 2008
<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
> 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:]
> 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
> 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".
More information about the Python-list
mailing list