day15 lambda recursive function dichotomy

day15 lambda recursive function dichotomy

 

I. anonymous function lambda
    1.lambda function
def func (n): # ordinary functions, function is relatively simple, when the function more time, named not good to take
    return n*n
print(func(9))
 
a = lambda n: n * n # anonymous function Syntax: the lambda parameter: Return value
print(a(9))
 
print(func.__name__)    #func
print (a .__ name__) # <lambda> name anonymous function is <lambda>, can be considered to be a function name
 
a = 1,2,3 # (1,2,3) a is a tuple
a = (1,2,3,) # (1,2,3) a is a tuple
a = (1,2,3) # (1,2,3) a is a tuple
y = 'other'
a = lambda x, y: x, y # (<function <lambda> at 0x0000011A5A332EA0>, 'other') # This is a time tuple
print(a)
a = lambda x, y: (x, y) # lambda is a function of time, want to return multiple values, the return value in brackets
print(a)
    Features 2.lambda function
1. The parameter may have multiple functions in order, separated by
2. anonymous function returns data (complex with def) after only write a single line, no matter how complex, and a logical ending
3. Return value and normal functions, may be any data type
    3.lambda function exercises
a = lambda *args: max(args)
print(a(12,3243,5,3,3))
 
II. Recursion (recursion)
    Function calls itself recursively default is an infinite loop
    Similar algorithms used for processing
    Memory consumption, can cycle, it is best not to use recursion
i = 0
def func():
    global i
    print('bajie: %s' % i)
    i+=1
    func()
func () # recursion depth, the number of times you can call your own, official (1000) times before this will throw an exception
 
import sys
sys.setrecursionlimit (1000) # Set recursion depth value
print (sys.getrecursionlimit ()) # View recursion depth value
    File folder traversal
import os
filepath = r'C:\Users\THINKPAD\PycharmProjects\s15'
def func(filepath,n):
    files = os.listdir(filepath)
    for file in files:
        file_path = os.path.join(filepath, file)
        if os.path.isdir(file_path):
            print('\t'*n, file+':')
            func (file_path, n + 1) # Why use recursion, the same first operation, the second circulation times uncertain
        else:
            print('\t'*n, file)
 
func(filepath,0)
    Binary search
        Dichotomy to find each half of the data can be ruled out. Find a very high efficiency
        Requirements: must find a sequence is an ordered sequence
        Primeval dichotomy:
lst = [1,2,4,5,9,21,23,34,35,56,87,123,231,345,678,999]
n = 35
 
for i in lst: # # Find the maximum traversal time complexity o (n)
    if i == n:
        print('found')
        break
else:
    print('not found')
 
left = 0
right = len(lst)-1
while left <= right:            #使用二分法可以提高效率(有序的才能用这种方法)(一次砍一半)
    middle = (left + right)//2  #这里必须是整除
    if lst[middle] > n:         #2**n < 数据量;    比如1亿个数, 27次就可以找到
        right = middle - 1
    if lst[middle] < n:
        left = middle + 1
    if lst[middle] == n:
        print('found')
        break
else:
    print('not found')
        递归可以完成二分法
lst = [1,2,4,5,9,21,23,34,35,56,87,123,231,345,678,999]
def func(n,left,right):
    if left <= right:                   #为啥不用while, 因为用了递归
        middle = (left + right)//2
        if n > lst[middle]:
            left = middle + 1
            return func(n, left, right)     #递归
        if n < lst[middle]:
            right = middle - 1
            return func(n, left, right)     #递归    #返回值的问题: 如果递归了很多层, 最后一层得到结果,返回给倒数第二层, 就完事了. 如何一层层返回: return 倒数第二层给倒数第三次, 依次类推直到返回给第一层.
        if n == lst[middle]:
            print('found')
            return middle              #通过return返回, 不能用break
    else:
        print('not found')
        return -1                      #1.模仿find找不到返回 -1(一般index是整数); 2. -1 比 None好运算,可能会用到
rst = func(87, 0, len(lst)-1)
print(rst)
    
三.查找最快的方案
lst1 = [2,3,5,6,8]
lst2 = [0 for i in range(max(lst1)+1)]      #找到列表中最大的数, 作为都是 0 的新列表的长度
 
for el in lst1:                             #把数字变成index
    lst2[el] = 1
n = 1
if lst2[n] == 1:                            #优点o(1)   时间复杂度, 空间复杂度最低   
    print('it is in')
else:
    print('it not in')
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/aiaii/p/11909793.html