Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Creating a list with changes in direction solution in Clear category for Changing direction by H0r4c3
def decide_order(elements: list[int]) -> list[str]:
'''
Create a list with changes in direction
'''
order_list = list()
for i in range((len(elements) - 1)):
if elements[i] < elements[i+1]:
order = 'asc'
order_list.append(order)
elif elements[i] > elements[i+1]:
order = 'desc'
order_list.append(order)
else:
order = ''
order_list.append(order)
order_list = [item for item in order_list if item != '']
print(order_list)
return(order_list)
def changing_direction(elements: list[int]) -> int:
result = 0
order_list = decide_order(elements)
for i in range((len(order_list) - 1)):
if order_list[i] != order_list[i+1]:
result += 1
print(result)
return result
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!")
Nov. 29, 2022
Comments: