Pascal's Triangle for Python

后端 未结 11 1567
轮回少年
轮回少年 2020-11-30 06:13

As a learning experience for Python, I am trying to code my own version of Pascal\'s triangle. It took me a few hours (as I am just starting), but I came out with this code:

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 06:28

    # combining the insights from Aaron Hall and Hai Vu,
    # we get:
    
    def pastri(n):
        rows = [[1]]
        for _ in range(1, n+1):
            rows.append([1] +
                        [sum(pair) for pair in zip(rows[-1], rows[-1][1:])] +
                        [1])
        return rows
    
    # thanks! learnt that "shape shifting" data,
    # can yield/generate elegant solutions.
    

提交回复
热议问题