当前位置:网站首页>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
边栏推荐
- 200行代码快速入门文档型数据库MonogoDB
- Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
- Kettle switch / case control to realize classification processing
- Unity learning notes (rigid body physics collider trigger)
- 自控原理学习笔记-系统稳定性分析(2)-环路分析及Nyquist-Bode判据
- 如何用自动化测试搞垮团队
- kettle JVM内存设置---效果不明显
- 大冤种们,新进测试行业,如何正确选择意向企业?
- How to break the team with automated testing
- 每日一题(02):倒置字符串
猜你喜欢

编程式跳转

Kinect2 for unity3d - avatardemo learning

An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?

New system installation mysql+sqlyog

How to break the team with automated testing

2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions

MySQL learning notes (1) -- variables

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

Webmagic+selenium+chromedriver+jdbc垂直抓取数据。

The understanding of string in C.
随机推荐
The understanding of string in C.
Webmagic+selenium+chromedriver+jdbc grabs data vertically.
asp. Net experience
go-zero单体服务使用泛型简化注册Handler路由
X-shell remote connection virtual machine
kettle switch / case 控件实现分类处理
Definition of graph traversal and depth first search and breadth first search (2)
During the interface test, connect to the database and back up, restore and verify the data source
VIVO应用市场APP上架总结
opds sql 里面可以用set 定义局部变量吗
C语言打印菱形
Self control principle learning notes - system stability analysis (1) - BIBO stability and Routh criterion
kettle8.2 安装及常见问题
The go zero singleton service uses generics to simplify the registration of handler routes
Some advice for NS2 beginner.
JMeter interface automation - how to solve the content type conflict of request headers
"Testing novice encyclopedia" 5-minute quick start pytest automated testing framework
「测试新手百科」5 分钟快速上手Pytest 自动化测试框架
Kettle separate and merge records
Electromagnetic field learning notes - vector analysis and field theory foundation