Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Changing direction by freeman_lex
def changing_direction(elements: list) -> int:
dirs = []
for i, j in zip(elements, elements[1:]):
if j > i and (not dirs or dirs[-1] == '-'):
dirs.append('+')
elif j < i and (not dirs or dirs[-1] == '+'):
dirs.append('-')
return len(dirs)- bool(dirs)
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. 19, 2022
Comments: