Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
pairwise, groupby, xor solution in Clear category for Changing direction by Sim0000
from itertools import pairwise, groupby
def changing_direction(elements: list) -> int:
count = 0
dir = None
for e1, e2 in pairwise(k for k, _ in groupby(elements)):
if dir is None: dir = e1 < e2
if dir ^ (e1 < e2):
dir = not dir
count += 1
return count
print("Example:")
print(changing_direction([1, 2, 3, 4, 5]))
assert changing_direction([1, 2, 3, 4, 5]) == 0
assert changing_direction([1, 2, 3, 2, 1]) == 1
assert changing_direction([1, 2, 2, 1, 2, 2]) == 2
print("The mission is done! Click 'Check Solution' to earn rewards!")
Aug. 20, 2022