Python之杨辉三角算法实现
学习了廖雪峰的官方网站的python一些基础,里面有个题目,就是让写出杨辉三角的实现,然后我就花了时间实现了一把。思路也很简单,就是收尾插入0,然后逐层按照杨辉三角的算法去求和实现杨辉三角。

附属代码:
# -*- coding: utf-8 -*- # 期待输出:
# [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] def triangles(): L = [1]
yield L
while True:
# print 'L是:',L # 在L这个List头尾插入0
L.insert(0,0)
L.insert(len(L),0) n = 0
# print 'n:%d--L的长度%d'%(n,len(L))
T = []
while n < len(L)-1:
T.append(L[n]+L[n+1])
n = n+1 L = T
yield L n = 0
for t in triangles():
print(t)
n = n + 1
if n == 10:
break
Python之杨辉三角算法实现的更多相关文章
- 利用python打印杨辉三角
用python打印杨辉三角 介绍 杨辉三角,是初高中时候的一个数列,其核心思想就是说生成一个数列,该数列中的每一个元素,都是之前一个数列中,同样位置的元素和前一个元素的和. 正好在python中,也就 ...
- python实现杨辉三角
刚刚学python,原来用c++,Java很轻松实现的杨辉三角,现在用python实现,代码是少了,理解起来却不容易啊. 这里主要用到的Python的生成器. 我们都知道Python有列表解析功能,根 ...
- python 实现杨辉三角(依旧遗留问题)
1 #! usr/bin/env python3 #-*- coding :utf-8 -*- print('杨辉三角的generator') def triangles(): N=[1] while ...
- python输出杨辉三角
使用python列表,展示杨辉三角 # !/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan yanghui = [] fo ...
- Python实现杨辉三角,超详细!
巧妙实现杨辉三角代码 def triangles(): N=[1] #初始化为[1],杨辉三角的每一行为一个list while True: yield N #yield 实现记录功能,没有下一个ne ...
- Python杨辉三角算法
#!/usr/bin/env python # -*- coding: utf-8 -*- def triangles(): n = 1 aboveList = [] while True: if n ...
- python 杨辉三角 算法实现
def triangles(level): n = 1 L = [] while n <=level: if n <= 2: L.append(1) yield L elif n > ...
- python的杨辉三角
# # / \ # # / \ / \ # # / \ / \ / \ # # / \ / \ / \ / \ # # / \ / \ / \ / \ / \ # # ---------------- ...
- php写杨辉三角算法
<?phpfunction YangHui($iLine) { for ($i = 0;$i <= $iLine;$i++)//行 { for ($j ...
随机推荐
- spring结合mybatis实现数据库读写分离
随着系统用户访问量的不断增加,数据库的频繁访问将成为我们系统的一大瓶颈之一.由于项目前期用户量不大,我们实现单一的数据库就能完成.但是后期单一的数据库根本无法支撑庞大的项目去访问数据库,那么如何解决这 ...
- JavaScript 之 JavaScript 对象
重新看JavaScript对象,参考资料: RUNOOB.COM:http://www.runoob.com/jsref/jsref-fromcharcode.html: CodePlayer:htt ...
- Internet传输协议-TCP
http://phei.eefocus.com/book/08-07/473781276058574.html http://www.eefocus.com/communication/210643 ...
- 《React-Native系列》44、基于多个TextInput的键盘遮挡处理方案优化
曾经写过两篇关于在ReactNative上处理键盘遮挡输入表单TextInput的情况.建议读者能够先看看 1.<React-Native系列>33. 键盘遮挡问题处理 2.<Rea ...
- C++库研究笔记--用__attribute__((deprecated)) 管理过时代码
用__attribute__((deprecated)) 管理过时代码.同一时候保留兼容的接口 Linux下: #define DEPR_AFTER __attribute__((deprecated ...
- tcmalloc asan
http://blog.csdn.net/jinzhuojun/article/details/46659155 http://blog.csdn.net/hanlizhong85/article/d ...
- python程序打包
环境: CentOS6.5_x64Python版本 : 2.6 使用pyinstaller打包 pyinstaller可以将python程序打包成二进制文件,打包后的文件在没有python的环境中也可 ...
- 解决ODI 12C Studio 运行缓慢问题
一.配置 ODI 12C Studio 1.1 修改ODI Studio process的-Xms和-Xmx ide.conf: modifying the initial Heap size (-X ...
- POJ 3268 Bookshelf 2 动态规划法题解
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- eclipse debug模式下总是自动跳到ThreadPoolExecutor.java类
1.情景展示 使用eclipse,debug模式运行项目时,总是会调用这个ThreadPoolExecutor.java类,明明没有打断点却自动停止运行. 2.原因分析 在eclipse中,默认是 ...