Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Changing direction by mortonfox
def changing_direction(elements: list) -> int:
dir = count = 0
for a, b in zip(elements, elements[1:]):
dir2 = a - b
if dir2:
if dir2 * dir < 0: count += 1
dir = dir2
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!")
Sept. 9, 2022
Comments: