Skip to content

Commit 1945830

Browse files
authored
Merge pull request kodecocodes#2 from raywenderlich/master
update master
2 parents a6bff78 + f895ba1 commit 1945830

File tree

35 files changed

+197
-184
lines changed

35 files changed

+197
-184
lines changed

Breadth-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
1+
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
22
var queue = Queue<Node>()
33
queue.enqueue(source)
44

Breadth-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
public class Edge: Equatable {
22
public var neighbor: Node
33

4-
public init(neighbor: Node) {
4+
public init(_ neighbor: Node) {
55
self.neighbor = neighbor
66
}
77
}
88

9-
public func == (lhs: Edge, rhs: Edge) -> Bool {
9+
public func == (_ lhs: Edge, rhs: Edge) -> Bool {
1010
return lhs.neighbor == rhs.neighbor
1111
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Graph.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ public class Graph: CustomStringConvertible, Equatable {
55
self.nodes = []
66
}
77

8-
public func addNode(label: String) -> Node {
9-
let node = Node(label: label)
8+
public func addNode(_ label: String) -> Node {
9+
let node = Node(label)
1010
nodes.append(node)
1111
return node
1212
}
1313

14-
public func addEdge(source: Node, neighbor: Node) {
15-
let edge = Edge(neighbor: neighbor)
14+
public func addEdge(_ source: Node, neighbor: Node) {
15+
let edge = Edge(neighbor)
1616
source.neighbors.append(edge)
1717
}
1818

@@ -27,7 +27,7 @@ public class Graph: CustomStringConvertible, Equatable {
2727
return description
2828
}
2929

30-
public func findNodeWithLabel(label: String) -> Node {
30+
public func findNodeWithLabel(_ label: String) -> Node {
3131
return nodes.filter { $0.label == label }.first!
3232
}
3333

@@ -50,6 +50,6 @@ public class Graph: CustomStringConvertible, Equatable {
5050
}
5151
}
5252

53-
public func == (lhs: Graph, rhs: Graph) -> Bool {
53+
public func == (_ lhs: Graph, rhs: Graph) -> Bool {
5454
return lhs.nodes == rhs.nodes
5555
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Node.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Node: CustomStringConvertible, Equatable {
55
public var distance: Int?
66
public var visited: Bool
77

8-
public init(label: String) {
8+
public init(_ label: String) {
99
self.label = label
1010
neighbors = []
1111
visited = false
@@ -22,11 +22,11 @@ public class Node: CustomStringConvertible, Equatable {
2222
return distance != nil
2323
}
2424

25-
public func remove(edge: Edge) {
26-
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
25+
public func remove(_ edge: Edge) {
26+
neighbors.remove(at: neighbors.index { $0 === edge }!)
2727
}
2828
}
2929

30-
public func == (lhs: Node, rhs: Node) -> Bool {
30+
public func == (_ lhs: Node, rhs: Node) -> Bool {
3131
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
3232
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Queue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct Queue<T> {
1313
return array.count
1414
}
1515

16-
public mutating func enqueue(element: T) {
16+
public mutating func enqueue(_ element: T) {
1717
array.append(element)
1818
}
1919

Breadth-First Search/BreadthFirstSearch.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
1+
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
22
var queue = Queue<Node>()
33
queue.enqueue(source)
44

Breadth-First Search/README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ For an unweighted graph, this tree defines a shortest path from the starting nod
9090
Simple implementation of breadth-first search using a queue:
9191

9292
```swift
93-
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
93+
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
9494
var queue = Queue<Node>()
9595
queue.enqueue(source)
9696

Brute-Force String Search/BruteForceStringSearch.playground/Contents.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
//: Playground - noun: a place where people can play
22

33
extension String {
4-
func indexOf(pattern: String) -> String.Index? {
5-
for i in self.startIndex ..< self.endIndex {
4+
func indexOf(_ pattern: String) -> String.Index? {
5+
6+
for i in self.characters.indices {
67
var j = i
78
var found = true
8-
for p in pattern.startIndex ..< pattern.endIndex {
9-
if j == self.endIndex || self[j] != pattern[p] {
9+
for p in pattern.characters.indices{
10+
if j == self.characters.endIndex || self[j] != pattern[p] {
1011
found = false
1112
break
1213
} else {
13-
j = j.successor()
14+
j = self.characters.index(after: j)
1415
}
1516
}
1617
if found {

Brute-Force String Search/BruteForceStringSearch.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)