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, 2)
- } else {
- return (x1 - x2, 1)
- }
- }()
- if greaterOne == 1 {
- if v1 > v2 {
- return "NO"
- }
- } else if greaterOne == 2 {
- if v2 > v1 {
- return "NO"
- }
- }
- while(true) {
- currentLocation1 = currentLocation1 + v1
- currentLocation2 = currentLocation2 + v2
- if differenceInLocations == 0 {
- return "YES"
- }
- if greaterOne == 1 {
- if differenceInLocations > currentLocation1 - currentLocation2 && currentLocation1 - currentLocation2 >= 0 {
- differenceInLocations = currentLocation1 - currentLocation2
- } else {
- return "NO"
- }
- } else if greaterOne == 2 {
- if differenceInLocations > currentLocation2 - currentLocation1 && currentLocation2 - currentLocation1 >= 0{
- differenceInLocations = currentLocation2 - currentLocation1
- } else {
- return "NO"
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement