当前位置:网站首页>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
边栏推荐
- Error analysis of building Alibaba cloud +typera+picgo map bed
- ES6 learning notes (1) - quick start
- 如何用自动化测试搞垮团队
- kettle8.2 安装及常见问题
- 「测试新手百科」5 分钟快速上手Pytest 自动化测试框架
- Unity learning notes (rigid body physics collider trigger)
- ES6 new method
- WinForm screenshot save C code
- Kinect for unity3d - backgroundremovaldemo learning
- "Testing novice encyclopedia" 5-minute quick start pytest automated testing framework
猜你喜欢

What if idea successfully connects to the database without displaying the table

SQL field type conversion

每日一题(02):倒置字符串

The understanding of string in C.

kettle引用外部脚本完成电话号码清洗、去重缩进

C language case: password setting and login > clear solution getchar and scanf

编程式跳转

阿里云视频点播服务的开通和使用

2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management

MongoDB学习笔记(1)——安装MongoDB及其相关配置
随机推荐
MySQL learning notes (1) -- variables
kettle switch / case 控件实现分类处理
Selenium automated test interview questions family bucket
Mongodb learning notes (1) - install mongodb and its related configurations
Daily question (02): inverted string
一个经验
JS common utils encapsulation
go-zero单体服务使用泛型简化注册Handler路由
每日一题(02):倒置字符串
4 轮拿下字节 Offer,面试题复盘
ref 关键字的用法
SQL time processing (SQL server\oracle)
Kinect for Unity3d----KinectManager
搭建阿里云+typora+Picgo图床错误分析
C language case: password setting and login > clear solution getchar and scanf
Kettle references external scripts to complete phone number cleaning, de duplication and indentation
阿里云视频点播服务的开通和使用
Summary of "performance test" of special test
Kettle learning - the repository configuration in version 8.2 is grayed out, and there is no connect button
C language printing diamond