Python learning ---- dichotomy and anonymous functions

First, the dichotomy

Algorithm: is a highly efficient solution to the problem
dichotomy Algorithms

Demand: There is a list in numerical order from small to large of
the need to find a number that we want from the list of numbers of
how to do more efficiently? ? ?

nums=[-3,4,7,10,13,21,43,77,89]
find_num=10

nums=[-3,4,13,10,-2,7,89]
nums.sort()
print(nums)

Option One: The overall efficiency is too low to traverse

or num in nums:
    if num == find_num:
        print('find it')
        break

Option Two: dichotomy

def binary_search(find_num,列表):
    mid_val=找列表中间的值
    if find_num > mid_val:

The next lookup should be the right half of the list

List = list slice right half

    binary_search(find_num,列表)
    elif find_num < mid_val:

The next lookup should be the left half of the list

List = list slice the left half

   binary_search(find_num,列表)
    else:
        print('find it')

nums=[-3,4,7,10,13,21,43,77,89]
find_num=8
def binary_search(find_num,l):
    print(l)
    if len(l) == 0:
        print('找的值不存在')
        return
    mid_index=len(l) // 2

​    if find_num > l[mid_index]:

The next lookup should be the right half of the list

  l=l[mid_index+1:]
        binary_search(find_num,l)
    elif find_num < l[mid_index]:

The next lookup should be the left half of the list

 l=l[:mid_index]
        binary_search(find_num,l)
    else:
        print('find it')

binary_search(find_num,nums)

1, def for defining a named function

func=函数的内存地址
def func(x,y):
    return x+y

print(func)

2, lamdab function for defining anonymous

print(lambda x,y:x+y)

3, call the anonymous function

method one:

res=(lambda x,y:x+y)(1,2)
print(res)

Second way:

func=lambda x,y:x+y
res=func(1,2)
print(res)

4, anonymous call for a temporary scene: more anonymous is used in conjunction with other functions

Programming ideas / paradigm

Process-oriented programming ideas:
the core is the "process" the word, that is, the process flow, referring to the steps of doing things: what first, and then what, after doing
based on the idea to write a program like the design of a pipeline

Advantages: the problem of complex processes, and further simplification of
disadvantages: very poor scalability

Process-oriented programming ideas scenario analysis:
1, not all software requires frequent changes: for example, write a script
2, even if a software requires frequent changes, nor does not mean that all the software components of the change are required together

Guess you like

Origin www.cnblogs.com/x945669/p/12578595.html