Skip to content

Commit d4b54ff

Browse files
authored
Merge pull request kodecocodes#4 from raywenderlich/master
update master
2 parents 6e281b9 + 5f64dc0 commit d4b54ff

File tree

20 files changed

+50
-90
lines changed

20 files changed

+50
-90
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ script:
3838
# - xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
3939
# - xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
4040
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
41-
# - xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
41+
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
4242
- xcodebuild test -project ./Treap/Treap/Treap.xcodeproj -scheme Tests

Longest Common Subsequence/README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ func backtrack(_ matrix: [[Int]]) -> String {
152152
}
153153
}
154154

155-
return String(lcs.characters.reverse())
155+
return String(lcs.characters.reversed())
156156
}
157157
```
158158

159159
This backtracks from `matrix[n+1][m+1]` (bottom-right corner) to `matrix[1][1]` (top-right corner), looking for characters that are common to both strings. It adds those characters to a new string, `lcs`.
160160

161161
The `charInSequence` variable is an index into the string given by `self`. Initially this points to the last character of the string. Each time we decrement `i`, we also move back `charInSequence`. When the two characters are found to be equal, we add the character at `self[charInSequence]` to the new `lcs` string. (We can't just write `self[i]` because `i` may not map to the current position inside the Swift string.)
162162

163-
Due to backtracking, characters are added in reverse order, so at the end of the function we call `reverse()` to put the string in the right order. (Appending new characters to the end of the string and then reversing it once is faster than always inserting the characters at the front of the string.)
163+
Due to backtracking, characters are added in reverse order, so at the end of the function we call `reversed()` to put the string in the right order. (Appending new characters to the end of the string and then reversing it once is faster than always inserting the characters at the front of the string.)
164164

165165
## Putting it all together
166166

README.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Bad sorting algorithms (don't use these!):
103103
- [Shunting Yard Algorithm](Shunting Yard/). Convert infix expressions to postfix.
104104
- Statistics
105105
- [Karatsuba Multiplication](Karatsuba Multiplication/). Another take on elementary multiplication.
106+
- [Haversine Distance](HaversineDistance/). Calculating the distance between 2 points from a sphere.
106107

107108
### Machine learning
108109

Shuffle/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Array {
1212
var temp = [Element]()
1313
while !isEmpty {
1414
let i = random(count)
15-
let obj = removeAtIndex(i)
15+
let obj = remove(at: i)
1616
temp.append(obj)
1717
}
1818
self = temp
@@ -44,7 +44,7 @@ Here is a much improved version of the shuffle algorithm:
4444
```swift
4545
extension Array {
4646
public mutating func shuffle() {
47-
for i in (count - 1).stride(through: 1, by: -1) {
47+
for i in stride(from: count - 1, through: 1, by: -1) {
4848
let j = random(i + 1)
4949
if i != j {
5050
swap(&self[i], &self[j])
@@ -96,8 +96,8 @@ There is a slight variation on this algorithm that is useful for when you want t
9696
Here is the code:
9797

9898
```swift
99-
public func shuffledArray(n: Int) -> [Int] {
100-
var a = [Int](count: n, repeatedValue: 0)
99+
public func shuffledArray(_ n: Int) -> [Int] {
100+
var a = [Int](repeating: 0, count: n)
101101
for i in 0..<n {
102102
let j = random(i + 1)
103103
if i != j {

Shuffle/Shuffle.playground/Contents.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Foundation
44

55
/* Returns a random integer between 0 and n-1. */
6-
public func random(n: Int) -> Int {
6+
public func random(_ n: Int) -> Int {
77
return Int(arc4random_uniform(UInt32(n)))
88
}
99

@@ -12,7 +12,7 @@ public func random(n: Int) -> Int {
1212
/* Fisher-Yates / Knuth shuffle */
1313
extension Array {
1414
public mutating func shuffle() {
15-
for i in (count - 1).stride(through: 1, by: -1) {
15+
for i in stride(from: count - 1, through: 1, by: -1) {
1616
let j = random(i + 1)
1717
if i != j {
1818
swap(&self[i], &self[j])
@@ -29,8 +29,8 @@ list.shuffle()
2929

3030

3131
/* Create a new array of numbers that is already shuffled. */
32-
public func shuffledArray(n: Int) -> [Int] {
33-
var a = [Int](count: n, repeatedValue: 0)
32+
public func shuffledArray(_ n: Int) -> [Int] {
33+
var a = [Int](repeating: 0, count: n)
3434
for i in 0..<n {
3535
let j = random(i + 1)
3636
if i != j {

Topological Sort/Graph.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Graph: CustomStringConvertible {
77
adjacencyLists = [Node : [Node]]()
88
}
99

10-
public func addNode(value: Node) -> Node {
10+
public func addNode(_ value: Node) -> Node {
1111
adjacencyLists[value] = []
1212
return value
1313
}

Topological Sort/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@
9292
isa = PBXProject;
9393
attributes = {
9494
LastSwiftUpdateCheck = 0720;
95-
LastUpgradeCheck = 0720;
95+
LastUpgradeCheck = 0800;
9696
ORGANIZATIONNAME = "Swift Algorithm Club";
9797
TargetAttributes = {
9898
7B2BBC7F1C779D720067B71D = {
9999
CreatedOnToolsVersion = 7.2;
100+
LastSwiftMigration = 0800;
100101
};
101102
};
102103
};
@@ -157,8 +158,10 @@
157158
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
158159
CLANG_WARN_EMPTY_BODY = YES;
159160
CLANG_WARN_ENUM_CONVERSION = YES;
161+
CLANG_WARN_INFINITE_RECURSION = YES;
160162
CLANG_WARN_INT_CONVERSION = YES;
161163
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
164+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
162165
CLANG_WARN_UNREACHABLE_CODE = YES;
163166
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
164167
CODE_SIGN_IDENTITY = "-";
@@ -201,8 +204,10 @@
201204
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
202205
CLANG_WARN_EMPTY_BODY = YES;
203206
CLANG_WARN_ENUM_CONVERSION = YES;
207+
CLANG_WARN_INFINITE_RECURSION = YES;
204208
CLANG_WARN_INT_CONVERSION = YES;
205209
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
210+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
206211
CLANG_WARN_UNREACHABLE_CODE = YES;
207212
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
208213
CODE_SIGN_IDENTITY = "-";
@@ -221,6 +226,7 @@
221226
MACOSX_DEPLOYMENT_TARGET = 10.11;
222227
MTL_ENABLE_DEBUG_INFO = NO;
223228
SDKROOT = macosx;
229+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
224230
};
225231
name = Release;
226232
};
@@ -232,6 +238,7 @@
232238
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
233239
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
234240
PRODUCT_NAME = "$(TARGET_NAME)";
241+
SWIFT_VERSION = 3.0;
235242
};
236243
name = Debug;
237244
};
@@ -243,6 +250,7 @@
243250
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
244251
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
245252
PRODUCT_NAME = "$(TARGET_NAME)";
253+
SWIFT_VERSION = 3.0;
246254
};
247255
name = Release;
248256
};

Topological Sort/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.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 = "0720"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Topological Sort/Tests/TopologicalSortTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Foundation
22
import XCTest
33

44
extension Graph {
5-
public func loadEdgeList(lines: [String]) {
5+
public func loadEdgeList(_ lines: [String]) {
66
for line in lines {
7-
let items = line.componentsSeparatedByString(" ").filter { s in !s.isEmpty }
7+
let items = line.components(separatedBy: " ").filter { s in !s.isEmpty }
88
if adjacencyList(forNode: items[0]) == nil {
99
addNode(items[0])
1010
}
@@ -20,10 +20,10 @@ class TopologicalSort: XCTestCase {
2020

2121
// The topological sort is valid if a node does not have any of its
2222
// predecessors in its adjacency list.
23-
func checkIsValidTopologicalSort(graph: Graph, _ a: [Graph.Node]) {
24-
for i in (a.count - 1).stride(to: 0, by: -1) {
23+
func checkIsValidTopologicalSort(_ graph: Graph, _ a: [Graph.Node]) {
24+
for i in stride(from: (a.count - 1), to: 0, by: -1) {
2525
if let neighbors = graph.adjacencyList(forNode: a[i]) {
26-
for j in (i - 1).stride(through: 0, by: -1) {
26+
for j in stride(from: (i - 1), through: 0, by: -1) {
2727
XCTAssertFalse(neighbors.contains(a[j]), "\(a) is not a valid topological sort")
2828
}
2929
}

Topological Sort/Topological Sort.playground/Sources/Graph.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Graph: CustomStringConvertible {
77
adjacencyLists = [Node : [Node]]()
88
}
99

10-
public func addNode(value: Node) -> Node {
10+
public func addNode(_ value: Node) -> Node {
1111
adjacencyLists[value] = []
1212
return value
1313
}

0 commit comments

Comments
 (0)