Cody Bable

Generation

generate functionTue, 23 May 2023

create a function of a set of integers and a value for which you need to find the nearest one.

def find_nearest(numbers, target): numbers.sort() i = 0 j = len(numbers) - 1 while i < j: mid = (i + j) // 2 if numbers[mid] == target: return mid elif numbers[mid] > target: j = mid else: i = mid + 1 return (i + j) // 2 find_nearest([1, 2, 3, 4, 5, 6, 7, 10, 15], 3)

Questions about programming?Chat with your personal AI assistant