Getting Started with FizzBuzz in Swift
suggest changeFor those of you who are new to programming in Swift and those of you coming from different programming backgrounds, such as Python or Java, this article should be quite helpful. In this post, we will discuss a simple solution for implementing Swift algorithms.
Fizz Buzz
You may have seen Fizz Buzz written as Fizz Buzz, FizzBuzz, or Fizz-Buzz; they’re all referring to the same thing. That “thing” is the main topic of discussion today. First, what is FizzBuzz?
This is a common question that comes up in job interviews.
Imagine a series of numbers from 1 to 10.
1 2 3 4 5 6 7 8 9 10
Fizz and Buzz refer to any number that’s a multiple of 3 and 5 respectively. In other words, if a number is divisible by 3, it is substituted with fizz; if a number is divisible by 5, it is substituted with buzz. If a number is simultaneously a multiple of 3 AND 5, the number is replaced with “fizz buzz.” In essence, it emulates the famous children's game “fizz buzz”.
To work on this problem, open up Xcode to create a new playground and initialize an array like below:
// for example
let number = [1,2,3,4,5]
// here 3 is fizz and 5 is buzz
To find all the fizz and buzz, we must iterate through the array and check which numbers are fizz and which are buzz. To do this, create a for loop to iterate through the array we have initialized:
for num in number {
// Body and calculation goes here
}
After this, we can simply use the “if else” condition and the modulo operator in Swift, %, to locate the fizz and buzz.
for num in number {
if num % 3 == 0 {
print("\(num) fizz")
} else {
print(num)
}
}
Great! You can go to the debug console in an Xcode playground to see the output. You will find that the multiples of 3 have been labeled fizz.
For the Buzz part, we will use the same technique. Let’s give it a try before scrolling through the article — you can check your results against this article once you’ve finished doing this.
for num in number {
if num % 3 == 0 {
print("\(num) fizz")
} else if num % 5 == 0 {
print("\(num) buzz")
} else {
print(num)
}
}
Check the output!
It’s rather straightforward: print fizz when the number is divisible by 3, and buzz when it is divisible by 5. Now, increase the numbers in the array.
let numbers = 1...15
The range is now 1...15 so that 15, a multiple of both 3 and 5, appears. That number should be replaced with “fizz buzz.” Try for yourself and check the answer!
Here is the solution:
for num in numbers {
if num % 3 == 0 && num % 5 == 0 {
print("\(num) fizz buzz")
} else if num % 3 == 0 {
print("\(num) fizz")
} else if num % 5 == 0 {
print("\(num) buzz")
} else {
print(num)
}
}
The conditions are evaluated in order for each number. Test the combined case first; otherwise, 15 would match the test for 3 before the program reached the combined case. Swift’s isMultiple(of:) method states the divisibility test directly.
Here is the final code:
let numbers = 1...15
for number in numbers {
switch (number.isMultiple(of: 3), number.isMultiple(of: 5)) {
case (true, true):
print("FizzBuzz")
case (true, false):
print("Fizz")
case (false, true):
print("Buzz")
case (false, false):
print(number)
}
}
The loop performs constant work per number, so its time complexity is O(n) and its auxiliary-space complexity is O(1).