def cluster(data, maxgap):
  """
  This function is from https://stackoverflow.com/a/14783998,
    which arranges data into groups where successive elements
    differ by no more than *maxgap*.
  """
  data.sort()
  groups = [[data[0]]]
  for x in data[1:]:
      if abs(x - groups[-1][-1]) <= maxgap:
          groups[-1].append(x)
      else:
          groups.append([x])
  return groups