• 递归函数


    import sys
    # sys.setrecursionlimit(1200)  # 修改递归层数限制
    
    count = 0
    def func():
         global count
         count += 1
         print(count)
         func()
         print(456)
    
    func()
    
    
    # RecursionError:递归错误
    # 官网规定的递归的最大深度1000层:为了节省内存空间,不让用户无限使用内存空间
    # 1.递归要尽量控制次数,如果需要很多层递归才能解决问题,不适合递归解决
    # 2.循环和递归的关系:
            # 递归不是万能的
            # 递归比起循环来说更占用内存
    
    
    # 你的递归函数 必须要停下来
    count = 0
    def func():
         global count
         count += 1
         print(count)
         if count == 3:
             return
         func()
         print(456)
    
    
    func()
    
    
    # 函数的调用
    # 函数的参数
    # 函数的返回值
    
    # 一个递归函数要想结束,必须在函数内写一个return,并且return的条件必须是可达到的
    
    
    def func(count):
         count += 1
         # print(count)
         if count == 5:
             return 5
         ret = func(count)
         return ret
    
    r = func(1)
    print(r)

    递归函数相关习题:

    # # 1.计算阶乘  5! = 5*4*3*2*1
    # # 循环:
    # r = 1
    # for i in range(5, 0, -1):
    #     r *= i
    # print(r)
    #
    # # 递归:
    # def func(n):
    #
    #     if n == 1:
    #         return 1
    #     ret = n * func(n - 1)
    #     return ret
    #
    #
    # print(func(5))
    
    
    # 2.os模块;查看一个文件夹下的所有文件,这个文件下面还有文件夹
    import os
    
    # dir_path = 'F:Lnh_Studylnh_01_基础部分lnh_14_模块的多种导入方式'
    # dir_ls = os.listdir('F:Lnh_Studylnh_01_基础部分lnh_14_模块的多种导入方式')
    # print(dir_ls)
    #
    # count = len(dir_ls)
    # def func(path, dir_ls):
    #     global count
    #     if count == 0:
    #         return
    #     for name in dir_ls:
    #         count -= 1
    #         stats = os.path.isdir(path + '/'+ name)
    #
    #         if stats and name != '__pycache__':
    #             new_path = path + '/' + name
    #             print(new_path)
    #             new_ls = os.listdir(new_path)
    #             print(new_ls)
    #             func(new_path, new_ls)
    #
    #
    # func(dir_path, dir_ls)
    
    
    # 3.os模块;计算一个文件夹下所有文件的大小,这个文件下面还有文件夹,不能用walk
    # dir_path = 'F:Lnh_Studylnh_01_基础部分lnh_14_模块的多种导入方式'
    # dir_ls = os.listdir(dir_path)
    # print(dir_ls)
    # def func(path, dir_ls):
    #     file_size = 0
    #     for name in dir_ls:
    #         stats = os.path.isdir(os.path.join(path, name))
    #         if stats:
    #             new_path = os.path.join(path, name)
    #             print(new_path)
    #             new_ls = os.listdir(new_path)
    #             print(new_ls)
    #             ret = func(new_path, new_ls)
    #             file_size += ret
    #         else:
    #             file_size += os.path.getsize(os.path.join(path, name))
    #
    #     return file_size
    #
    # ret = func(dir_path, dir_ls)
    # print(f'文件夹大小:{ret}字节')
    
    
    # 4.计算斐波那契数列  找第100个数
    # 循环:1 1 2 3 5 8 13 21
    def func(n):
        a = 1
        b = 1
        for i in range(n):
            yield a
            a, b = b, a+b
    obj = func(2)
    print(list(obj))
    
    
    # 递归:
    def func(n):
        n -= 1
        if n == 0:
            return 1, 1
    
        ret = func(n)
        return ret[1], (sum(ret))
    
    ret = func(100)
    print(ret[0])
    
    
    
    # 5.三级菜单 可能是n级...
    # menu = {
    #     '北京': {
    #         '海淀': {
    #             '五道口': {
    #                 'soho': {},
    #                 '网易': {},
    #                 'google': {}
    #             },
    #             '中关村': {
    #                 '爱奇艺': {},
    #                 '汽车之家': {},
    #                 'youku': {},
    #             },
    #             '上地': {
    #                 '百度': {},
    #             },
    #         },
    #         '昌平': {
    #             '沙河': {
    #                 '老男孩': {},
    #                 '北航': {},
    #             },
    #             '天通苑': {},
    #             '回龙观': {},
    #         },
    #         '朝阳': {},
    #         '东城': {},
    #     },
    #     '上海': {
    #         '闵行': {
    #             "人民广场": {
    #                 '炸鸡店': {}
    #             }
    #         },
    #         '闸北': {
    #             '火车战': {
    #                 '携程': {}
    #             }
    #         },
    #         '浦东': {},
    #     },
    #     '山东': {},
    # }
    #
    # def threeLM(dic):
    #     while True:
    #         for k in dic:
    #             print(k)
    #         key = input('input>>').strip()
    #         if key == 'b' or key == 'q':
    #             return key
    #         elif key in dic.keys() and dic[key]:
    #             ret = threeLM(dic[key])
    #             if ret == 'q':
    #                 return 'q'
    #
    # threeLM(menu)
  • 相关阅读:
    自定义View的ToolBar布局报错Error:(2) No resource identifier found for attribute 'context' in package 'c
    在学git之主分支 branch
    获取发布版SHA1
    关于开启线程与UI的操作
    播放音频和视频(VideoView控件)
    通知栏Notification的应用
    Android 真机调式 Installation failed with message 远程主机强迫关闭了一个现有的连接。. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing. WA
    运行程序申请危险权限
    mysql乐观锁总结和实践
    Nginx配置文件nginx.conf中文详解
  • 原文地址:https://www.cnblogs.com/GOD-L/p/13528727.html
Copyright © 2020-2023  润新知