import Cocoa
// using if else with a for in loop
for i in 1...100 {
if (i % 3 == 0) && (i % 5 == 0) {
print("FIZZ BUZZ")
} else if i % 3 == 0 {
print("FIZZ")
} else if i % 5 == 0 {
print("BUZZ")
} else {
print(i)
}
}
// using a switch with a while loop this time for variation
var number = 1
while number <= 100 {
let divisibleBy3 = number % 3
let divisibleBy5 = number % 5
let divisible = (divisibleBy3, divisibleBy5)
switch divisible {
case (0, 0):
print("FIZZ BUZZ")
case (0, _):
print("FIZZ")
case (_, 0):
print("BUZZ")
default:
print(number)
}
number += 1
}
My solution (using switch). It seems to work, but I’d be interested to know whether it’s an efficient way of solving the problem, or any other comments. Thanks.
var loopTuple = (f: false, b: false)
for i in 1...100 {
loopTuple = (f: i % 3 == 0, b: i % 5 == 0)
switch loopTuple {
case (f: true, b: true):
print("FIZZ BUZZ")
case (f: true, b: false):
print("FIZZ")
case (f: false, b: true):
print("BUZZ")
default:
print(i)
}
I think it is great that you put your solutions here, I learn a lot just by seeing different approaches to solving the same problem. My solution follows a similar approach, but I added some code to make the printed string prettier with commas and spaces. The if/else version is a bit messy, maybe someone else has a tidier solution?
Alain
var fizzBuzzString = ""
let numberOfLoops = 15
// Version of FIZZ BUZZ with if/else
// and prints numbers in between...
for i in 1 ... numberOfLoops {
if !(i % 3 == 0 || i % 5 == 0) {
fizzBuzzString += String(i)
} else {
if i % 3 == 0 {
fizzBuzzString += "FIZZ"
if i % 5 == 0 {
fizzBuzzString += " "
}
}
if i % 5 == 0 {
fizzBuzzString += "BUZZ"
}
}
if i != numberOfLoops {
fizzBuzzString += ", "
}
}
print("\(fizzBuzzString).\n")
// Version of FIZZ BUZZ with switch and tuple,
// and does not print numbers in between...
fizzBuzzString = ""
var printComma = false
var i = 1
while i <= numberOfLoops {
let caseTuple = (i % 3,i % 5)
switch caseTuple {
case (0,0):
fizzBuzzString += "FIZZ BUZZ"
printComma = true
case (_,0):
fizzBuzzString += "BUZZ"
printComma = true
case (0,_):
fizzBuzzString += "FIZZ"
printComma = true
default:
break
}
if i == numberOfLoops {
printComma = false
fizzBuzzString += "."
}
if printComma == true {
fizzBuzzString += ", "
}
printComma = false
i += 1
}
print("\(fizzBuzzString)\n")
Your code looks good, after defining your tuple, you could just check each of your cases as:
case(true, true)
Alternative when you do a mod on a number, and there’s no remainder, the result is 0. So your code also could have looked like:
var loopTuple = (f: 0, b: 0)
for i in 1...100 {
loopTuple = (i % 3, i % 5)
switch loopTuple {
case (0, 0):
print("FIZZ BUZZ")
case (0, _):
print("FIZZ")
case (_, 0):
print("BUZZ")
default:
print(i)
}
}
Declaring a tuple in a loop is just like declaring anything else in a loop. Do you need access to the information later or not?
One of the disadvantages of declaring it in your loop is you lose readability of your code. When you declare it outside of the loop: var loopTuple = (f: 0, b: 0) you can infer the first value is for fizz and the second for buzz. Declaring it the way you did in your loop, you might have to do some reverse engineering to try and figure what each of the values meant.
Given that the preceding pages were all about the "Control Transfer Statement, Redux" I thought I’d put one together which I’m in two minds about. I think it’s readability is high, but not convinced the structure is optimal with all the continue statements. I think my assembly programming roots are showing!
import Cocoa
for i in 1...100 {
if i % 3 == 0 && i % 5 == 0 {
print("FIZZ BUZZ")
continue
}
if i % 3 == 0 {
print("FIZZ")
continue
}
if i % 5 == 0 {
print("BUZZ")
continue
}
print (i)
}
I was trying to make one statement to print FIZZ and one to print BUZZ and have them work together without using a separate FIZZ BUZZ statement, and this is what I came up with. I had to do some digging to figure out how to get print() to not include a newline. I hope that is kosher as it hasn’t been covered in the book up to this point. Also, how do you paste the code into a post in a block like that? I couldn’t figure it out.
Thanks!
import Cocoa
for i in 1...100 {
if i % 3 == 0 || i % 5 == 0 {
if i % 3 == 0 {
print("FIZZ ", terminator:"")
}
if i % 5 == 0 {
print("BUZZ")
}else {
print("")
}
}else {
print("\(i)")
}
}
I had a lot of trouble figuring out a way to get the Switch solution to work. Here is what I finally came up with:
var i = 1
while i < 101 {
var byThree = i % 3
var byFive = i % 5
var check = (three: byThree, five: byFive)
switch check {
case let both where (check.three == 0) && (check.five == 0):
print ("\(i): FIZZ BUZZ: Both = \(check)")
case let threes where (check.three == 0) && (check.five != 0):
print ("\(i): FIZZ: Threes = \(check)")
case let fives where (check.three != 0) && (check.five == 0):
print ("\(i): BUZZ: Fives = \(check)")
default:
print ("\(i):")
}
i = i + 1
}
The big problem I was having was trying to figure out how to perform any logical check within each “case” statement because the result of the “case” is a Bool, apparently. Adding a local variable to the “case” statement fixed it.
I was curious to see what would happen when you told the compiler to print a tuple, and it prints right out.
var i = 1
while i < 101 {
var byThree = i % 3
var byFive = i % 5
var check = (three: byThree, five: byFive)
switch check {
case let (first, second) where (first == 0) && (second == 0):
print ("\(i): FIZZ BUZZ: Both = \(first, second)")
case let (first, second) where (first == 0) && (second != 0):
print ("\(i): FIZZ: Threes = \(first, second)")
case let (first, second) where (first != 0) && (second == 0):
print ("\(i): BUZZ: Fives = \(first, second)")
default:
print ("\(i):")
}
i = i + 1
}
Or:
var i = 1
while i < 101 {
var byThree = i % 3
var byFive = i % 5
var check = (three: byThree, five: byFive)
switch check {
case let tuple where (tuple.three == 0) && (tuple.five == 0):
print ("\(i): FIZZ BUZZ: Both = \(tuple)")
case let tuple where (tuple.three == 0) && (tuple.five != 0):
print ("\(i): FIZZ: Threes = \(tuple)")
case let tuple where (tuple.three != 0) && (tuple.five == 0):
print ("\(i): BUZZ: Fives = \(tuple)")
default:
print ("\(i):")
}
i = i + 1
}