Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
groups of signed diffs solution in Creative category for Changing direction by Phil15
from itertools import groupby, pairwise, starmap
from operator import sub
from typing import Iterable
def changing_direction(directions: Iterable[int]) -> int:
nonzero_diffs = filter(None, starmap(sub, pairwise(directions)))
signed_groups = groupby(nonzero_diffs, key=lambda x: x > 0)
nb_signs = sum(1 for _ in signed_groups)
nb_changes = max(0, nb_signs - 1)
return nb_changes
Aug. 19, 2022
Comments: