To code bilinear interpolation in python.
Implement bilinear_interpolate(grid, x, y), where grid is a rectangular 2D list, x is the horizontal coordinate, and y is the vertical coordinate. Return the interpolated numeric value using the four surrounding grid samples. Coordinates may lie on grid boundaries; clamp them to the valid grid range before selecting neighboring samples.
grid contains numeric values indexed as grid[row][column]. The function receives x and y as numbers and returns a float.
def bilinear_interpolate(grid, x, y):