Skip to content

Commit b23f10d

Browse files
authored
Merge branch 'master' into Update-Protocols
2 parents 0513f7d + bcd9eb3 commit b23f10d

File tree

30 files changed

+157
-3092
lines changed

30 files changed

+157
-3092
lines changed

.travis.yml

-55
This file was deleted.

Big-O Notation.markdown

+2-3
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
```

Binary Search Tree/README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ As a result, doing `tree.search(100)` gives nil.
557557
You can check whether a tree is a valid binary search tree with the following method:
558558

559559
```swift
560-
public func isBST(minValue minValue: T, maxValue: T) -> Bool {
560+
public func isBST(minValue: T, maxValue: T) -> Bool {
561561
if value < minValue || value > maxValue { return false }
562562
let leftBST = left?.isBST(minValue: minValue, maxValue: value) ?? true
563563
let rightBST = right?.isBST(minValue: value, maxValue: maxValue) ?? true

Count Occurrences/README.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ In code this looks as follows:
2525
func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
2626
var leftBoundary: Int {
2727
var low = 0
28-
var high = a.count
28+
var high = array.count
2929
while low < high {
3030
let midIndex = low + (high - low)/2
3131
if a[midIndex] < key {
@@ -39,7 +39,7 @@ func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
3939

4040
var rightBoundary: Int {
4141
var low = 0
42-
var high = a.count
42+
var high = array.count
4343
while low < high {
4444
let midIndex = low + (high - low)/2
4545
if a[midIndex] > key {
@@ -62,7 +62,7 @@ To test this algorithm, copy the code to a playground and then do:
6262
```swift
6363
let a = [ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
6464

65-
countOccurrencesOfKey(3, inArray: a) // returns 4
65+
countOccurrences(of: 3, in: a) // returns 4
6666
```
6767

6868
> **Remember:** If you use your own array, make sure it is sorted first!
+17-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
// last checked with Xcode 10.0 (10A255)
1+
// Last checked with Xcode Version 11.4.1 (11E503a)
22

33
func fizzBuzz(_ numberOfTurns: Int) {
4-
for i in 1...numberOfTurns {
5-
var result = ""
6-
7-
if i % 3 == 0 {
8-
result += "Fizz"
4+
guard numberOfTurns >= 1 else {
5+
print("Number of turns must be >= 1")
6+
return
97
}
10-
11-
if i % 5 == 0 {
12-
result += (result.isEmpty ? "" : " ") + "Buzz"
8+
9+
for i in 1...numberOfTurns {
10+
switch (i.isMultiple(of: 3), i.isMultiple(of: 5)) {
11+
case (false, false):
12+
print("\(i)")
13+
case (true, false):
14+
print("Fizz")
15+
case (false, true):
16+
print("Buzz")
17+
case (true, true):
18+
print("Fizz Buzz")
19+
}
1320
}
14-
15-
if result.isEmpty {
16-
result += "\(i)"
17-
}
18-
19-
print(result)
20-
}
2121
}
2222

23-
fizzBuzz(100)
23+
fizzBuzz(15)

Fizz Buzz/FizzBuzz.playground/contents.xcplayground

-4
This file was deleted.

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

-7
This file was deleted.

Fizz Buzz/FizzBuzz.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

-8
This file was deleted.

Fizz Buzz/FizzBuzz.swift

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
func fizzBuzz(_ numberOfTurns: Int) {
2-
for i in 1...numberOfTurns {
3-
var result = ""
4-
5-
if i % 3 == 0 {
6-
result += "Fizz"
7-
}
1+
// Last checked with Xcode Version 11.4.1 (11E503a)
82

9-
if i % 5 == 0 {
10-
result += (result.isEmpty ? "" : " ") + "Buzz"
3+
func fizzBuzz(_ numberOfTurns: Int) {
4+
guard numberOfTurns >= 1 else {
5+
print("Number of turns must be >= 1")
6+
return
117
}
12-
13-
if result.isEmpty {
14-
result += "\(i)"
8+
9+
for i in 1...numberOfTurns {
10+
switch (i.isMultiple(of: 3), i.isMultiple(of: 5)) {
11+
case (false, false):
12+
print("\(i)")
13+
case (true, false):
14+
print("Fizz")
15+
case (false, true):
16+
print("Buzz")
17+
case (true, true):
18+
print("Fizz Buzz")
19+
}
1520
}
16-
17-
print(result)
18-
}
19-
}
21+
}

Fizz Buzz/README.markdown

+69-28
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,48 @@ The modulus operator `%` is the key to solving fizz buzz.
1616

1717
The modulus operator returns the remainder after an integer division. Here is an example of the modulus operator:
1818

19-
| Division | Division Result | Modulus | Modulus Result |
20-
| ------------- | -------------------------- | --------------- | ---------------:|
21-
| 1 / 3 | 0 with a remainder of 3 | 1 % 3 | 1 |
22-
| 5 / 3 | 1 with a remainder of 2 | 5 % 3 | 2 |
23-
| 16 / 3 | 5 with a remainder of 1 | 16 % 3 | 1 |
19+
| Division | Division Result | Modulus | Modulus Result|
20+
| ----------- | --------------------- | ------------- | :-----------: |
21+
| 1 / 3 | 0 with a remainder of 3 | 1 % 3 | 1 |
22+
| 5 / 3 | 1 with a remainder of 2 | 5 % 3 | 2 |
23+
| 16 / 3 | 5 with a remainder of 1 | 16 % 3 | 1 |
2424

2525
A common approach to determine if a number is even or odd is to use the modulus operator:
2626

27-
| Modulus | Result | Swift Code | Swift Code Result | Comment |
28-
| ------------- | ---------------:| ------------------------------- | -----------------:| --------------------------------------------- |
29-
| 6 % 2 | 0 | `let isEven = (number % 2 == 0)` | `true` | If a number is divisible by 2 it is *even* |
30-
| 5 % 2 | 1 | `let isOdd = (number % 2 != 0)` | `true` | If a number is not divisible by 2 it is *odd* |
27+
| Modulus | Result | Swift Code | Swift Code<br>Result | Comment |
28+
| -------- | :-----:| -------------------------------- | :----------------:| --------------------------------------------- |
29+
| 6 % 2 | 0 | `let isEven = (number % 2 == 0)` | `true` | If a number is divisible by 2 it is *even* |
30+
| 5 % 2 | 1 | `let isOdd = (number % 2 != 0)` | `true` | If a number is not divisible by 2 it is *odd* |
31+
32+
Alternatively, Swift's built in function .isMultiple(of:) can be used, i.e. 6.isMultiple(of: 2) will return true, 5.isMultiple(of: 2) will return false
3133

3234
## Solving fizz buzz
3335

34-
Now we can use the modulus operator `%` to solve fizz buzz.
36+
Now we can use the modulus operator `%` or .isMultiple(of:) method to solve fizz buzz.
3537

3638
Finding numbers divisible by three:
3739

38-
| Modulus | Modulus Result | Swift Code | Swift Code Result |
39-
| ------- | --------------:| ------------- |------------------:|
40-
| 1 % 3 | 1 | `1 % 3 == 0` | `false` |
41-
| 2 % 3 | 2 | `2 % 3 == 0` | `false` |
42-
| 3 % 3 | 0 | `3 % 3 == 0` | `true` |
43-
| 4 % 3 | 1 | `4 % 3 == 0` | `false` |
40+
| Modulus | Modulus<br>Result | Swift Code<br>using Modulo | Swift Code<br>using .isMultiple(of:) | Swift Code<br>Result |
41+
| ------- | :---------------: | -------------------------- | ------------------------------------ | ------------------- |
42+
|1 % 3 | 1 | `1 % 3 == 0` | `1.isMultiple(of: 3)` | `false` |
43+
|2 % 3 | 2 | `2 % 3 == 0` | `2.isMultiple(of: 3)` | `false` |
44+
|3 % 3 | 0 | `3 % 3 == 0` | `3.isMultiple(of: 3)` | `true` |
45+
|4 % 3 | 1 | `4 % 3 == 0` | `4.isMultiple(of: 3)` | `false` |
4446

4547
Finding numbers divisible by five:
4648

47-
| Modulus | Modulus Result | Swift Code | Swift Code Result |
48-
| ------- | --------------:| ------------- |------------------:|
49-
| 1 % 5 | 1 | `1 % 5 == 0` | `false` |
50-
| 2 % 5 | 2 | `2 % 5 == 0` | `false` |
51-
| 3 % 5 | 3 | `3 % 5 == 0` | `false` |
52-
| 4 % 5 | 4 | `4 % 5 == 0` | `false` |
53-
| 5 % 5 | 0 | `5 % 5 == 0` | `true` |
54-
| 6 % 5 | 1 | `6 % 5 == 0` | `false` |
49+
| Modulus | Modulus<br>Result | Swift Code<br>using Modulo | Swift Code<br>using .isMultiple(of:) | Swift Code<br>Result |
50+
| ------- | :---------------: | -------------------------- | ------------------------------------ | -------------------- |
51+
| 1 % 5 | 1 | `1 % 5 == 0` | `1.isMultiple(of: 5)` | `false` |
52+
| 2 % 5 | 2 | `2 % 5 == 0` | `2.isMultiple(of: 5)` | `false` |
53+
| 3 % 5 | 3 | `3 % 5 == 0` | `3.isMultiple(of: 5)` | `false` |
54+
| 4 % 5 | 4 | `4 % 5 == 0` | `4.isMultiple(of: 5)` | `false` |
55+
| 5 % 5 | 0 | `5 % 5 == 0` | `5.isMultiple(of: 5)` | `true` |
56+
| 6 % 5 | 1 | `6 % 5 == 0` | `6.isMultiple(of: 5)` | `false` |
5557

5658
## The code
5759

58-
Here is a simple implementation in Swift:
60+
Here is a simple implementation in Swift using Modulus approach
5961

6062
```swift
6163
func fizzBuzz(_ numberOfTurns: Int) {
@@ -79,18 +81,57 @@ func fizzBuzz(_ numberOfTurns: Int) {
7981
}
8082
```
8183

82-
Put this code in a playground and test it like so:
84+
Here is simple implementation in Swift using .isMultiple(of:) and switch statement
85+
86+
```swift
87+
func fizzBuzz(_ numberOfTurns: Int) {
88+
guard numberOfTurns >= 1 else {
89+
print("Number of turns must be >= 1")
90+
return
91+
}
92+
93+
for i in 1...numberOfTurns {
94+
switch (i.isMultiple(of: 3), i.isMultiple(of: 5)) {
95+
case (false, false):
96+
print("\(i)")
97+
case (true, false):
98+
print("Fizz")
99+
case (false, true):
100+
print("Buzz")
101+
case (true, true):
102+
print("Fizz Buzz")
103+
}
104+
}
105+
}
106+
```
107+
108+
Put either code in a playground and test it like so:
83109

84110
```swift
85111
fizzBuzz(15)
86112
```
87113

88114
This will output:
89115

90-
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz
116+
1
117+
2
118+
Fizz
119+
4
120+
Buzz
121+
Fizz
122+
7
123+
8
124+
Fizz
125+
Buzz
126+
11
127+
Fizz
128+
13
129+
14
130+
Fizz Buzz
91131

92132
## See also
93133

94134
[Fizz buzz on Wikipedia](https://en.wikipedia.org/wiki/Fizz_buzz)
95135

96-
*Written by [Chris Pilcher](https://github.com/chris-pilcher)*
136+
*Originally written by [Chris Pilcher](https://github.com/chris-pilcher)*<br>
137+
*Updated by [Lance Rettberg](https://github.com/l-rettberg)*

Graph/Graph/Edge.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extension Edge: CustomStringConvertible {
2929

3030
extension Edge: Hashable {
3131

32-
3332
public func hash(into hasher: inout Hasher) {
3433
hasher.combine(from)
3534
hasher.combine(to)
@@ -38,6 +37,7 @@ extension Edge: Hashable {
3837
}
3938
}
4039

40+
4141
}
4242

4343
public func == <T>(lhs: Edge<T>, rhs: Edge<T>) -> Bool {

Graph/Graph/Vertex.swift

+3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ extension Vertex: CustomStringConvertible {
2525
extension Vertex: Hashable {
2626

2727

28+
2829

2930
public func hasher(into hasher: inout Hasher){
3031

3132
hasher.combine(data)
3233
hasher.combine(index)
3334
}
3435

36+
37+
3538
}
3639

3740
public func ==<T>(lhs: Vertex<T>, rhs: Vertex<T>) -> Bool {

HaversineDistance/HaversineDistance.playground/Contents.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import UIKit
22

3-
func haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double {
3+
func haversineDistance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double {
44

55
let haversin = { (angle: Double) -> Double in
66
return (1 - cos(angle))/2
@@ -27,4 +27,4 @@ let amsterdam = (52.3702, 4.8952)
2727
let newYork = (40.7128, -74.0059)
2828

2929
// Google says it's 5857 km so our result is only off by 2km which could be due to all kinds of things, not sure how google calculates the distance or which latitude and longitude google uses to calculate the distance.
30-
haversineDinstance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)
30+
haversineDistance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)

Heap Sort/README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ As you can see, the largest items are making their way to the back. We repeat th
4242

4343
> **Note:** This process is very similar to [selection sort](../Selection%20Sort/), which repeatedly looks for the minimum item in the remainder of the array. Extracting the minimum or maximum value is what heaps are good at.
4444
45-
Performance of heap sort is **O(n lg n)** in best, worst, and average case. Because we modify the array directly, heap sort can be performed in-place. But it is not a stable sort: the relative order of identical elements is not preserved.
45+
Performance of heap sort is **O(n log n)** in best, worst, and average case. Because we modify the array directly, heap sort can be performed in-place. But it is not a stable sort: the relative order of identical elements is not preserved.
4646

4747
Here's how you can implement heap sort in Swift:
4848

0 commit comments

Comments
 (0)