Skip to content

Commit d9ac8e2

Browse files
committed
Merge pull request kodecocodes#41 from chris-pilcher/fizz-buzz
Added fizz buzz
2 parents b9a9759 + f634578 commit d9ac8e2

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}
8+
9+
if i % 5 == 0 {
10+
result += (result.isEmpty ? "" : " ") + "Buzz"
11+
}
12+
13+
if result.isEmpty {
14+
result += "\(i)"
15+
}
16+
17+
print(result)
18+
}
19+
}
20+
21+
fizzBuzz(100)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Fizz Buzz/FizzBuzz.swift

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func fizzBuzz(numberOfTurns: Int) {
2+
for i in 1...numberOfTurns {
3+
var result = ""
4+
5+
if i % 3 == 0 {
6+
result += "Fizz"
7+
}
8+
9+
if i % 5 == 0 {
10+
result += (result.isEmpty ? "" : " ") + "Buzz"
11+
}
12+
13+
if result.isEmpty {
14+
result += "\(i)"
15+
}
16+
17+
print(result)
18+
}
19+
}

Fizz Buzz/README.markdown

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Fizz Buzz
2+
3+
Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz".
4+
5+
Fizz buzz has been used as an interview screening device for computer programmers.
6+
7+
## Example
8+
9+
A typical round of fizz buzz:
10+
11+
`1`, `2`, `Fizz`, `4`, `Buzz`, `Fizz`, `7`, `8`, `Fizz`, `Buzz`, `11`, `Fizz`, `13`, `14`, `Fizz Buzz`, `16`, `17`, `Fizz`, `19`, `Buzz`, `Fizz`, `22`, `23`, `Fizz`, `Buzz`, `26`, `Fizz`, `28`, `29`, `Fizz Buzz`, `31`, `32`, `Fizz`, `34`, `Buzz`, `Fizz`, ...
12+
13+
## Modulus Operator
14+
15+
The modulus operator `%` is the key to solving fizz buzz.
16+
17+
The modulus operator returns the remainder after an integer division. Here is an example of the modulus operator:
18+
19+
| Division | Division Result | Modulus | Modulus Result |
20+
| ------------- | -------------------------- | --------------- | ---------------:|
21+
| 1 `/` 3 | `0` with a remainder of 3 | 1 `%` 3 | `3` |
22+
| 5 `/` 3 | `1` with a remainder of 2 | 5 `%` 3 | `2` |
23+
| 16 `/` 3 | `5` with a remainder of 1 | 16 `%` 3 | `1` |
24+
25+
A common approach to determine if a number is even or odd is to use the modulus operator:
26+
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` |
31+
32+
Now we can use the modulus operator `%` to solve fizz buzz.
33+
34+
Finding numbers divisible by three:
35+
36+
| Modulus | Modulus Result | Swift Code | Swift Code Result |
37+
| ------- | --------------:| ------------- |------------------:|
38+
| 1 `%` 3 | `1` | `1 % 3 == 0` | `false` |
39+
| 2 `%` 3 | `2` | `2 % 3 == 0` | `false` |
40+
| 3 `%` 3 | `0` | `3 % 3 == 0` | `true` |
41+
| 4 `%` 3 | `1` | `4 % 3 == 0` | `false` |
42+
43+
Finding numbers divisible by five:
44+
45+
| Modulus | Modulus Result | Swift Code | Swift Code Result |
46+
| ------- | --------------:| ------------- |------------------:|
47+
| 1 `%` 5 | `1` | `1 % 5 == 0` | `false` |
48+
| 2 `%` 5 | `2` | `2 % 5 == 0` | `false` |
49+
| 3 `%` 5 | `3` | `3 % 5 == 0` | `false` |
50+
| 4 `%` 5 | `4` | `4 % 5 == 0` | `false` |
51+
| 5 `%` 5 | `0` | `5 % 5 == 0` | `true` |
52+
| 6 `%` 5 | `1` | `6 % 5 == 0` | `false` |
53+
54+
## The code
55+
56+
Here is a simple implementation in Swift:
57+
58+
```swift
59+
func fizzBuzz(numberOfTurns: Int) {
60+
for i in 1...numberOfTurns {
61+
var result = ""
62+
63+
if i % 3 == 0 {
64+
result += "Fizz"
65+
}
66+
67+
if i % 5 == 0 {
68+
result += (result.isEmpty ? "" : " ") + "Buzz"
69+
}
70+
71+
if result.isEmpty {
72+
result += "\(i)"
73+
}
74+
75+
print(result)
76+
}
77+
}
78+
```
79+
80+
Put this code in a playground and test it like so:
81+
82+
```swift
83+
fizzBuzz(15) // This will output 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz
84+
```
85+
## See also
86+
87+
[Fizz buzz on Wikipedia](https://en.wikipedia.org/wiki/Fizz_buzz)
88+
89+
*Written by [Chris Pilcher](https://github.com/chris-pilcher)*

README.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Most of the time using just the built-in `Array`, `Dictionary`, and `Set` types
166166
A lot of software developer interview questions consist of algorithmic puzzles. Here is a small selection of fun ones. For more puzzles (with answers), see [here](http://elementsofprogramminginterviews.com/) and [here](http://www.crackingthecodinginterview.com).
167167

168168
- [Two-Sum Problem](Two-Sum Problem/)
169+
- [Fizz Buzz](Fizz Buzz/)
169170

170171
## Learn more!
171172

0 commit comments

Comments
 (0)