当前位置:网站首页>Yanghui triangle
Yanghui triangle
2022-07-27 19:26:00 【BasilGuo】
practice
Yang Hui triangle is defined as follows :
1
/ \
1 1
/ \ / \
1 2 1
/ \ / \ / \
1 3 3 1
/ \ / \ / \ / \
1 4 6 4 1
/ \ / \ / \ / \ / \
1 5 10 10 5 1
Think of each line as a list, Try to write a generator, Keep outputting the next line list:
# Looking forward to output :
# [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]
n = 0
results = []
for t in triangles():
print(t)
results.append(t)
n = n + 1
if n == 10:
break
if results == [
[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]
]:
print(' The test passed !')
else:
print(' Test to fail !')Method 1
# Really good !!!
# -*- coding: utf-8 -*-
def triangles():
ls = [1]
while True:
yield ls
ls = [1] + [ls[i]+ls[i+1] for i in range(len(ls)-1)] + [1]Method 2
# python The key to the code is to be concise and easy to understand
# The idea of Yang Hui triangle is as follows :
# The number in a line is list b,
# Then the next line is calculated as follows :
# 0, b0, b1, b2
# + b0, b1, b2, 0
# That is, add one at the beginning and one at the end 0 And then add up
def triangles():
b = [1]
while True:
yield b
c = [0] + b
d = b + [0]
b = list(map(operator.add, c, d)) source :
https://www.zhihu.com/question/39256042/answer/328053927
边栏推荐
- Daily question (02): inverted string
- 如何用自动化测试搞垮团队
- Nacos集群部署-高可用保证
- An experience
- Mongodb learning notes (1) - install mongodb and its related configurations
- Unity learning notes (realize the conveyor belt)
- Useful resources for ns2
- [Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
- SSM integration
- Nacos的基本使用(1)——入门
猜你喜欢

IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet

"Testing novice encyclopedia" 5-minute quick start pytest automated testing framework

Nacos的基本使用(1)——入门

go-zero单体服务使用泛型简化注册Handler路由

Performance analysis of continuous time systems (2) - second order system performance improvement methods PID, PR

VIVO应用市场APP上架总结

MongoDB学习笔记(1)——安装MongoDB及其相关配置

自控原理学习笔记-系统稳定性分析(2)-环路分析及Nyquist-Bode判据

Mongodb learning notes (1) - install mongodb and its related configurations

I'm afraid I won't use the JMeter interface testing tool if I accept this practical case
随机推荐
Performance analysis of continuous time system (1) - performance index and first and second order analysis of control system
Unity learning notes (rigid body physics collider trigger)
MySQL 02 initial experience
Using vscode to build u-boot development environment
MongoDB
收下这份实操案例,还怕不会用Jmeter接口测试工具
101. (cesium chapter) cesium particle system - snow
Using functions to extract numbers from text strings in Excel
kettle入门级操作第一篇(读取excel、输出excel)
[Luogu p4183] cow at large P (graph theory) (tree array)
Nacos集群部署-高可用保证
一个经验
Kettle8.2 installation and common problems
Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
SQL field type conversion
Analysis of Eureka server
X-shell remote connection virtual machine
Useful resources for ns2
kettle switch / case 控件实现分类处理
进行接口测试时,连接数据库,对数据源进行备份、还原、验证操作