杨辉三角算法

# -*- coding:utf-8 -*-
# author : Keekuun

"""
杨辉三角形,又称贾宪三角形,帕斯卡三角形,是二项式系数在三角形中的一种几何排列。规律如下:
         1                         0 1
        1 1                \     + 1 0     0 1 1
       1 2 1        --------\   --------  +1 0 0    0 1 2 1     0 1 3 3 1
      1 3 3 1       --------/      1 1   --------  +1 2 1 0    +1 3 3 1 0
     1 4 6 4 1             /               1 2 1   ---------   ----------
    ''''''''''                                      1 3 3 1     1 4 6 4 1
"""

# 补充知识zip函数:

a = [1, 2, 3]
b = [4, 5, 6]
c = [4, 5, 6, 7]
ab_zip = zip(a, b,c)
ab_list = list(ab_zip)
ac_zip = zip(a, c)
ac_list = list(ac_zip)
ac = list(zip(*ab_zip))

print(ab_zip)
print(ac_zip)
print(ab_list)
print(ac_list)
print(*ab_list)

print('*' * 50)
print('*' * 50)


def triangle1(n):
    if n == 1:
        return [1]
    else:
        return [sum(i) for i in zip((triangle1(n - 1) + [0]), [0] + triangle1(n - 1))]


for i in range(1, 11):
    print(triangle1(i))

print('*' * 50)
print('*' * 50)


def triangle2():
    L = [1]
    while True:
        yield L
        L = [sum(i) for i in zip((L + [0]), [0] + L)]


n = 0
for i in triangle2():
    print(i)
    n += 1
    if n >= 10:
        break

输出:

C:\python\Python36\python.exe E:/PycharmProjects/Leetcode/triangle.py
<zip object at 0x000001CEDE313A08>
<zip object at 0x000001CEDE313A88>
[(1, 4, 4), (2, 5, 5), (3, 6, 6)]
[(1, 4), (2, 5), (3, 6)]
(1, 4, 4) (2, 5, 5) (3, 6, 6)
**************************************************
**************************************************
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
**************************************************
**************************************************
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
[(1, 7, 4), (2, 8, 5), (3, 9, 6)]
(1, 7, 4) (2, 8, 5) (3, 9, 6)
(1, 2, 3) (7, 8, 9) (4, 5, 6)

Process finished with exit code 0

 

转载于:https://www.cnblogs.com/zkkysqs/p/9387769.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值