Home > other >  Python recursive functions to solve!!!!!!
Python recursive functions to solve!!!!!!

Time:10-08


1, write a recursive function, list the output minimum cumulative,
For example: m=,3,2,1,3 [2] the final return value for,2,2,1,1 [2]

2, how to write dichotomy in recursive function to find out the location of the elements in the list?

Thank you very much!!!!!!

CodePudding user response:

1
 list_in=[2, 3, 2, 1, 3] 
List_out=[]
Dd=float (" inf ")

Def get_min (list_in, list_out list_min) :
Mm=min (list_in [0], list_min)
List_out. Append (mm)
Print (list_out)
If len (list_in) & gt; 1:
Get_min (list_in [1], list_out, mm)

Get_min (list_in list_out, dd)

CodePudding user response:

The second question

 list_in=[2, 4, 6, 8, 10] 
Value=https://bbs.csdn.net/topics/5

Def binary_search (list_in, index_low index_high, value) :
If index_low==index_high:
Return index_low if list_in [index_low]==value else - 1

Index_mid=int ((index_low + index_high)/2)
If list_in [index_mid]==value: # is equal to the median
Return index_mid
Elif list_in [index_mid] Return binary_search (list_in index_mid + 1, index_high, value)
The else: # is less than the median
Return binary_search (list_in, index_low index_mid, value)

Print (binary_search (list_in, 0, len (list_in) - 1, value))


O

1
[Finished in 0.1 s]
  • Related