0518Python Basics-built-in functions-two points find

Source: Internet
Author: User
Tags iterable

1. Yesterday's content review

Len () Number of tests

Eval () removes the quotation marks from the string, returning the internal result

Eval (' + + ')---> 2

EXEC () Remove the quotation marks from the string and execute the internal code

RET = "If True:

Print (666)

‘‘‘

EXEC (ret)

Range

Next ()

ITER ()

Reversed () returns an iterator

Sorted (iterable,key,reverse) return list key

Zip (Iter1,iter2,iter3 ...) Zipper method iterator

Map (key,iterable) loop mode.     [I-I in iterable] returns an iterator. Key

Filter (key,iterable) filtering mode [i-I in iterable if ...] returns an iterator. Key

MIN () minimum value, which returns int str. Key

SUM (ITERABLE,ARGV) sum.

Max () max value.    The return is int str. Key

Open () file operation

Repr the true colours.

Divmod (dividend, divisor) return value (quotient, remainder)

Dir ()

ABS () absolute value.

Callable

Bytes () str---> bytes

Locals () The local variable at the current position.

Globals () global variable.

2. Recursive function

Recursive functions: The function itself is called in a function. themselves call themselves

Maximum recursion depth: 998

Modify the default recursion depth

Import Sys

Sys.setrecursionlimit (100000)

The maximum depth of recursion can be modified in this way, and just as we set the recursive depth allowed by Python to 10w, the actual depth that can be reached depends on the performance of the computer. However, it is still not recommended to modify this default recursion depth, because if the 997-layer recursion is not solved the problem is either not suitable for the use of recursive resolution or you write code is too bad ~ ~ ~

def foo (n):

Print (n)

n + = 1

Foo (n)

Foo (1)

‘‘‘

n = 1 Taibai age (1) = 23

n = 2nd days 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

Print (age (4)) # 23 + 2 + 2 + 2

"""

def age (4):

if n = = 1:

return 23

Else

Return Age (N-1) + 2 Age (4) = Age (3) + 2

RET = Age (4)

def age (3):

if n = = 1:

return 23

Else

Return Age (N-1) + 2 Age (3) = Age (2) + 2

def age (2):

if n = = 1:

return 23

Else

return Age (1) + 2 Age (2) = Age (1) + 2

def age (1):

if n = = 1:

Return to age (1) = 23

Else

return Age (1) + 2

"""

3, two-point search

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]

Do you observe this list, which is not an ordered list of small to large sort?

If that's the case, if I'm looking for a larger number than the middle of the list, am I just going to look in the back half of the list?

This is the binary search algorithm!

So how do we implement it in the code?

Simple version of Binary method

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

For I in range (Len (l)):

If l[i] = = 47:

Print (i)

Break

Else

Print (' Can't find .... ')

‘‘‘

Target value: Aim = 66

Looking for intermediate index: Min_index = Len (l)//2

Aim is compared with the value of the intermediate index

Aim > L[min_index]:

L[min_index+1:]

Aim < L[min_index]:

L[:MIN_INDEX-1]

Aim = = L[min_index]

Return Min_index

‘‘‘

Two-way version of the upgrade

L1 = [1, 3, 5, 7, 8, 10, 11]

def binary_search (Li,aim,start=0,end=none): # First time: [1, 3, 5, 7, 8, ten, one] aim 6,start 0,end 6

Second time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:0,end:3

Third time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:2,end:3

Fourth time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:3,end:3

Fifth time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:3,end:3

end = Len (LI) If End is None else end

Mid_index = (End-start)//2 + start # First Mid 3 second time: Mid 1 third time: Mid:2 fourth time: Mid:3

If Start <= end:

If aim > Li[mid_index]:

Return Binary_search (Li, Aim, start=mid_index+1, End=end) # second time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:2 End:3

Third time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:3 End:3

Elif Aim < Li[mid_index]:

Return Binary_search (Li, Aim, start=start, end = Mid_index) # first time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:0,end:3

Fourth time: [1, 3, 5, 7, 8, ten, one] aim 6 Start:3,end:3

elif aim = = Li[mid_index]:

Return Mid_index

Else

Return None

Print (Binary_search (l1,3))

Print (Binary_search (l1,11))

0518Python Basics-built-in functions-two points find

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.