Print Pascals Triangle

"""pascals_triangle = lambda n:[[i for i in str(11**j)] for j in range(n)]
print pascals_triangle 
"""
## {{{ http://code.activestate.com/recipes/577542/ (r5)
def pascals_triangle(n):
    x=[[1]]
    for i in range(n-1):
        x.append([sum(i) for i in zip([0]+x[-1],x[-1]+[0])])
    return x
## end of http://code.activestate.com/recipes/577542/ }}}


def print_pascals_triangle(n):    
    lenth = len(", ".join(str(x) for x in pascals_triangle(n)[-1]))    
    for x in pascals_triangle(n):
        print('{0:^{1}}'.format(x,lenth))       


        
if __name__=="__main__":
    print_pascals_triangle(15)        

你可能感兴趣的:(python,print,Pascals,Triangle)