Skip to content

Instantly share code, notes, and snippets.

@keitaito
Created January 6, 2017 05:03
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 keitaito/3f82015b3cb8a3b4e30553d3994e3c2a to your computer and use it in GitHub Desktop.
Save keitaito/3f82015b3cb8a3b4e30553d3994e3c2a to your computer and use it in GitHub Desktop.
// 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