python使用递归思想处理问题

今天我们主要学习使用递归思想处理,斐波那契数列,打印文件层级结构

,汉诺塔小游戏。

递归说的稍微简点就是函数调用自身的一种方法,哪有人说,自己调用自己那不就死循环的了吗,那就需要我们自己设定结束条件,我个人认为递归思想你只要把开头和结束条件理清了思路,中间的那大部分就是一遍又一遍的调用自身。
先来看一下斐波纳挈数列,我们先是用循环在使用递归思想来打印:
def fab(n):
    a = 0
    b = 1
    while n > 0:

        if a == 0 or b == 1:
            print(1,end= ' ')
            a, b = b, a + b
            n -= 1
        else:
            print(b, end= ' ')
            a, b = b, a + b
            n -= 1
   我们打印一下5以内的斐波纳挈数列:
   fab(10)
   G:\python\python.exe G:/Pyproject/day115/test3.py
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 
Process finished with exit code 0

我们在使用递归思想打印斐波纳挈数列:

def fab(n):
    if n == 2 or n == 1:
        return 1
    else:
         return fab(n - 1) + fab(n - 2)


for i in range(1, 21):
    print(fab(i), end= ' ')

我们打印二十以内的斐波纳挈数列
G:\python\python.exe G:/Pyproject/day115/test3.py
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 
Process finished with exit code 0

接下来我们使用递归思想来实现打印 文件目录结构层级的打印,我们还是先用循环来解决问题:

import os
def get_all(path):

    temp = [path]
    while len(temp):
        new_path = temp.pop()
        L = os.listdir(new_path)
        for name in L:
            new_name = os.path.join(new_path, name)
            if os.path.isdir(new_name):
                print('目录:', new_name)
                temp.append(new_name)
            else:
                print('文件:', new_name)


get_all(r'G:\blogworkspace\blogproject\comments')

来看实现结果:
G:\python\python.exe G:/Pyproject/day115/test2.py
文件: G:\blogworkspace\blogproject\comments\admin.py
文件: G:\blogworkspace\blogproject\comments\apps.py
文件: G:\blogworkspace\blogproject\comments\forms.py
目录: G:\blogworkspace\blogproject\comments\migrations
文件: G:\blogworkspace\blogproject\comments\models.py
文件: G:\blogworkspace\blogproject\comments\tests.py
文件: G:\blogworkspace\blogproject\comments\urls.py
文件: G:\blogworkspace\blogproject\comments\views.py
文件: G:\blogworkspace\blogproject\comments\__init__.py
目录: G:\blogworkspace\blogproject\comments\__pycache__
文件: G:\blogworkspace\blogproject\comments\__pycache__\admin.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\forms.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\models.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\urls.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\views.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\__init__.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\migrations\0001_initial.py
文件: G:\blogworkspace\blogproject\comments\migrations\__init__.py
目录: G:\blogworkspace\blogproject\comments\migrations\__pycache__
文件: G:\blogworkspace\blogproject\comments\migrations\__pycache__\0001_initial.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\migrations\__pycache__\__init__.cpython-36.pyc

Process finished with exit code 0

接着我们使用递归来实现:

import os


def get_all(path):
    L = os.listdir(path)

    for name in L:
        new_path = os.path.join(path, name)
        if os.path.isdir(new_path):
            print('目录:', new_path)
            get_all(new_path)
        else:
            print('文件:', new_path)


get_all(r'G:\blogworkspace\blogproject\comments')

打印结果是:
G:\python\python.exe G:/Pyproject/day115/test2.py
文件: G:\blogworkspace\blogproject\comments\admin.py
文件: G:\blogworkspace\blogproject\comments\apps.py
文件: G:\blogworkspace\blogproject\comments\forms.py
目录: G:\blogworkspace\blogproject\comments\migrations
文件: G:\blogworkspace\blogproject\comments\migrations\0001_initial.py
文件: G:\blogworkspace\blogproject\comments\migrations\__init__.py
目录: G:\blogworkspace\blogproject\comments\migrations\__pycache__
文件: G:\blogworkspace\blogproject\comments\migrations\__pycache__\0001_initial.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\migrations\__pycache__\__init__.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\models.py
文件: G:\blogworkspace\blogproject\comments\tests.py
文件: G:\blogworkspace\blogproject\comments\urls.py
文件: G:\blogworkspace\blogproject\comments\views.py
文件: G:\blogworkspace\blogproject\comments\__init__.py
目录: G:\blogworkspace\blogproject\comments\__pycache__
文件: G:\blogworkspace\blogproject\comments\__pycache__\admin.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\forms.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\models.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\urls.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\views.cpython-36.pyc
文件: G:\blogworkspace\blogproject\comments\__pycache__\__init__.cpython-36.pyc

Process finished with exit code 0

最后我们来看一下汉诺塔游戏:

def hanoi(n, x, y, z):
    if n == 1:
        print(x, ' --> ', z)
    else:
        hanoi(n-1, x, z, y) # 将前n-1个盘子从x移动到y上
        print(x, ' --> ', z) # 将最底下的最后一个盘子从x移动到z上
        hanoi(n-1, y, x, z) # 将y上的n-1个盘子移动到z上

n = int(input('请输入汉诺塔的层数:'))
hanoi(n, 'X', 'Y', 'Z')

有x y z三个柱子,箭头指向表示从哪转移哪
我们输入三,看一下执行结果:
请输入汉诺塔的层数:3
X  -->  Z
X  -->  Y
Z  -->  Y
X  -->  Z
Y  -->  X
Y  -->  Z
X  -->  Z

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值