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

Xcode updates + Persian C style loop fix. #2

Merged
merged 7 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion CodeChallenge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0710;
LastUpgradeCheck = 0810;
ORGANIZATIONNAME = iosdevelopers;
TargetAttributes = {
9272E1A31BD7F6560030B403 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0810;
};
9272E1AD1BD7F6560030B403 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0810;
};
};
};
Expand Down Expand Up @@ -374,8 +376,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -422,8 +426,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -443,6 +449,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -454,6 +461,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -465,13 +473,15 @@
PRODUCT_NAME = CodeChallenge;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
9272E1BA1BD7F6560030B403 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -482,6 +492,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.iosdevelopershq.CodeChallenge;
PRODUCT_NAME = CodeChallenge;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -493,6 +504,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.iosdevelopershq.CodeChallengeTests;
PRODUCT_MODULE_NAME = CodeChallengeTests;
PRODUCT_NAME = CodeChallenge;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -504,6 +516,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.iosdevelopershq.CodeChallengeTests;
PRODUCT_MODULE_NAME = CodeChallengeTests;
PRODUCT_NAME = CodeChallenge;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0810"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0810"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
36 changes: 18 additions & 18 deletions CodeChallenge/Base/CodeChallengeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protocol CodeChallengeType {

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

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

/// The block to be run to evaluate this entry.
let block: (input: ChallengeType.InputType) -> ChallengeType.OutputType
let block: (ChallengeType.InputType) -> ChallengeType.OutputType
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This required change is interesting 😩

}

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

/// 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.
let times: [NSTimeInterval]
let times: [TimeInterval]

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

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

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

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

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

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

if total == 0 {
self.averageTime = NSTimeInterval.infinity
self.averageTime = TimeInterval.infinity
} else {
self.averageTime = Double(results.reduce(0) { $0 + $1.averageTime }) / Double(total)
}
}
}

extension CodeChallengeType {
private typealias ResultType = CodeChallengeResult<Self>
private typealias OperationType = RunOperation<Self>
fileprivate typealias ResultType = CodeChallengeResult<Self>
fileprivate typealias OperationType = RunOperation<Self>

typealias AccumulatedResultType = AccumulatedChallengeResult<Self>

Expand All @@ -140,14 +140,14 @@ extension CodeChallengeType {
let dataset = generateDataset()

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

// For each entry
for entry in entries {
workers.appendContentsOf(dataset.map { OperationType(entry: entry, input: $0, iterations: iterations) })
workers.append(contentsOf: dataset.map { OperationType(entry: entry, input: $0, iterations: iterations) })
}

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

// Return the accumulated results sorted by average time.
return accumulatedResults.sort { $0.averageTime < $1.averageTime }
return accumulatedResults.sorted { $0.averageTime < $1.averageTime }
}
}

