Can this program be shortened? Measuring program-length?
wolfram.hinderer at googlemail.com
wolfram.hinderer at googlemail.com
Thu Jul 10 17:59:13 EDT 2008
On 10 Jul., 21:57, "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)
More information about the Python-list
mailing list