zoukankan      html  css  js  c++  java
  • 汉诺塔,杨辉三角之python实现

    汉诺塔

    def move(n, a, buffer, c):
    if(n == 1):
    print(a, "->", c)
    return
    move(n-1, a, c, buffer)
    move(1, a, buffer, c)
    move(n-1, buffer, a, c)
    
    move(1, 'a', 'b', 'c')

    杨辉三角

    def triangel(n):
    L = [1]
    while True:
    yield L
    L = [L[x] + L[x+1] for x in range(len(L)-1)]
    L.insert(0,1)
    L.append(1)
    if len(L) > 10:
    break
    
    a = angel(10)
    for i in a:
    print(i)

    注:普通函数和generator生成器的区别:
    1.普通函数调用直接返回结果,generator函数的调用,返回一个generator对象;(调用generator时可以先创建一个对象,再用next()方法不断获得下一个返回值,但实际中通常用for循环实现)
    2.generator在执行过程中,遇到yield就中断,下次又继续执行

  • 相关阅读:
    UNIX常用shell
    exit函数
    linux消息队列
    互斥量
    RCS版本控制
    linux samba
    UML建模
    linux syslog
    python基础-列表List及内置方法
    仿美团详情页与购物车源码-详情页
  • 原文地址:https://www.cnblogs.com/Emily07/p/6513854.html
Copyright © 2011-2022 走看看