scrabble by rolf

n=50
def h(q):
 x,y,d,w=q
 u=x;v=y
 if f[y-1+d][x-d]!=" ":return 0
 for c in w:
  if f[y][x]!=" "and f[y][x]!=c:return 0  
  if f[y][x]==" "and(f[y-d][x-1+d]!=" "or f[y+d][x+1-d]!=" "):return 0
  x+=d;y+=1-d
 if f[y][x]!=" ":return 0
 if w=="labeled":w="laleled"
 if w=="altitude":w="lltitude"
 for c in w:f[v][u]=c;u+=d;v+=1-d
 return 1
s=raw_input().split(" ");f=[[" "]*n for i in range(n)];q=(n/2,n/2,1,s.pop(0));h(q);d=0;k=""
for w in s:
 o=0;x,y,g,t=q;a=t in ["essay"];b=t in ["scrabble","natural"]
 w1,t1=(t,w)if b else(w,t) 
 x+=g*len(t)*a
 y+=(1-g)*len(t)*a
 for j in t1: 
  u,v=x,y 
  for i in w1:
   p=(u,v,d,w)
   if h(p):q=p;d=1-d;o=1;break
   if b:u+=g;v+=1-g     
   else:u-=d;v-=1-d
  if o:break
  if a:x-=g;y-=1-g
  elif b:x-=d;y-=1-d     
  else:x+=g;y+=1-g
 if 1-o:k="\n/* error with %s */"%w;break
r=["".join(z).rstrip()for z in f]
while r[0]=="":r=r[1:]
while r[-1]=="":r=r[:-1]
c=lambda z:0 if z[0]!=" "else 1+c(z[1:]);m=min([c(z)for z in r])
for z in r:print z[m:]
if k:print k

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

download

return top