Nearest shim thickness
Your task
A machine shop stocks shim washers in a drawer, one entry per stocked thickness in microns, and thicknesses arrives sorted ascending. A thickness can be listed more than once. A machinist measuring a gap of target_um will fit the stocked thickness nearest to that gap, from either side of it. Given thicknesses and target_um, return that thickness itself, not its position in the drawer. When two stocked thicknesses sit exactly the same distance from the gap, return the thinner of the two, since a shim that is a little thin can be doubled up and one that is too thick cannot be shaved. Return -1 when the drawer is empty. Thicknesses are positive, and the drawer is large enough that reading every entry per lookup is too slow.
Examples
nearest_shim_thickness(thicknesses: list[int], target_um: int) -> intExample 1
Input
nearest_shim_thickness([50, 100, 250, 400], 180)Output
250Example 2
Input
nearest_shim_thickness([50, 100, 250, 400], 175)Output
100Example 3
Input
nearest_shim_thickness([], 90)Output
-1