Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotational cipher #290

Merged
merged 7 commits into from Aug 16, 2017
Merged
Diff settings

Always

Just for now

Copy path View file
@@ -541,6 +541,14 @@
"Searching"
]
},
{
"slug": "rotational-cipher",
"difficulty": 4,

This comment has been minimized.

Copy link
@masters3d

masters3d Aug 16, 2017

Member

Looks good!

"topics": [
"Strings",
"Transforming"
]
}
{
"uuid": "942d0c4b-eaef-4576-b3f8-383757c53768",
"slug": "roman-numerals",
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
@@ -0,0 +1,23 @@
identifier_name:
min_length:

This comment has been minimized.

Copy link
@masters3d

masters3d Aug 16, 2017

Member

Use the config file on the root. I'd rather her not have nested config files. You are welcome to suggest changes to that file.

warning: 1
error: 0

large_tuple:
warning: 5
error: 6

colon:
apply_to_dictionaries: false

opt_in_rules:
- vertical_whitespace
- closure_spacing

disabled_rules: # rule identifiers to exclude from running
- function_body_length
- line_length
- trailing_comma
- force_try

reporter: "json"
@@ -0,0 +1,5 @@
import PackageDescription

let package = Package(
name: "rotational-cipher"

This comment has been minimized.

Copy link
@masters3d

masters3d Aug 16, 2017

Member

We use camel case for the package name here.

)
@@ -0,0 +1 @@
//Solution goes in Sources
@@ -0,0 +1,30 @@
struct RotationalCipher {

static func rotate(_ target: String, ROT: Int) -> String {

var result = ""

target.unicodeScalars.forEach { unicode in

switch unicode.value {
case 65...90: // A to Z
var scalar = unicode.value + UInt32(ROT)
if scalar > 90 { scalar -= 26 }
result.append(Character(UnicodeScalar(scalar)!))

case 97...122: // a to z
var scalar = unicode.value + UInt32(ROT)
if scalar > 122 { scalar -= 26 }
result.append(Character(UnicodeScalar(scalar)!))

default:
result.append(Character(unicode))
}

}

return result

}

}
@@ -0,0 +1,6 @@
import XCTest
@testable import rotational_cipherTests

This comment has been minimized.

Copy link
@masters3d

masters3d Aug 16, 2017

Member

Camel case for test me too. Same as package.


XCTMain([
testCase(RotationalCipherTests.allTests),
])
@@ -0,0 +1,70 @@
import XCTest
@testable import rotational_cipher

class RotationalCipherTests: XCTestCase {

func testRotateSingleCharacterBy1() {
let expected = "b"
XCTAssertEqual(expected, RotationalCipher.rotate("a", ROT: 1))
}

func testRotateSingleCharacterBy26() {
let expected = "a"
XCTAssertEqual(expected, RotationalCipher.rotate("a", ROT: 26))
}

func testRotateSingleCharacterBy0() {
let expected = "a"
XCTAssertEqual(expected, RotationalCipher.rotate("a", ROT: 0))
}

func testRotateSingleCharacterBy13() {
let expected = "z"
XCTAssertEqual(expected, RotationalCipher.rotate("m", ROT: 13))
}

func testRotateSingleCharacterWithWrapAround() {
let expected = "a"
XCTAssertEqual(expected, RotationalCipher.rotate("n", ROT: 13))
}

func testRotateCapitalLetters() {
let expected = "TRL"
XCTAssertEqual(expected, RotationalCipher.rotate("OMG", ROT: 5))
}

func testRotateSpaces() {
let expected = "T R L"
XCTAssertEqual(expected, RotationalCipher.rotate("O M G", ROT: 5))
}

func testRotateNumbers() {
let expected = "Xiwxmrk 1 2 3 xiwxmrk"
XCTAssertEqual(expected, RotationalCipher.rotate("Testing 1 2 3 testing", ROT: 4))
}

func testRotatePunctuation() {
let expected = "Gzo'n zvo, Bmviyhv!"
XCTAssertEqual(expected, RotationalCipher.rotate("Let's eat, Grandma!", ROT: 21))
}

func testRotateAllLetters() {
let expected = "The quick brown fox jumps over the lazy dog."
XCTAssertEqual(expected, RotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", ROT: 13))
}

static var allTests: [(String, (RotationalCipherTests) -> () throws -> Void)] {
return [
("testRotateSingleCharacterBy1", testRotateSingleCharacterBy1),
("testRotateSingleCharacterBy26", testRotateSingleCharacterBy26),
("testRotateSingleCharacterBy0", testRotateSingleCharacterBy0),
("testRotateSingleCharacterBy13", testRotateSingleCharacterBy13),
("testRotateSingleCharacterWithWrapAround", testRotateSingleCharacterWithWrapAround),
("testRotateCapitalLetters", testRotateCapitalLetters),
("testRotateSpaces", testRotateSpaces),
("testRotateNumbers", testRotateNumbers),
("testRotatePunctuation", testRotatePunctuation),
("testRotateAllLetters", testRotateAllLetters)
]
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.