leetcode python 006

##  改为z型字符串
def change_to_z(s,n):
    ## 字符串不能生成完整的区,用空格补全
    b=len(s)%(2*n-2)
    if b!=0:
        s+=' '*(n*2-2-b)
    a=len(s)//(2*n-2)
    lz=[]
    ##  不同的层
    for i in range(n):
        lg=[]
        ##完整的区
        for j in range(a):
            if i==0 or i==n-1:
                l=s[(2*n-2)*j+i]+' '*(n-1)
            else:
                l=s[(2*n-2)*j+i]+' '*(n-i-1)+s[(2*n-2)*j+2*n-i-2]+' '*(i-1)
            lg.append(l)
        lz.append('#'+''.join(lg))
    return '#\n'.join(lz)+'#'    
    
s='12345678'*10
print(change_to_z(s,7))

猜你喜欢

转载自www.cnblogs.com/offline-ant/p/9368515.html
006