Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Easy ifs solution in Clear category for Autopilot by BootzenKatzen
def speed_adjust(A: int, B: int, C: int) -> int:
"""
--------------------------------------------------------------------------------------------------
function determines if car B needs to speed up (1), slow down (-1), or maintain current speed (0)
--------------------------------------------------------------------------------------------------
First we find the distances between the cars by subtracting the car behind from the car ahead
-B is ahead of A, so B-A, and C is ahead of B, so C-B
Then we use simple logic to determine what our middle car needs to do, and return the appropriate result
-If the distance between the cars is the same, we maintain the current speed to stay even (0)
-If the distance to the back car is greater than the one to the front, we need to slow down (-1)
-If the distance to the back car is smaller than the one to the front, we need to speed up (1)
"""
behind = B - A
ahead = C - B
if behind == ahead:
return 0
elif behind > ahead:
return -1
elif ahead > behind:
return 1
print("Example:")
print(speed_adjust(2, 4, 6))
# These "asserts" are used for self-checking
assert speed_adjust(0, 2, 6) == 1
assert speed_adjust(0, 4, 6) == -1
assert speed_adjust(0, 3, 6) == 0
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 26, 2024
Comments: