• 递归函数 二分查找


    递归函数:
    自己调用自己

    def func1():
        print(666)
        func1()  #自己调用自己
    func1()
    View Code

    递归到一定深度,就会主动终止,默认最大递归深度998(python解释器给终止的)

    count = 0
    def func1(n):
        n += 1
        print(n)
        func1(n)
    func1(count)
    View Code

    设置递归深度:

    count = 0
    def func1(n):
        n += 1
        print(n)
        func1(n)
    func1(count)
    View Code

    递归函数例子;

    '''
    n = 1  太白  age(1)= 23
    n = 2  日天   age(2)= age(1) + 2
    n = 3  wusir  age(3)= age(2) + 2
    n = 4  alex   age(4)= age(3) + 2
    '''
    def age(n):
        if n == 1:
            return 23
        else:
            return age(n-1) + 2      # age(4) = age(3) + 2
    print(age(4))           #23+2+2+2
    View Code
    二分查找:
    用代码写的计算方式就是算法。
    二分查找是最简单的算法,比较经典的算法。
    前提:数字序列,有序,不重复。
    查找66索引:
    l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
    # print(l.index(66))
    count = 0
    for i in l:
        if i == 66:
    
    
            print(count)
        count += 1
    查找66索引
    for i in range(len(l)):
        if l[i] == 66:
            print(i)
            break
    else:
        print('找不到..')
    l1 = [1,3,5,7,8,10,11]
    View Code
    def index(l,aim):
        count = -1
        for i in l:
            count += 1
            if i == aim:
                return count
        else:
            return '没有此值'
    print(index(l,66))
    View Code

    二分查找算法:

    l1 = [2, 3, 5, 10, 15, 16, 18, 19]
    def two_find(l,aim):
        mid_index = len(l) // 2
        if aim > l[mid_index]:
            return two_find(l[mid_index+1:],aim)
        elif aim < l[mid_index]:
            return two_find(l[:mid_index], aim)
        elif aim == l[mid_index]:
            return mid_index
        else:
            return None
    print(two_find(l1,18))
    print(two_find(l1,3))
    print(two_find(l1,10))
    第一版 原列表发生改变,导致索引改变.
    l1 = [2, 3, 5, 10, 15, 16, 18]
    
    def two_find(l,aim,start=0,end=None):
        end = len(l) - 1 if end is None else end
        mid_index = (end - start) // 2 + start
        '''
            第一次: aim:15 start: 0  end: 6  min_index: 3  中间值:10  aim > 10
            第二次: aim:15 start: 4  end: 6  min_index: 5  中间值:16  aim < 16
            第三次: aim:15 start: 4  end: 5  min_index: 4  中间值:15  aim = 15
            '''
        if start <= end:
            if aim > l[mid_index]:
                return two_find(l, aim, start=mid_index+1, end=end)
            elif aim < l[mid_index]:
                return two_find(l, aim, start=start, end=mid_index)
            elif aim == l[mid_index]:
                return mid_index
            else:
                return None
        else:
            return None
    print(two_find(l1,15))
    第二版 原列表不发生改变,你的min_index = (end - start) // 2 + start
    l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
    def func(l,aim):
        mid = (len(l)-1)//2
        if l:
            if aim > l[mid]:
                func(l[mid+1:],aim)
            elif aim < l[mid]:
                func(l[:mid],aim)
            elif aim == l[mid]:
                print("bingo",mid)
        else:
            print('找不到')
    func(l,66)
    func(l,6)
    View Code
    升级版二分法:
    l1 = [1, 2, 4, 5, 7, 9]
    def two_search(l,aim,start=0,end=None):
        end = len(l)-1 if end is None else end
        mid_index = (end - start) // 2 + start
        if end >= start:
            if aim > l[mid_index]:
                return two_search(l,aim,start=mid_index+1,end=end)
    
            elif aim < l[mid_index]:
                return two_search(l,aim,start=start,end=mid_index-1)
    
            elif aim == l[mid_index]:
                return mid_index
            else:
                return '没有此值'
        else:
            return '没有此值'
    print(two_search(l1,9))
    View Code
  • 相关阅读:
    获取网络上的北京时间,如果大于设定的过期时间就...
    MYSQL注释
    mysql的perror
    Spring + CXF(REST):webservice not found
    vim 学习笔记
    mysql存储过程controller的not found造成混乱的解决办法
    pt-query-digest 安装及使用
    MYSQL预处理传参不区分大小写解决办法
    解压版mysql安装--windows系统
    sql plus 和 pl/sql无法连接远程oracle数据库
  • 原文地址:https://www.cnblogs.com/ls13691357174/p/9213319.html
Copyright © 2020-2023  润新知