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.

Apple and Orange

I have been applying to different internships lately and have been referred to hackerrank through many of them so I decided why not add and talk about my solutions to some of the challenges.

This problem is called Apple and Orange and the whole point is to print the number of apples and oranges that fall from Sam’s house.

Here is the problem:

appleOrange

#!/bin/python3

import sys

#given

s,t = input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = input().strip().split(' ')
a,b = [int(a),int(b)]
m,n = input().strip().split(' ')
m,n = [int(m),int(n)]
apple = [int(apple_temp) for apple_temp in input().strip().split(' ')]
orange = [int(orange_temp) for orange_temp in input().strip().split(' ')]

#my code

count = 0
for ap in apple:
     temp = a+ap
     if temp <= t and temp >=s:
         count = count + 1 
print (count)

count = 0
for ora in orange:
    temp = b+ora
    if temp <= t and temp >=s:
         count = count + 1 
print (count)

This problem is fairly simple I used two for loops since the array size for apple and orange can differ and I had to print the total apple and total orange.