Recursive Expansion by test

R=range
def F(x,y,u,v,r,s,n):
 if n:F(x,y,r,s,u/4,v/4,n-1);F(x+u/4,y+v/4,u/2,v/2,r/2,s/2,n-1);F(x+u/4+r/2,y+v/4+s/2,u/2,v/2,r/2,s/2,n-1);F(x+3*u/4,y+3*v/4,r,s,u/4,v/4,n-1)
 else:
  w=max(r,u);h=max(v,s)
  for i in R(h):f[y+i+1][2*x]=f[y+i+1][2*x+2*w]="|"
  for i in R(2*w):
   for j in [0,h]:
    if f[y+j][2*x+i]!="|":f[y+j][2*x+i]="_"
n=input()
f=[[" "]*(2**(n+2)+1)for i in R(2**n+1)]
F(0,0,2**-~n,0,0,2**n,n)
f[0][0]=" "
for x in f:print"".join(x).rstrip()

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page