Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Changing direction by book1978
def changing_direction(e: list) -> int:
# variant 1
# counter, posl = 0, None
# posl = None
# for i in range(len(e) - 1):
# if e[i + 1] - e[i]:
# tek = (e[i + 1] - e[i]) > 0
# if posl != None and posl != tek:
# counter += 1
# posl = tek
# return counter
#
# d = [x - y for x, y in zip(e, e[1:]) if x != y]
# return sum(x * y < 0 for x, y in zip(d, d[1:]))
# variant 2
d = [x - y for x, y in zip(e, e[1:]) if x != y]
return sum(x * y < 0 for x, y in zip(d, d[1:]))
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. 29, 2022
Comments: