Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit 86fbfc7

Browse files
author
Jazbo
authored
Merge pull request #2 from iosdevelopershq/swift3
WIP: Xcode updates + Persian C style loop fix.
2 parents 8868778 + 01fae11 commit 86fbfc7

17 files changed

+77
-60
lines changed

CodeChallenge.xcodeproj/project.pbxproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,16 @@
268268
isa = PBXProject;
269269
attributes = {
270270
LastSwiftUpdateCheck = 0710;
271-
LastUpgradeCheck = 0710;
271+
LastUpgradeCheck = 0810;
272272
ORGANIZATIONNAME = iosdevelopers;
273273
TargetAttributes = {
274274
9272E1A31BD7F6560030B403 = {
275275
CreatedOnToolsVersion = 7.0;
276+
LastSwiftMigration = 0810;
276277
};
277278
9272E1AD1BD7F6560030B403 = {
278279
CreatedOnToolsVersion = 7.0;
280+
LastSwiftMigration = 0810;
279281
};
280282
};
281283
};
@@ -374,8 +376,10 @@
374376
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
375377
CLANG_WARN_EMPTY_BODY = YES;
376378
CLANG_WARN_ENUM_CONVERSION = YES;
379+
CLANG_WARN_INFINITE_RECURSION = YES;
377380
CLANG_WARN_INT_CONVERSION = YES;
378381
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
382+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
379383
CLANG_WARN_UNREACHABLE_CODE = YES;
380384
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
381385
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -422,8 +426,10 @@
422426
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
423427
CLANG_WARN_EMPTY_BODY = YES;
424428
CLANG_WARN_ENUM_CONVERSION = YES;
429+
CLANG_WARN_INFINITE_RECURSION = YES;
425430
CLANG_WARN_INT_CONVERSION = YES;
426431
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
432+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
427433
CLANG_WARN_UNREACHABLE_CODE = YES;
428434
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
429435
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -443,6 +449,7 @@
443449
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
444450
MTL_ENABLE_DEBUG_INFO = NO;
445451
SDKROOT = iphoneos;
452+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
446453
TARGETED_DEVICE_FAMILY = "1,2";
447454
VALIDATE_PRODUCT = YES;
448455
VERSIONING_SYSTEM = "apple-generic";
@@ -454,6 +461,7 @@
454461
isa = XCBuildConfiguration;
455462
buildSettings = {
456463
CLANG_ENABLE_MODULES = YES;
464+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
457465
DEFINES_MODULE = YES;
458466
DYLIB_COMPATIBILITY_VERSION = 1;
459467
DYLIB_CURRENT_VERSION = 1;
@@ -465,13 +473,15 @@
465473
PRODUCT_NAME = CodeChallenge;
466474
SKIP_INSTALL = YES;
467475
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
476+
SWIFT_VERSION = 3.0;
468477
};
469478
name = Debug;
470479
};
471480
9272E1BA1BD7F6560030B403 /* Release */ = {
472481
isa = XCBuildConfiguration;
473482
buildSettings = {
474483
CLANG_ENABLE_MODULES = YES;
484+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
475485
DEFINES_MODULE = YES;
476486
DYLIB_COMPATIBILITY_VERSION = 1;
477487
DYLIB_CURRENT_VERSION = 1;
@@ -482,6 +492,7 @@
482492
PRODUCT_BUNDLE_IDENTIFIER = com.iosdevelopershq.CodeChallenge;
483493
PRODUCT_NAME = CodeChallenge;
484494
SKIP_INSTALL = YES;
495+
SWIFT_VERSION = 3.0;
485496
};
486497
name = Release;
487498
};
@@ -493,6 +504,7 @@
493504
PRODUCT_BUNDLE_IDENTIFIER = com.iosdevelopershq.CodeChallengeTests;
494505
PRODUCT_MODULE_NAME = CodeChallengeTests;
495506
PRODUCT_NAME = CodeChallenge;
507+
SWIFT_VERSION = 3.0;
496508
};
497509
name = Debug;
498510
};
@@ -504,6 +516,7 @@
504516
PRODUCT_BUNDLE_IDENTIFIER = com.iosdevelopershq.CodeChallengeTests;
505517
PRODUCT_MODULE_NAME = CodeChallengeTests;
506518
PRODUCT_NAME = CodeChallenge;
519+
SWIFT_VERSION = 3.0;
507520
};
508521
name = Release;
509522
};

CodeChallenge.xcodeproj/xcshareddata/xcschemes/CodeChallenge.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0710"
3+
LastUpgradeVersion = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

