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

Questions about programming?Chat with your personal AI assistant