def interpolate(profile, x):
    """Interpolate y given x and a [(x: int, y: int), ...] profile.

  Requires the profile to be sorted by x, with no duplicate x values (see
  normalize).  Expects profiles with integer x and y values, and
  returns duty rounded to the nearest integer.

  >>> interpolate([(20, 50), (50, 70), (60, 100)], 33)
  59
  >>> interpolate([(20, 50), (50, 70)], 19)
  50
  >>> interpolate([(20, 50), (50, 70)], 51)
  70
  >>> interpolate([(20, 50)], 20)
  50
  """
    lower, upper = profile[0], profile[-1]
    for step in profile:
        if step[0] <= x:
            lower = step
        if step[0] >= x:
            upper = step
            break
    if lower[0] == upper[0]:
        return lower[1]
    return round(lower[1] + (x - lower[0]) / (upper[0] - lower[0]) * (upper
        [1] - lower[1]))