CodeChallenge.xcodeproj/xcshareddata/xcschemes/CodeChallengeTests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0710"
3+
LastUpgradeVersion = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

CodeChallenge/Base/CodeChallengeType.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protocol CodeChallengeType {
3535

3636
- Returns: True if the output is valid, false if not.
3737
*/
38-
func verifyOutput(output: OutputType, forInput input: InputType) -> Bool
38+
func verifyOutput(_ output: OutputType, forInput input: InputType) -> Bool
3939
}
4040

4141
/// An entry for a `CodeChallengeType`.
@@ -44,7 +44,7 @@ struct CodeChallengeEntry<ChallengeType: CodeChallengeType> {
4444
let name: String
4545

4646
/// The block to be run to evaluate this entry.
47-
let block: (input: ChallengeType.InputType) -> ChallengeType.OutputType
47+
let block: (ChallengeType.InputType) -> ChallengeType.OutputType
4848
}
4949

5050
/// A structure to hold the result of a single run of an Entry's `block`.
@@ -59,18 +59,18 @@ struct CodeChallengeResult<ChallengeType: CodeChallengeType> {
5959
let outputs: [ChallengeType.OutputType]
6060

6161
/// The amount of wall-clock time it took to process the input. There should be one time measurment in this array for each iteration, and the index here will match the corresponding output in the `outputs` array.
62-
let times: [NSTimeInterval]
62+
let times: [TimeInterval]
6363

6464
/// The number of times the entry was run.
6565
let iterations: Int
6666

6767
/// The total wall-clock time it took to process input `iterations` times.
68-
var totalTime: NSTimeInterval { return times.reduce(0) { $0 + $1 } }
68+
var totalTime: TimeInterval { return times.reduce(0) { $0 + $1 } }
6969

7070
/// The average wall-clock time a single iteration took to process the input.
71-
var averageTime: NSTimeInterval {
71+
var averageTime: TimeInterval {
7272
// computers don't like dividing by zero.
73-
guard times.count != 0 else { return NSTimeInterval.infinity }
73+
guard times.count != 0 else { return TimeInterval.infinity }
7474
return Double(totalTime) / Double(times.count)
7575
}
7676
}
@@ -96,10 +96,10 @@ struct AccumulatedChallengeResult<ChallengeType: CodeChallengeType> {
9696
let results: [CodeChallengeResult<ChallengeType>]
9797

9898
/// The total wall-clock time it took to process all results.
99-
let totalTime: NSTimeInterval
99+
let totalTime: TimeInterval
100100

101101
/// The average wall-clock time a single iteration took to process its input.
102-
let averageTime: NSTimeInterval
102+
let averageTime: TimeInterval
103103

104104
/**
105105
Create a new AccumulatedChallengeResult from the given set of results. The time properties will be calculated using the given parameters to this init.
@@ -114,16 +114,16 @@ struct AccumulatedChallengeResult<ChallengeType: CodeChallengeType> {
114114
self.successRate = Double(successes) / Double(total)
115115

116116
if total == 0 {
117-
self.averageTime = NSTimeInterval.infinity
117+
self.averageTime = TimeInterval.infinity
118118
} else {
119119
self.averageTime = Double(results.reduce(0) { $0 + $1.averageTime }) / Double(total)
120120
}
121121
}
122122
}
123123

124124
extension CodeChallengeType {
125-
private typealias ResultType = CodeChallengeResult<Self>
126-
private typealias OperationType = RunOperation<Self>
125+
fileprivate typealias ResultType = CodeChallengeResult<Self>
126+
fileprivate typealias OperationType = RunOperation<Self>
127127

128128
typealias AccumulatedResultType = AccumulatedChallengeResult<Self>
129129

@@ -140,14 +140,14 @@ extension CodeChallengeType {
140140
let dataset = generateDataset()
141141

142142
// Make a worker queue so that we can run through everything concurrently and save the runner some time.
143-
let workerQueue = NSOperationQueue()
143+
let workerQueue = OperationQueue()
144144
workerQueue.maxConcurrentOperationCount = 30
145145
var workers = [OperationType]()
146146
let iterations = 10
147147

148148
// For each entry
149149
for entry in entries {
150-
workers.appendContentsOf(dataset.map { OperationType(entry: entry, input: $0, iterations: iterations) })
150+
workers.append(contentsOf: dataset.map { OperationType(entry: entry, input: $0, iterations: iterations) })
151151
}
152152

153153
print("Adding \(workers.count) operations to the queue and running them \(min(workerQueue.maxConcurrentOperationCount, workers.count)) at a time")
@@ -181,11 +181,11 @@ extension CodeChallengeType {
181181
}
182182

183183
// Return the accumulated results sorted by average time.
184-
return accumulatedResults.sort { $0.averageTime < $1.averageTime }
184+
return accumulatedResults.sorted { $0.averageTime < $1.averageTime }
185185
}
186186
}
187187

188-
private class RunOperation<ChallengeType: CodeChallengeType>: NSOperation {
188+
private class RunOperation<ChallengeType: CodeChallengeType>: Operation {
189189
let entry: CodeChallengeEntry<ChallengeType>
190190
let input: ChallengeType.InputType
191191
let iterations: Int
@@ -200,10 +200,10 @@ private class RunOperation<ChallengeType: CodeChallengeType>: NSOperation {
200200

201201
override func main() {
202202
var outputs = Array<ChallengeType.OutputType>()
203-
var times = Array<NSTimeInterval>()
203+
var times = Array<TimeInterval>()
204204
for _ in 1...iterations {
205-
let start = NSDate()
206-
outputs.append(entry.block(input: input))
205+
let start = Date()
206+
outputs.append(entry.block(input))
207207
times.append(-start.timeIntervalSinceNow)
208208
}
209209
result = CodeChallengeResult(name: entry.name, input: input, outputs: outputs, times: times, iterations: iterations)

CodeChallenge/Base/ExampleCodeChallenge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct ExampleCodeChallenge: CodeChallengeType {
2626
return [Int](1...1000)
2727
}
2828

29-
func verifyOutput(output: String, forInput input: Int) -> Bool {
29+
func verifyOutput(_ output: String, forInput input: Int) -> Bool {
3030
return output == "\(input)"
3131
}
3232
}

CodeChallenge/Challenges/LetterCombinationsOfPhoneNumber/Entries/BugKrushaLetterCombinationsOfPhoneNumberEntry.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
let bugKrushaLetterCombinationOfPhoneNumberEntry = CodeChallengeEntry<LetterCombinationsOfPhoneNumberChallenge>(name: "bugKrusha") { input in
12-
return getCombinations(input)
12+
return getCombinations(digitString: input)
1313
}
1414

1515

@@ -27,7 +27,7 @@ private enum Digit: String {
2727
}
2828

2929

30-
private func getCharactersForNumber(number number: String) -> Digit? {
30+
private func getCharactersForNumber(number: String) -> Digit? {
3131
switch number {
3232
case "2": return Digit.Two
3333
case "3": return Digit.Three
@@ -45,7 +45,7 @@ private func getCharactersForNumber(number number: String) -> Digit? {
4545

4646
private func getCombinations(digitString: String) -> [String] {
4747
var arrayOfCharacterSets = [String]()
48-
for (index, character) in digitString.characters.enumerate() {
48+
for (index, character) in digitString.characters.enumerated() {
4949
if let charactersForNumber = getCharactersForNumber(number: "\(character)")?.rawValue {
5050
if index == 0 {
5151
arrayOfCharacterSets = charactersForNumber.characters.map { "\($0)" }

CodeChallenge/Challenges/LetterCombinationsOfPhoneNumber/Entries/LoganWrightEntry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Foundation
1212

1313
typealias PhoneCombinationsTest = CodeChallengeEntry<LetterCombinationsOfPhoneNumberChallenge>
1414
let LoganWrightLetterCombinationOfPhoneNumberEntry =
15-
PhoneCombinationsTest(name: "LoganWright") { possibleCombinationsForPhoneNumber($0) }
15+
PhoneCombinationsTest(name: "LoganWright") { possibleCombinationsForPhoneNumber(phoneNumber: $0) }
1616

1717
// MARK: Solution
1818

CodeChallenge/Challenges/LetterCombinationsOfPhoneNumber/LetterCombinationsOfPhoneNumberChallenge.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ struct LetterCombinationsOfPhoneNumberChallenge: CodeChallengeType {
3737
return ["23"]
3838
}
3939

40-
func verifyOutput(output: OutputType, forInput input: InputType) -> Bool {
40+
func verifyOutput(_ output: OutputType, forInput input: InputType) -> Bool {
4141
guard let expected = verificationDictionary[input] else { return false }
42-
return output.sort(<) == expected.sort(<)
42+
return output.sorted(by: <) == expected.sorted(by: <)
4343
}
4444

45-
private let verificationDictionary = [
45+
fileprivate let verificationDictionary = [
4646
"23": ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
4747
]
4848
}

CodeChallenge/Challenges/LongestSubstringWithoutRepeatingCharacters/LongestSubstringWithoutRepeatingCharactersChallenge.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ struct LongestSubstringWithoutRepeatingCharactersChallenge: CodeChallengeType {
3232
]
3333
}
3434

35-
func verifyOutput(output: OutputType, forInput input: InputType) -> Bool {
35+
func verifyOutput(_ output: OutputType, forInput input: InputType) -> Bool {
3636
guard let expected = verificationDictionary[input] else { return false }
3737
return output == expected
3838
}
3939

40-
private let verificationDictionary = [
40+
fileprivate let verificationDictionary = [
4141
"abcabcbb": 3,
4242
"bbbbb": 1
4343
]

CodeChallenge/Challenges/TwoSum/Entries/AlexPersian.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ let alexPersianTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "AlexPers
1717
}
1818
}
1919
return nil
20-
}
20+
}

CodeChallenge/Challenges/TwoSum/Entries/Aranasaurus.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ let aranasaurusTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "Aranasau
1313
var max = Datum(0, 0)
1414
let mid = input.target / 2
1515

16-
for (i, number) in input.numbers.enumerate() {
16+
for (i, number) in input.numbers.enumerated() {
1717
guard number >= 0 && number <= input.target && number >= mid else { continue }
1818

1919
if number > max.number {
20-
if let diffIndex = input.numbers.indexOf(input.target - number) {
20+
if let diffIndex = input.numbers.index(of: input.target - number) {
2121
return (diffIndex + 1, i + 1)
2222
}
2323
max = Datum(i, number)
2424
}
2525
}
2626

27-
return .None
27+
return .none
2828
}
2929

3030
private struct Datum {

CodeChallenge/Challenges/TwoSum/Entries/BugKrusha.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
let bugkrushaTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "Jazbo") { input in
12-
for (index, num) in input.numbers.enumerate() {
12+
for (index, num) in input.numbers.enumerated() {
1313
for i in index + 1 ..< input.numbers.count {
1414
if calculate(input.numbers[i], numTwo: num, calculation: +) == input.target {
1515
return (index + 1, i + 1)
@@ -19,6 +19,6 @@ let bugkrushaTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "Jazbo") {
1919
return nil
2020
}
2121

22-
private func calculate(numOne: Int, numTwo: Int, calculation: (a: Int, b: Int) -> Int) -> Int {
23-
return calculation(a: numOne, b: numTwo)
24-
}
22+
private func calculate(_ numOne: Int, numTwo: Int, calculation: (_ a: Int, _ b: Int) -> Int) -> Int {
23+
return calculation(numOne, numTwo)
24+
}

CodeChallenge/Challenges/TwoSum/Entries/IanKeen.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import Foundation
1010

1111
let ianKeenTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "IanKeen") { input in
12-
for (index, digit) in input.numbers.enumerate() {
13-
if let other = input.numbers.indexOf(input.target - digit) {
12+
for (index, digit) in input.numbers.enumerated() {
13+
if let other = input.numbers.index(of: input.target - digit) {
1414
return (index + 1, Int(other) + 1)
1515
}
1616
}
1717
return nil
18-
}
18+
}

CodeChallenge/Challenges/TwoSum/Entries/Mosab.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ let mosabTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "Mosab") { inpu
1212
var numbers = input.numbers
1313
var dictionary : [Int : Int] = [:] //Dictionary to hold numbers and indices
1414

15-
for (index, number) in numbers.enumerate() {
15+
for (index, number) in numbers.enumerated() {
1616
dictionary[number] = index
1717
}
1818

1919
return findIndices(&dictionary, numbers: &numbers, target: input.target)
2020
}
2121

22-
private func findIndices(inout map : [Int:Int], inout numbers : [Int], let target : Int) -> (Int, Int)?
22+
private func findIndices(_ map : inout [Int:Int], numbers : inout [Int], target : Int) -> (Int, Int)?
2323
{
24-
for (index, number) in numbers.enumerate()
24+
for (index, number) in numbers.enumerated()
2525
{
2626
if let index2 = map[abs(number - target)]
2727
{
@@ -30,4 +30,4 @@ private func findIndices(inout map : [Int:Int], inout numbers : [Int], let targe
3030
}
3131

3232
return nil
33-
}
33+
}

0 commit comments

Comments
 (0)