Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b23f10d

Browse files
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

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
```

‎Binary Search Tree/README.markdown

Lines changed: 1 addition & 1 deletion
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(minValueminValue: 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

Lines changed: 3 additions & 3 deletions
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!
Lines changed: 17 additions & 17 deletions
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

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

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

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

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

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

‎Fizz Buzz/FizzBuzz.swift

Lines changed: 18 additions & 16 deletions
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

Lines changed: 69 additions & 28 deletions
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 | ModulusResult | Swift Code| Swift CodeResult |
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 | ModulusResult | Swift Code| Swift CodeResult |
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)*

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /