Cody Bable

Generation

generate functionTue, 23 May 2023

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

def closest(lst, K): lst = sorted(lst) return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))] # Driver code lst = [1,2,3,4,8] K = 2 print(closest(lst, K))

Questions about programming?Chat with your personal AI assistant