def yanghui(lines):
currentlst,lastlst,n=[],[],1
if lines<1:
return
while n<=lines:
lastlst=currentlst
currentlst=[]
for i in range(n):
if(i==0):
currentlst.insert(0,1)
elif(i==n-1):
currentlst.insert(i,1)
else:
currentlst.insert(i,lastlst[i]+lastlst[i-1])
n=n+1
yield currentlst f = yanghui(10)
for t in f:
print(t)

  

用python实现杨辉三角的更多相关文章

  1. 利用python打印杨辉三角

    用python打印杨辉三角 介绍 杨辉三角,是初高中时候的一个数列,其核心思想就是说生成一个数列,该数列中的每一个元素,都是之前一个数列中,同样位置的元素和前一个元素的和. 正好在python中,也就 ...

  2. python实现杨辉三角

    刚刚学python,原来用c++,Java很轻松实现的杨辉三角,现在用python实现,代码是少了,理解起来却不容易啊. 这里主要用到的Python的生成器. 我们都知道Python有列表解析功能,根 ...

  3. python 实现杨辉三角(依旧遗留问题)

    1 #! usr/bin/env python3 #-*- coding :utf-8 -*- print('杨辉三角的generator') def triangles(): N=[1] while ...

  4. Python之杨辉三角算法实现

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

  5. python输出杨辉三角

    使用python列表,展示杨辉三角 # !/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan yanghui = [] fo ...

  6. Python实现杨辉三角,超详细!

    巧妙实现杨辉三角代码 def triangles(): N=[1] #初始化为[1],杨辉三角的每一行为一个list while True: yield N #yield 实现记录功能,没有下一个ne ...

  7. python的杨辉三角

    # # / \ # # / \ / \ # # / \ / \ / \ # # / \ / \ / \ / \ # # / \ / \ / \ / \ / \ # # ---------------- ...

  8. python 生成器生成杨辉三角

    用Python写趣味程序感觉屌屌的,停不下来 #生成器生成展示杨辉三角 #原理是在一个2维数组里展示杨辉三角,空的地方用0,输出时,转化为' ' def yang(line): n,leng=0,2* ...

  9. 【Python初级】由生成杨辉三角代码所思考的一些问题

    杨辉三角定义如下: 1 / \ 1 1 / \ / \ 1 2 1 / \ / \ / \ 1 3 3 1 / \ / \ / \ / \ 1 4 6 4 1 / \ / \ / \ / \ / \ ...

随机推荐

  1. 重拾安卓_01_安卓开发环境搭建(eclipse)

    一.下载安装Android SDK 1.下载地址 (1)官网(可FQ选择):http://developer.android.com/sdk/index.html (2)不可FQ选择:http://w ...

  2. Java_异常_01_org.apache.commons.lang.exception.NestableRuntimeException

    异常信息: The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved. It is ...

  3. javaScript-基础篇(二)

    1.DOM概念 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树) 将HTML代码 ...

  4. codeforces 633D D. Fibonacci-ish(dfs+暴力+map)

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  5. OpenCV——非线性滤波器

    参考: PS 图像特效,非线性滤波器 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_IN ...

  6. sql根据坐标算距离

    CREATE FUNCTION ConvertXYToDistance(@la1 DECIMAL,@lo1 DECIMAL,@la2 DECIMAL,@lo2 DECIMAL)RETURNS FLOA ...

  7. JEECG datagrid 列表检索条件 添加下拉级联功能

    $("#communityId").change( function(){ var id = $(this).children('option:selected').val(); ...

  8. JAVA JDBC 读取配置文件链接数据库(oracle)

    ----db.properties-------- dbDriver = oracle.jdbc.driver.OracleDriverurl = jdbc:oracle:thin:@192.168. ...

  9. linux shell查询

    查看当前版本可用的shell cat /etc/shells 查看当前使用的shell echo $0 查看当前用户默认使用的shell echo $SHELL

  10. Poj 1316 Self Numbers(水题)

    一.Description In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called se ...