Kangaroo

This is another easy Hackerrank question, Kangaroo.

This problem takes a high school math problem of will one car ever catch another, but instead of cars we are talking kangaroos

kangaroo

def kangaroo(x1, v1, x2, v2):
 if x1 > x2:
    temp = x1
    x1 = x2
    x2 = temp
    temp = v1
    v1 = v2
    v2 = temp 
 
 if v1 <= v2:
    return "NO"
 
 while(x1 < x2):
    x1+=v1
    x2+=v2
 if(x1 == x2):
   return "YES"
 else:
   return "NO"

#given
x1, v1, x2, v2 = input().strip().split(' ')
x1, v1, x2, v2 = [int(x1), int(v1), int(x2), int(v2)]
result = kangaroo(x1, v1, x2, v2)
print(result)

For this problem I set x2 to always be further than x1 by creating a swap condition. After I check if the kangaroo at x1 has a velocity higher than kangaroo at x2 if it isn’t they will never meet. Finally I cycle through until x1 is no longer less than x2.