Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.35 KB | None | 0 0
  1. import Foundation
  2.  
  3. // Complete the kangaroo function below.
  4. func kangaroo(x1: Int, v1: Int, x2: Int, v2: Int) -> String {
  5.     var currentLocation1 = x1
  6.     var currentLocation2 = x2
  7.     var (differenceInLocations, greaterOne): (Int, Int) = {
  8.         if x1 < x2 {
  9.             return (x2 - x1, 2)
  10.         } else {
  11.             return (x1 - x2, 1)
  12.         }
  13.     }()
  14.     if greaterOne == 1 {
  15.         if v1 > v2 {
  16.             return "NO"
  17.         }
  18.     } else if greaterOne == 2 {
  19.         if v2 > v1 {
  20.             return "NO"
  21.         }
  22.     }
  23.     while(true) {
  24.         currentLocation1 = currentLocation1 + v1
  25.         currentLocation2 = currentLocation2 + v2
  26.         if differenceInLocations == 0 {
  27.             return "YES"
  28.         }
  29.         if greaterOne == 1 {
  30.             if differenceInLocations > currentLocation1 - currentLocation2  && currentLocation1 - currentLocation2 >= 0 {
  31.                 differenceInLocations = currentLocation1 - currentLocation2
  32.             } else {
  33.                 return "NO"
  34.             }
  35.         } else if greaterOne == 2 {
  36.             if differenceInLocations > currentLocation2 - currentLocation1 && currentLocation2 - currentLocation1 >= 0{
  37.                 differenceInLocations = currentLocation2 - currentLocation1
  38.             } else {
  39.                 return "NO"
  40.             }
  41.         }
  42.        
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement