Skip to content

Commit 3a24cf9

Browse files
authored
Merge pull request #932 from l-rettberg/master
Resolve Issue #931 - Update Fizz Buzz
2 parents 7c7afb9 + 51b0c2b commit 3a24cf9

File tree

6 files changed

+104
-80
lines changed

6 files changed

+104
-80
lines changed

Diff for: Fizz Buzz/FizzBuzz.playground/Contents.swift

+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)

Diff for: Fizz Buzz/FizzBuzz.playground/contents.xcplayground

-4
This file was deleted.

Diff for: Fizz Buzz/FizzBuzz.playground/playground.xcworkspace/contents.xcworkspacedata

-7
This file was deleted.

Diff for: Fizz Buzz/FizzBuzz.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

-8
This file was deleted.

Diff for: 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+
}

Diff for: 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)*

0 commit comments

Comments
 (0)