Quantcast
Viewing all articles
Browse latest Browse all 14271

Need help in implementing a Binary search type algorithm

Have a list i.e nums=[1,5,9,1,5,9], i want to get the possible pairs with in an abs difference of valuediff. My intention is to traverse through each element within the sorted list, split the list into left & right parts barring the element & pass the list into a binary search kind of alog, where the last & the 1st elem of list is to be checked & final get to a point where all the elements are within the range of abs difference. Request you to please help me with the remaining part of the code :

def binary_search(array_list,num,index_pos,valuediff):    if abs(num-array_list[0])>valuediff:        return    elif  abs(num-array_list[-1])<valuediff:        return array_list    elif abs(num-array_list[0])<=valuediff and abs(num-array_list[-1])>valuediff:        check_iteration=True        left, right = 0, len(array_list) - 1        while(check_iteration):            midpoint=left + (right - left) // 2            if abs(array_list[midpoint]-num)<=valuediff and abs(array_list[midpoint+1]-num)<=valuediff:    while left <= right:        mid = left + (right - left) // 2        if            pass        else:            passdef find_pairs(nums1,index_pos,num):    leftside_array=[]    rightside_array=[]    if index_pos==0 :        leftside_array=[]        try:            rightside_array=nums1[index_pos+1:]        except:            pass    else:        try:            leftside_array=nums1[:index_pos]        except:            pass        try:            rightside_array = nums1[index_pos + 1:]        except:            pass    print(index_pos,leftside_array+rightside_array,valuediff)    binary_search(leftside_array, num)    possible_pairs=[]if __name__=="__main__":    nums=[1,5,9,1,5,9]    nums1=sorted(nums)    possible_pairs=[]    valuediff=3    for index,num in enumerate(nums1):        find_pairs(nums1,index,num)

Viewing all articles
Browse latest Browse all 14271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>