Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Маршрут
- func getRoute(_ tickets: [[String: String]]) -> [[String: String]] {
- var points = [String: String]()
- for ticket in tickets {
- let from = ticket["from"]!
- let to = ticket["to"]!
- points[from] = to
- }
- let start = Array(Set(points.keys).subtracting(Set(points.values))).first!
- var route = [[String: String]]()
- var currentPoint = start
- while let nextPoint = points[currentPoint] {
- route.append([currentPoint : nextPoint])
- currentPoint = nextPoint
- }
- return route
- }
- // Пример использования:
- let tickets = [
- ["from": "London", "to": "Moscow"],
- ["from": "NY", "to": "London"],
- ["from": "Moscow", "to": "SPb"]
- ]
- let route = getRoute(tickets)
- print(route)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement