Created
March 8, 2016 21:36
-
-
Save wjlafrance/1745499aba05b05c645a 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
//: Playground - noun: a place where people can play | |
import Darwin | |
let alphabets: [[Character]] = [ | |
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"], | |
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] | |
] | |
func cipherWithRotation(rotation: Int) -> [Character: Character] { | |
var cipher = [Character: Character]() | |
for alphabet in alphabets { | |
for index in (0..<(alphabet.count)) { | |
cipher[alphabet[index]] = alphabet[(index + rotation) % alphabet.count] | |
} | |
} | |
return cipher | |
} | |
func cipherString(str: String, rotation: Int) -> String { | |
let cipher = cipherWithRotation(rotation) | |
return String(str.characters.map { cipher[$0] ?? $0 }) | |
} | |
let input = "The quick brown fox jumped over the lazy dog." | |
let cipherText = cipherString(input, rotation: 13) | |
let decipherText = cipherString(cipherText, rotation: 13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment