Last active
May 20, 2016 19:15
-
-
Save tianskylan/9ad502b5b291ceb004e29d7a59983780 to your computer and use it in GitHub Desktop.
My initial take on a itinerary problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Flight { | |
let fromCity: String | |
let toCity: String | |
} | |
let originCities = itinerary1.map { $0.fromCity } | |
let destinationCities = itinerary1.map { $0.toCity } | |
var travelLog: [String: Int] = [:] | |
originCities.forEach { | |
if let x = travelLog[$0] { | |
travelLog[$0] = x - 1 | |
} else { | |
travelLog[$0] = -1 | |
} | |
} | |
destinationCities.forEach { | |
if let y = travelLog[$0] { | |
travelLog[$0] = y + 1 | |
} else { | |
travelLog[$0] = 1 | |
} | |
} | |
let cities = travelLog.filter { $0.1 != 0 } | |
print(cities) | |
// Test cases | |
let itinerary1 = [ | |
Flight(fromCity: "DEN", toCity: "SEA"), | |
Flight(fromCity: "SJC", toCity: "SLC"), | |
Flight(fromCity: "SLC", toCity: "DEN"), | |
Flight(fromCity: "LAX", toCity: "LAS"), | |
Flight(fromCity: "SEA", toCity: "JFK"), | |
Flight(fromCity: "LAS", toCity: "SJC"), | |
Flight(fromCity: "SFO", toCity: "LAX"), | |
Flight(fromCity: "ATL", toCity: "SFO") | |
] // ATL, SFO, LAX, LAS, SJC, SLC, DEN, SEA, JFK | |
let itinerary2 = [ | |
Flight(fromCity: "SFO", toCity: "LAX"), | |
Flight(fromCity: "DEN", toCity: "SEA"), | |
Flight(fromCity: "MIA", toCity: "SFO"), | |
Flight(fromCity: "SFO", toCity: "MIA"), | |
Flight(fromCity: "LAX", toCity: "LAS"), | |
Flight(fromCity: "LAS", toCity: "SJC"), | |
Flight(fromCity: "IAH", toCity: "DFW"), | |
Flight(fromCity: "DFW", toCity: "MIA"), | |
Flight(fromCity: "SJC", toCity: "SLC"), | |
Flight(fromCity: "MIA", toCity: "IAH"), | |
Flight(fromCity: "SLC", toCity: "DEN"), | |
Flight(fromCity: "ATL", toCity: "SFO"), | |
Flight(fromCity: "SEA", toCity: "JFK") | |
] // ATL, SFO, MIA, IAH, DFW, MIA, SFO, LAX, LAS, SJC, SLC, DEN, SEA, JFK | |
let itinerary3 = [ | |
Flight(fromCity: "LAX", toCity: "SFO"), | |
Flight(fromCity: "SFO", toCity: "IAH"), | |
Flight(fromCity: "IAH", toCity: "SEA"), | |
Flight(fromCity: "SEA", toCity: "SFO"), | |
Flight(fromCity: "SFO", toCity: "PHX"), | |
Flight(fromCity: "PHX", toCity: "JFK"), | |
Flight(fromCity: "JFK", toCity: "SFO") | |
] // LAX, SFO, IAH, SEA, SFO, PHX, JFK, SFO | |
let itinerary4 = [ | |
Flight(fromCity: "LAX", toCity: "SFO"), | |
Flight(fromCity: "SFO", toCity: "IAH"), | |
Flight(fromCity: "IAH", toCity: "SEA"), | |
Flight(fromCity: "SEA", toCity: "SFO"), | |
Flight(fromCity: "SFO", toCity: "PHX"), | |
Flight(fromCity: "PHX", toCity: "JFK"), | |
Flight(fromCity: "JFK", toCity: "SFO"), | |
Flight(fromCity: "SFO", toCity: "ORL"), | |
Flight(fromCity: "ORL", toCity: "SAC"), | |
Flight(fromCity: "SAC", toCity: "SEA"), | |
Flight(fromCity: "SEA", toCity: "SJC"), | |
Flight(fromCity: "SJC", toCity: "SAC"), | |
Flight(fromCity: "SAC", toCity: "MIA"), | |
Flight(fromCity: "MIA", toCity: "SLC"), | |
Flight(fromCity: "SLC", toCity: "SJC"), | |
Flight(fromCity: "SJC", toCity: "DEN"), | |
Flight(fromCity: "DEN", toCity: "LAX") | |
] // LAX, SFO, IAH, SEA, SFO, PHX, JFK, SFO, ORL, SAC, SEA, SJC, SAC, MIA, SLC, SJC, DEN, LAX |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment