Skip to content

Instantly share code, notes, and snippets.

@fcanas
Created May 27, 2015 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcanas/9f05148044951705945f to your computer and use it in GitHub Desktop.
Save fcanas/9f05148044951705945f to your computer and use it in GitHub Desktop.
your swift ride is here
func encode(name :String) -> Int {
return reduce(map(name.unicodeScalars) { Int($0.value) - 64 }, 1, *) % 47
}
func ride(group: String, comet: String) -> String {
return encode(group) == encode(comet) ? "GO" : "STAY"
}
let testData = [
("COMETQ","HVNGAT","GO"),
("STARAB","USACO","STAY"),
("EARTH","LEFTB","GO"),
("PULSAR","VENUS","STAY"),
("KANSAS","UTAH","STAY"),
("APPLE","URSA","GO"),
("MSFT","MARS","STAY"),
("PLUTO","BKHOLE","STAY"),
("COWSBC","RIGHT","GO"),
("DRKMTR","SNIKER","STAY")]
func testRide() -> String {
for testCase in testData {
if ride(testCase.0, testCase.1) != testCase.2 {
return "Fail: Group \(testCase.0) and comet \(testCase.1) should \(testCase.2)"
}
}
return "pass"
}
let testResult = testRide()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment