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 nearest_value(values: set, one: int) -> int: lst = list(values) lst.sort() if one <= lst[0]: return lst[0] if one >= lst[-1]: return lst[-1] for i in range(len(lst)-1): if lst[i] < one < lst[i+1]: if one - lst[i] > lst[i+1] - one: return lst[i+1] else: return lst[i]

Questions about programming?Chat with your personal AI assistant