Last active
November 29, 2017 01:07
-
-
Save cjnevin/02a74b752af5e852f13dc26b2405b9ba to your computer and use it in GitHub Desktop.
This file contains 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
let replacements = [("3", "*"), ("7", "3"), ("*", "7")] | |
func twisted37(_ val: Int) -> Int { | |
return Int(replacements.reduce(String(val), { $0.replacingOccurrences(of: $1.0, with: $1.1) })) ?? 0 | |
} | |
func sortTwisted37(_ arr: [Int]) -> [Int] { | |
return arr.sorted(by: { twisted37($0) < twisted37($1) }) | |
} | |
assert(sortTwisted37([1,2,3,4,5,6,7,8,9]) == [1, 2, 7, 4, 5, 6, 3, 8, 9]) | |
assert(sortTwisted37([12,13,14]) == [12,14,13]) | |
assert(sortTwisted37([9,2,4,7,3]) == [2, 7, 4, 3, 9]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment