Created
January 6, 2017 05:03
-
-
Save keitaito/3f82015b3cb8a3b4e30553d3994e3c2a to your computer and use it in GitHub Desktop.
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
// Updated to Swift 3 syntax. | |
func mapScalarValues(s: String, f: (UInt32) -> UInt32) -> String { | |
let scalars = Array(s.unicodeScalars) | |
let encrypted = scalars.map { x in | |
Character(UnicodeScalar(f(x.value))!) | |
} | |
return String(encrypted) | |
} | |
func caesar(plainText: String) -> String { | |
return mapScalarValues(s: plainText) { x in x + 7 } | |
} | |
func rot13(plainText: String) -> String { | |
let A: UInt32 = 65 | |
let Z: UInt32 = 90 | |
return mapScalarValues(s: plainText) { x in | |
if x >= A && x <= Z { | |
return A + ((x + 13) % 26) | |
} | |
return x | |
} | |
} | |
let input = "AAA" | |
let output = rot13(plainText: input) | |
print(output) // output is "AAA". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment