[Python3]2行Python代碼實現楊輝三角形

yhsjx = lambda col: [1] * col if col <= 2 else [1] + [yhsjx(col - 1)[x] + yhsjx(col - 1)[x + 1] for x in range(len(yhsjx(col - 1)) - 1)] + [1]
[print(yhsjx(x)) for x in range(1, 6)]

生成一個5階楊輝三角形
運行結果

/usr/local/bin/python3.7 /Users/yqcd/Downloads/bob_folder/pyFolder/test.py
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]

Process finished with exit code 0

能不能減少爲1行呢?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章