Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- // Complete the kangaroo function below.
- func kangaroo(x1: Int, v1: Int, x2: Int, v2: Int) -> String {
- var currentLocation1 = x1
- var currentLocation2 = x2
- var (differenceInLocations, greaterOne): (Int, Int) = {
- if x1 < x2 {
- return (x2 - x1, x2)
- } else {
- return (x1 - x2, x1)
- }
- }()
- if greaterOne == x1 {
- if v1 > v2 {
- return "NO"
- }
- } else {
- if v2 > v1 {
- return "NO"
- }
- }
- while(differenceInLocations >= 0) {
- if differenceInLocations == 0 {
- return "YES"
- }
- currentLocation1 = currentLocation1 + v1
- currentLocation2 = currentLocation2 + v2
- if greaterOne == x1 {
- if differenceInLocations > currentLocation1 - currentLocation2 {
- differenceInLocations = currentLocation1 - currentLocation2
- } else {
- return "NO"
- }
- } else if greaterOne == x2 {
- if differenceInLocations > currentLocation2 - currentLocation1 {
- differenceInLocations = currentLocation2 - currentLocation1
- } else {
- return "NO"
- }
- }
- }
- return "NO"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement