Recursive Functions
: Call yourself yourself
Import SYS
Sys.setrecursionlimit (10000)
def func1 ():
print (666)
func1 ()
func1 ()
default maximum recursive depth 998
Import SYS
Sys.setrecursionlimit (10000)
count=0
def func1 (n):
n+=1
print (n)
func1 (n)
func1 (count)
" "
n = 1 Taibai age (1) =
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
Else:
return Age (n-1) +2
Print (age (4)) #23 +2+2+2
The following is the specific operation of the recursive function
def Age (n): #n =4
if n==1:
return
Else:
return Age (n-1) +2
Ret=age (4)
" "
def Age (N): # n=3
if n==1:
return
Else:
return Age (1) +2
def Age (n): #n =2
if n==1:
return
Else:
return Age (1) +2
def Age (): #$4$$$$
if n==1:
return
Else:
return Age (1) +2
" "
Two-point search:
the way in which the code is written.
binary Search is the simplest algorithm, compared with the classical algorithm.
1, number sequence, ordered, not duplicated.
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 (Len (l))
print (L.index ())
count=0
For i in L:
If I ==66:
print (count)
count+=1
For i in range (Len (l)):
if l[i]==66:
print (i)
Break
Else:
print (' not found ')
target value: Aim = $
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
L1 = [1, 3, 5, 7, 8, ten, one]
def binary_search (l1,aim):
Mid_index=len (L1)//2
if Aim >l1[mid_index]:
return Binary_search (L1[mid_index+1:],aim)
elif Aim<l1[mid_index]:
return Binary_search (L1[:mid_index],aim)
elif Aim==l1[mid_index]:
return Mid_index
Else:
return None
Print (Binary_search (l1,5))
Li = [1, 3, 5, 7, 8, ten, one]
def func (li,aim):
Mid_index=len (LI)//2
if Aim>li[mid_index]:
return func (Li[mid_index+1:],aim)
elif Aim<li[mid_index]:
return func (Li[:mid_index], aim)
elif Aim==li[mid_index]:
return Mid_index
Else:
return None
Print (func (li,10))
Li = [1, 3, 5, 7, 8, ten, one]
def binary_search (li,aim,start=0,end=none): #$$$$$$$$
End=len (LI)-1 if End is None else end
mid_index= (end-start)//2+start
if Start<=end:
if Aim >li[mid_index]:
return Binary_search (li,aim,start=mid_index+1,end=end)
elif Aim<li[mid_index]:
return Binary_search (li,aim,start=start,end=mid_index)
elif Aim==li[mid_index]:
return Mid_index
Else:
return None
Else:
return None
Print (Binary_search (li,17))
Python-----Recursive function, two-point lookup