Skip to content

Commit a3d0d8f

Browse files
authored
Merge pull request kodecocodes#1 from raywenderlich/master
update fork
2 parents 5b8c263 + dca1e2c commit a3d0d8f

File tree

36 files changed

+1089
-1070
lines changed

36 files changed

+1089
-1070
lines changed

Binary Tree/BinaryTree.playground/Contents.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@ tree.count // 12
5353

5454

5555
extension BinaryTree {
56-
public func traverseInOrder(@noescape process: T -> Void) {
56+
public func traverseInOrder(process: (T) -> Void) {
5757
if case let .Node(left, value, right) = self {
58-
left.traverseInOrder(process)
58+
left.traverseInOrder(process: process)
5959
process(value)
60-
right.traverseInOrder(process)
60+
right.traverseInOrder(process: process)
6161
}
6262
}
6363

64-
public func traversePreOrder(@noescape process: T -> Void) {
64+
public func traversePreOrder(process: (T) -> Void) {
6565
if case let .Node(left, value, right) = self {
6666
process(value)
67-
left.traversePreOrder(process)
68-
right.traversePreOrder(process)
67+
left.traversePreOrder(process: process)
68+
right.traversePreOrder(process: process)
6969
}
7070
}
7171

72-
public func traversePostOrder(@noescape process: T -> Void) {
72+
public func traversePostOrder(process: (T) -> Void) {
7373
if case let .Node(left, value, right) = self {
74-
left.traversePostOrder(process)
75-
right.traversePostOrder(process)
74+
left.traversePostOrder(process: process)
75+
right.traversePostOrder(process: process)
7676
process(value)
7777
}
7878
}

Binary Tree/README.markdown

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,26 @@ Something you often need to do with trees is traverse them, i.e. look at all the
111111
Here is how you'd implement that:
112112

113113
```swift
114-
public func traverseInOrder(@noescape process: T -> Void) {
114+
public func traverseInOrder(process: (T) -> Void) {
115115
if case let .Node(left, value, right) = self {
116-
left.traverseInOrder(process)
116+
left.traverseInOrder(process: process)
117117
process(value)
118-
right.traverseInOrder(process)
118+
right.traverseInOrder(process: process)
119119
}
120120
}
121121

122-
public func traversePreOrder(@noescape process: T -> Void) {
122+
public func traversePreOrder(process: (T) -> Void) {
123123
if case let .Node(left, value, right) = self {
124124
process(value)
125-
left.traversePreOrder(process)
126-
right.traversePreOrder(process)
125+
left.traversePreOrder(process: process)
126+
right.traversePreOrder(process: process)
127127
}
128128
}
129129

130-
public func traversePostOrder(@noescape process: T -> Void) {
130+
public func traversePostOrder(process: (T) -> Void) {
131131
if case let .Node(left, value, right) = self {
132-
left.traversePostOrder(process)
133-
right.traversePostOrder(process)
132+
left.traversePostOrder(process: process)
133+
right.traversePostOrder(process: process)
134134
process(value)
135135
}
136136
}

Bounded Priority Queue/BoundedPriorityQueue.playground/Sources/BoundedPriorityQueue.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class BoundedPriorityQueue<T: Comparable> {
1212
private typealias Node = LinkedListNode<T>
1313

1414
private(set) public var count = 0
15-
private var head: Node?
15+
fileprivate var head: Node?
1616
private var tail: Node?
1717
private var maxElements: Int
1818

@@ -28,7 +28,7 @@ public class BoundedPriorityQueue<T: Comparable> {
2828
return head?.value
2929
}
3030

31-
public func enqueue(value: T) {
31+
public func enqueue(_ value: T) {
3232
if let node = insert(value, after: findInsertionPoint(value)) {
3333
// If the newly inserted node is the last one in the list, then update
3434
// the tail pointer.
@@ -44,7 +44,7 @@ public class BoundedPriorityQueue<T: Comparable> {
4444
}
4545
}
4646

47-
private func insert(value: T, after: Node?) -> Node? {
47+
private func insert(_ value: T, after: Node?) -> Node? {
4848
if let previous = after {
4949

5050
// If the queue is full and we have to insert at the end of the list,
@@ -78,11 +78,11 @@ public class BoundedPriorityQueue<T: Comparable> {
7878

7979
/* Find the node after which to insert the new value. If this returns nil,
8080
the new value should be inserted at the head of the list. */
81-
private func findInsertionPoint(value: T) -> Node? {
81+
private func findInsertionPoint(_ value: T) -> Node? {
8282
var node = head
8383
var prev: Node? = nil
8484

85-
while let current = node where value < current.value {
85+
while let current = node, value < current.value {
8686
prev = node
8787
node = current.next
8888
}

Bounded Priority Queue/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class BoundedPriorityQueue<T: Comparable> {
3535
private typealias Node = LinkedListNode<T>
3636

3737
private(set) public var count = 0
38-
private var head: Node?
38+
fileprivate var head: Node?
3939
private var tail: Node?
4040
private var maxElements: Int
4141

@@ -55,7 +55,7 @@ public class BoundedPriorityQueue<T: Comparable> {
5555
The `BoundedPriorityQueue` class contains a doubly linked list of `LinkedListNode` objects. Nothing special here yet. The fun stuff happens in the `enqueue()` method:
5656

5757
```swift
58-
public func enqueue(value: T) {
58+
public func enqueue(_ value: T) {
5959
if let node = insert(value, after: findInsertionPoint(value)) {
6060
// If the newly inserted node is the last one in the list, then update
6161
// the tail pointer.
@@ -71,7 +71,7 @@ public func enqueue(value: T) {
7171
}
7272
}
7373

74-
private func insert(value: T, after: Node?) -> Node? {
74+
private func insert(_ value: T, after: Node?) -> Node? {
7575
if let previous = after {
7676

7777
// If the queue is full and we have to insert at the end of the list,
@@ -105,7 +105,7 @@ private func insert(value: T, after: Node?) -> Node? {
105105

106106
/* Find the node after which to insert the new value. If this returns nil,
107107
the new value should be inserted at the head of the list. */
108-
private func findInsertionPoint(value: T) -> Node? {
108+
private func findInsertionPoint(_ value: T) -> Node? {
109109
var node = head
110110
var prev: Node? = nil
111111

Fizz Buzz/FizzBuzz.playground/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func fizzBuzz(numberOfTurns: Int) {
1+
func fizzBuzz(_ numberOfTurns: Int) {
22
for i in 1...numberOfTurns {
33
var result = ""
44

Fizz Buzz/FizzBuzz.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Fizz Buzz/FizzBuzz.playground/timeline.xctimeline

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

Fizz Buzz/FizzBuzz.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func fizzBuzz(numberOfTurns: Int) {
1+
func fizzBuzz(_ numberOfTurns: Int) {
22
for i in 1...numberOfTurns {
33
var result = ""
44

Fizz Buzz/README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Finding numbers divisible by five:
5858
Here is a simple implementation in Swift:
5959

6060
```swift
61-
func fizzBuzz(numberOfTurns: Int) {
61+
func fizzBuzz(_ numberOfTurns: Int) {
6262
for i in 1...numberOfTurns {
6363
var result = ""
6464

GCD/GCD.playground/Contents.swift

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

33
// Recursive version
4-
func gcd(a: Int, _ b: Int) -> Int {
4+
func gcd(_ a: Int, _ b: Int) -> Int {
55
let r = a % b
66
if r != 0 {
77
return gcd(b, r)
@@ -26,7 +26,7 @@ func gcd(m: Int, _ n: Int) -> Int {
2626
}
2727
*/
2828

29-
func lcm(m: Int, _ n: Int) -> Int {
29+
func lcm(_ m: Int, _ n: Int) -> Int {
3030
return m*n / gcd(m, n)
3131
}
3232

GCD/GCD.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Euclid's algorithm for finding the greatest common divisor
33
*/
4-
func gcd(m: Int, _ n: Int) -> Int {
4+
func gcd(_ m: Int, _ n: Int) -> Int {
55
var a = 0
66
var b = max(m, n)
77
var r = min(m, n)
@@ -16,7 +16,7 @@ func gcd(m: Int, _ n: Int) -> Int {
1616

1717
/*
1818
// Recursive version
19-
func gcd(a: Int, _ b: Int) -> Int {
19+
func gcd(_ a: Int, _ b: Int) -> Int {
2020
let r = a % b
2121
if r != 0 {
2222
return gcd(b, r)
@@ -29,6 +29,6 @@ func gcd(a: Int, _ b: Int) -> Int {
2929
/*
3030
Returns the least common multiple of two numbers.
3131
*/
32-
func lcm(m: Int, _ n: Int) -> Int {
32+
func lcm(_ m: Int, _ n: Int) -> Int {
3333
return m*n / gcd(m, n)
3434
}

GCD/README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ where `a % b` calculates the remainder of `a` divided by `b`.
1717
Here is an implementation of this idea in Swift:
1818

1919
```swift
20-
func gcd(a: Int, _ b: Int) -> Int {
20+
func gcd(_ a: Int, _ b: Int) -> Int {
2121
let r = a % b
2222
if r != 0 {
2323
return gcd(b, r)
@@ -68,7 +68,7 @@ gcd(841, 299) // 1
6868
Here is a slightly different implementation of Euclid's algorithm. Unlike the first version this doesn't use recursion but only a basic `while` loop.
6969

7070
```swift
71-
func gcd(m: Int, _ n: Int) -> Int {
71+
func gcd(_ m: Int, _ n: Int) -> Int {
7272
var a = 0
7373
var b = max(m, n)
7474
var r = min(m, n)
@@ -101,7 +101,7 @@ We can calculate the LCM using Euclid's algorithm too:
101101
In code:
102102

103103
```swift
104-
func lcm(m: Int, _ n: Int) -> Int {
104+
func lcm(_ m: Int, _ n: Int) -> Int {
105105
return m*n / gcd(m, n)
106106
}
107107
```
Lines changed: 10 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,34 @@
11
//: Playground - noun: a place where people can play
22

3-
public struct HashTable<Key: Hashable, Value> {
4-
private typealias Element = (key: Key, value: Value)
5-
private typealias Bucket = [Element]
6-
7-
private var buckets: [Bucket]
8-
private(set) var count = 0
9-
10-
public init(capacity: Int) {
11-
assert(capacity > 0)
12-
buckets = .init(count: capacity, repeatedValue: [])
13-
}
14-
15-
public var isEmpty: Bool {
16-
return count == 0
17-
}
18-
19-
private func indexForKey(key: Key) -> Int {
20-
return abs(key.hashValue) % buckets.count
21-
}
22-
}
23-
24-
extension HashTable {
25-
public subscript(key: Key) -> Value? {
26-
get {
27-
return valueForKey(key)
28-
}
29-
set {
30-
if let value = newValue {
31-
updateValue(value, forKey: key)
32-
} else {
33-
removeValueForKey(key)
34-
}
35-
}
36-
}
37-
38-
public func valueForKey(key: Key) -> Value? {
39-
let index = indexForKey(key)
40-
41-
for element in buckets[index] {
42-
if element.key == key {
43-
return element.value
44-
}
45-
}
46-
return nil // key not in hash table
47-
}
48-
49-
public mutating func updateValue(value: Value, forKey key: Key) -> Value? {
50-
let index = indexForKey(key)
51-
52-
// Do we already have this key in the bucket?
53-
for (i, element) in buckets[index].enumerate() {
54-
if element.key == key {
55-
let oldValue = element.value
56-
buckets[index][i].value = value
57-
return oldValue
58-
}
59-
}
60-
61-
// This key isn't in the bucket yet; add it to the chain.
62-
buckets[index].append((key: key, value: value))
63-
count += 1
64-
return nil
65-
}
66-
67-
public mutating func removeValueForKey(key: Key) -> Value? {
68-
let index = indexForKey(key)
69-
70-
// Find the element in the bucket's chain and remove it.
71-
for (i, element) in buckets[index].enumerate() {
72-
if element.key == key {
73-
buckets[index].removeAtIndex(i)
74-
count -= 1
75-
return element.value
76-
}
77-
}
78-
return nil // key not in hash table
79-
}
80-
81-
public mutating func removeAll() {
82-
buckets = .init(count: buckets.count, repeatedValue: [])
83-
count = 0
84-
}
85-
}
86-
87-
extension HashTable: CustomStringConvertible {
88-
public var description: String {
89-
return buckets.flatMap { b in b.map { e in "\(e.key) = \(e.value)" } }.joinWithSeparator(", ")
90-
}
91-
}
92-
93-
extension HashTable: CustomDebugStringConvertible {
94-
public var debugDescription: String {
95-
var s = ""
96-
for (i, bucket) in buckets.enumerate() {
97-
s += "bucket \(i): " + bucket.map { e in "\(e.key) = \(e.value)" }.joinWithSeparator(", ") + "\n"
98-
}
99-
return s
100-
}
101-
}
102-
103-
104-
1053
// Playing with hash values
4+
1065
"firstName".hashValue
1076
abs("firstName".hashValue) % 5
7+
1088
"lastName".hashValue
1099
abs("lastName".hashValue) % 5
10+
11011
"hobbies".hashValue
11112
abs("hobbies".hashValue) % 5
11213

113-
114-
11514
// Playing with the hash table
15+
11616
var hashTable = HashTable<String, String>(capacity: 5)
11717

11818
hashTable["firstName"] = "Steve"
11919
hashTable["lastName"] = "Jobs"
12020
hashTable["hobbies"] = "Programming Swift"
12121

122-
hashTable.description
22+
print(hashTable)
12323
print(hashTable.debugDescription)
12424

12525
let x = hashTable["firstName"]
12626
hashTable["firstName"] = "Tim"
27+
12728
let y = hashTable["firstName"]
12829
hashTable["firstName"] = nil
30+
12931
let z = hashTable["firstName"]
32+
33+
print(hashTable)
34+
print(hashTable.debugDescription)

0 commit comments

Comments
 (0)