private class RunOperation<ChallengeType: CodeChallengeType>: NSOperation {
private class RunOperation<ChallengeType: CodeChallengeType>: Operation {
let entry: CodeChallengeEntry<ChallengeType>
let input: ChallengeType.InputType
let iterations: Int
Expand All @@ -200,10 +200,10 @@ private class RunOperation<ChallengeType: CodeChallengeType>: NSOperation {

override func main() {
var outputs = Array<ChallengeType.OutputType>()
var times = Array<NSTimeInterval>()
var times = Array<TimeInterval>()
for _ in 1...iterations {
let start = NSDate()
outputs.append(entry.block(input: input))
let start = Date()
outputs.append(entry.block(input))
times.append(-start.timeIntervalSinceNow)
}
result = CodeChallengeResult(name: entry.name, input: input, outputs: outputs, times: times, iterations: iterations)
Expand Down
2 changes: 1 addition & 1 deletion CodeChallenge/Base/ExampleCodeChallenge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct ExampleCodeChallenge: CodeChallengeType {
return [Int](1...1000)
}

func verifyOutput(output: String, forInput input: Int) -> Bool {
func verifyOutput(_ output: String, forInput input: Int) -> Bool {
return output == "\(input)"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

let bugKrushaLetterCombinationOfPhoneNumberEntry = CodeChallengeEntry<LetterCombinationsOfPhoneNumberChallenge>(name: "bugKrusha") { input in
return getCombinations(input)
return getCombinations(digitString: input)
}


Expand All @@ -27,7 +27,7 @@ private enum Digit: String {
}


private func getCharactersForNumber(number number: String) -> Digit? {
private func getCharactersForNumber(number: String) -> Digit? {
switch number {
case "2": return Digit.Two
case "3": return Digit.Three
Expand All @@ -45,7 +45,7 @@ private func getCharactersForNumber(number number: String) -> Digit? {

private func getCombinations(digitString: String) -> [String] {
var arrayOfCharacterSets = [String]()
for (index, character) in digitString.characters.enumerate() {
for (index, character) in digitString.characters.enumerated() {
if let charactersForNumber = getCharactersForNumber(number: "\(character)")?.rawValue {
if index == 0 {
arrayOfCharacterSets = charactersForNumber.characters.map { "\($0)" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation

typealias PhoneCombinationsTest = CodeChallengeEntry<LetterCombinationsOfPhoneNumberChallenge>
let LoganWrightLetterCombinationOfPhoneNumberEntry =
PhoneCombinationsTest(name: "LoganWright") { possibleCombinationsForPhoneNumber($0) }
PhoneCombinationsTest(name: "LoganWright") { possibleCombinationsForPhoneNumber(phoneNumber: $0) }

// MARK: Solution

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ struct LetterCombinationsOfPhoneNumberChallenge: CodeChallengeType {
return ["23"]
}

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

private let verificationDictionary = [
fileprivate let verificationDictionary = [
"23": ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ struct LongestSubstringWithoutRepeatingCharactersChallenge: CodeChallengeType {
]
}

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

private let verificationDictionary = [
fileprivate let verificationDictionary = [
"abcabcbb": 3,
"bbbbb": 1
]
Expand Down
2 changes: 1 addition & 1 deletion CodeChallenge/Challenges/TwoSum/Entries/AlexPersian.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ let alexPersianTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "AlexPers
}
}
return nil
}
}
6 changes: 3 additions & 3 deletions CodeChallenge/Challenges/TwoSum/Entries/Aranasaurus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ let aranasaurusTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "Aranasau
var max = Datum(0, 0)
let mid = input.target / 2

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

if number > max.number {
if let diffIndex = input.numbers.indexOf(input.target - number) {
if let diffIndex = input.numbers.index(of: input.target - number) {
return (diffIndex + 1, i + 1)
}
max = Datum(i, number)
}
}

return .None
return .none
}

private struct Datum {
Expand Down
8 changes: 4 additions & 4 deletions CodeChallenge/Challenges/TwoSum/Entries/BugKrusha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

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

private func calculate(numOne: Int, numTwo: Int, calculation: (a: Int, b: Int) -> Int) -> Int {
return calculation(a: numOne, b: numTwo)
}
private func calculate(_ numOne: Int, numTwo: Int, calculation: (_ a: Int, _ b: Int) -> Int) -> Int {
return calculation(numOne, numTwo)
}
6 changes: 3 additions & 3 deletions CodeChallenge/Challenges/TwoSum/Entries/IanKeen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import Foundation

let ianKeenTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "IanKeen") { input in
for (index, digit) in input.numbers.enumerate() {
if let other = input.numbers.indexOf(input.target - digit) {
for (index, digit) in input.numbers.enumerated() {
if let other = input.numbers.index(of: input.target - digit) {
return (index + 1, Int(other) + 1)
}
}
return nil
}
}
8 changes: 4 additions & 4 deletions CodeChallenge/Challenges/TwoSum/Entries/Mosab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ let mosabTwoSumEntry = CodeChallengeEntry<TwoSumChallenge>(name: "Mosab") { inpu
var numbers = input.numbers
var dictionary : [Int : Int] = [:] //Dictionary to hold numbers and indices

for (index, number) in numbers.enumerate() {
for (index, number) in numbers.enumerated() {
dictionary[number] = index
}

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

private func findIndices(inout map : [Int:Int], inout numbers : [Int], let target : Int) -> (Int, Int)?
private func findIndices(_ map : inout [Int:Int], numbers : inout [Int], target : Int) -> (Int, Int)?
{
for (index, number) in numbers.enumerate()
for (index, number) in numbers.enumerated()
{
if let index2 = map[abs(number - target)]
{
Expand All @@ -30,4 +30,4 @@ private func findIndices(inout map : [Int:Int], inout numbers : [Int], let targe
}

return nil
}
}
Loading