Skip to content

Commit 4aacbc8

Browse files
authored
Merge pull request #2 from raywenderlich/master
update
2 parents d747f76 + 041c005 commit 4aacbc8

File tree

14 files changed

+34
-2998
lines changed

14 files changed

+34
-2998
lines changed

.travis.yml

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

Big-O Notation.markdown

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ Below are some examples for each category of performance:
124124
func solveHanoi(n: Int, from: String, to: String, spare: String) {
125125
guard n >= 1 else { return }
126126
if n > 1 {
127-
solveHanoi(n: n - 1, from: from, to: spare, spare: to)
128-
} else {
129-
solveHanoi(n: n - 1, from: spare, to: to, spare: from)
127+
solveHanoi(n: n - 1, from: from, to: spare, spare: to)
128+
solveHanoi(n: n - 1, from: spare, to: to, spare: from)
130129
}
131130
}
132131
```

Heap/Heap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ extension Heap where T: Equatable {
212212
return nodes.index(where: { $0 == node })
213213
}
214214

215-
/** Removes the first occurrence of a node from the heap. Performance: O(n log n). */
215+
/** Removes the first occurrence of a node from the heap. Performance: O(n). */
216216
@discardableResult public mutating func remove(node: T) -> T? {
217217
if let index = index(of: node) {
218218
return remove(at: index)

Queue/Queue-Optimized.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct Queue<T> {
2323
}
2424

2525
public mutating func dequeue() -> T? {
26-
guard head < array.count, let element = array[head] else { return nil }
26+
guard let element = array[guarded: head] else { return nil }
2727

2828
array[head] = nil
2929
head += 1
@@ -45,3 +45,12 @@ public struct Queue<T> {
4545
}
4646
}
4747
}
48+
49+
extension Array {
50+
subscript(guarded idx: Int) -> Element? {
51+
guard (startIndex..<endIndex).contains(idx) else {
52+
return nil
53+
}
54+
return self[idx]
55+
}
56+
}

Quicksort/Quicksort.playground/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int
4242
var i = low
4343
for j in low..<high {
4444
if a[j] <= pivot {
45-
(a[i], a[j]) = (a[j], a[i])
45+
a.swapAt(i, j)
4646
i += 1
4747
}
4848
}
4949

50-
(a[i], a[high]) = (a[high], a[i])
50+
a.swapAt(i, high)
5151
return i
5252
}
5353

Quicksort/README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int
137137
var i = low
138138
for j in low..<high {
139139
if a[j] <= pivot {
140-
(a[i], a[j]) = (a[j], a[i])
140+
a.swapAt(i, j)
141141
i += 1
142142
}
143143
}
144144

145-
(a[i], a[high]) = (a[high], a[i])
145+
a.swapAt(i, high)
146146
return i
147147
}
148148
```

Simulated annealing/simann_example.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension Tour {
138138
}
139139

140140
func shuffle() {
141-
self.shuffle()
141+
self.tour.shuffle()
142142
}
143143
}
144144

Skip-List/SkipList.playground/Contents.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ print("Hello, Swift 4!")
55

66
// SkipList is ready for Swift 4.
77
// TODO: Add Test
8+
9+
let k = SkipList<Int, String>()
10+
k.insert(key: 10, data: "10")
11+
k.insert(key: 12, data: "12")
12+
k.insert(key: 13, data: "13")
13+
k.insert(key: 20, data: "20")
14+
k.insert(key: 24, data: "24")
15+
16+
if let value = k.get(key: 20) {
17+
print(value)
18+
} else {
19+
print("not found!")
20+
}

Skip-List/SkipList.playground/Sources/SkipList.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ extension SkipList {
212212
}
213213
}
214214

215-
func insert(key: Key, data: Payload) {
215+
public func insert(key: Key, data: Payload) {
216216
if head != nil {
217217
if let node = findNode(key: key) {
218218
// replace, in case of key already exists.

Skip-List/SkipList.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ extension SkipList {
212212
}
213213
}
214214

215-
func insert(key: Key, data: Payload) {
215+
public func insert(key: Key, data: Payload) {
216216
if head != nil {
217217
if let node = findNode(key: key) {
218218
// replace, in case of key already exists.

0 commit comments

Comments
 (0)