Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Nearest value with min solution in Clear category for Nearest Value by pavel.fasther
def nearest_value(values: set[int], value: int) -> int:
"""
Return the nearest value to value from values.
If two values are equidistant, return the smaller value.
"""
if value in values:
return value
return min(values, key=lambda x: (abs(x - value), x))
July 14, 2023