Advertisement
Alexxik

Untitled

Mar 19th, 2024
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.80 KB | None | 0 0
  1. // Маршрут
  2.  
  3. func getRoute(_ tickets: [[String: String]]) -> [[String: String]] {
  4.     var points = [String: String]()
  5.    
  6.     for ticket in tickets {
  7.         let from = ticket["from"]!
  8.         let to = ticket["to"]!
  9.         points[from] = to
  10.     }
  11.  
  12.     let start = Array(Set(points.keys).subtracting(Set(points.values))).first!
  13.    
  14.     var route = [[String: String]]()
  15.     var currentPoint = start
  16.    
  17.     while let nextPoint = points[currentPoint] {
  18.         route.append([currentPoint : nextPoint])
  19.         currentPoint = nextPoint
  20.     }
  21.    
  22.     return route
  23. }
  24.  
  25. // Пример использования:
  26. let tickets = [
  27.     ["from": "London", "to": "Moscow"],
  28.     ["from": "NY", "to": "London"],
  29.     ["from": "Moscow", "to": "SPb"]
  30. ]
  31. let route = getRoute(tickets)
  32. print(route)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement