def extrema(seq):
    """Returns the (locs, vals) of the extrema of the given `seq`.
    An extrema is defined as a point which is greater or smaller than both of
    its neighbors. Both endpoints of the `seq` are always extrema."""
    locs = []
    vals = []
    for i, v in enumerate(seq):
        if i == 0 or i == len(seq)-1:
            locs.append(i)
            vals.append(v)
            continue
        prev = seq[i-1]
        next = seq[i+1]
        if (v < prev and v < next) or (v > prev and v > next):
            locs.append(i)
            vals.append(v)
            continue
    return locs, vals