Skip to content

Instantly share code, notes, and snippets.

@nmbr73
Last active December 7, 2022 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmbr73/c8052a8a8d04c77b580d28478ce95c72 to your computer and use it in GitHub Desktop.
Save nmbr73/c8052a8a8d04c77b580d28478ce95c72 to your computer and use it in GitHub Desktop.
Wanted to make use of what we learned about switch statements and arrays and such in #100DaysOfSwiftUI. So here to come my 6 solutions for 'Checkpoint 3' ...
// Solution 1: Straightforward, I guess this is the standard way to tackle the task?!?
// Note: The order in which the conditions are checked is crucial - you see why?!?
for i in 1...100 {
if i.isMultiple(of: 3) && i.isMultiple(of: 5) {
print("FizzBuzz")
} else if i.isMultiple(of: 3) {
print("Fizz")
} else if i.isMultiple(of: 5) {
print("Buzz")
} else
{ print("\(i)")
}
}
// Solution 2: Instead of isMultiple you can also use the modulo operator,
// but I think the isMultiple is better to show the intend.
for i in 1...100 {
if i % 3 == 0 && i % 5 == 0 {
print("FizzBuzz")
} else if i % 3 == 0 {
print("Fizz")
} else if i % 5 == 0 {
print("Buzz")
} else
{ print("\(i)")
}
}
// Solution 3: We can optimize our first solution (same for the second one, if you prefer modulo) given that,
// if a number is a multiple of 3 and a multiple of 5, then it is a multiple of 15 (15, 30, 45, etc. are the
// common multiples of 3 and 5) ...
for i in 1...100 {
if i.isMultiple(of: 15) {
print("FizzBuzz")
} else if i.isMultiple(of: 3) {
print("Fizz")
} else if i.isMultiple(of: 5) {
print("Buzz")
} else
{ print("\(i)")
}
}
// Solution 4: Next variant - you remember that ternary operator on day 5?
// Did I mention that I like it? Less code and less comparisons
// ... but honstently, what do you think if you have to read such code?
for i in 1...100 {
print( i.isMultiple(of: 3) ? "Fizz" + (i.isMultiple(of: 5) ?
"Buzz" : "") : (i.isMultiple(of: 5) ? "Buzz" : String(i)))
}
// Solution 5: Okay, let's do it without that W?T:F magic.
// It's quite the same, but different - more readable maybe?!?
for i in 1...100 {
var string: String
if i.isMultiple(of: 3) {
string = ("Fizz")
if i.isMultiple(of: 5) {
string.append("Buzz")
}
} else {
if i.isMultiple(of: 5) {
string = "Buzz"
} else {
string = String(i)
}
}
print(string)
}
// Solution 6: Let's try something different. We had the switch-case on day 5 - so let's use it.
// Hint: There are 10 types of people in this world - those who understand binary and those who don't ...
for i in 1...100 {
var choice = 0
if i.isMultiple(of: 3) {
choice = 1
}
if i.isMultiple(of: 5) {
choice += 2
}
switch choice {
case 1:
print("Fizz")
case 2:
print("Buzz")
case 3:
print("FizzBuzz")
default:
print(String(i))
}
}
// Solution 7: There is actually a special notation in Swift that lets us use a switch directly ...
for i in 1...100 {
switch ( i.isMultiple(of: 3), i.isMultiple(of: 5) ) {
case (true, true):
print("FizzBuzz")
case (true, false):
print("Fizz")
case (false, true):
print("Buzz")
case (false, false):
print(String(i))
}
}
// Solution 8: But with the approach of solution 6 we can use these arrays that we learned about on day 3 ...
var fizzBuzzChoices = ["","Fizz","Buzz","FizzBuzz"]
for i in 1...100 {
fizzBuzzChoices[0] = String(i)
var choice = 0
if i.isMultiple(of: 3) {
choice = 1
}
if i.isMultiple(of: 5) {
choice += 2
}
print(fizzBuzzChoices[choice])
}
// Solution 9: Let's do the same spiced up with some W?T:F ... that's pretty concise, isn't it?
// But still probably hard to read and to "see" what's the intention here ...
let buzzFizzChoices = ["","Fizz","Buzz","FizzBuzz"]
for i in 1...100 {
let choice = (i.isMultiple(of: 3) ? 1 : 0) + (i.isMultiple(of: 5) ? 2 : 0)
print( choice == 0 ? String(i) : buzzFizzChoices[choice] )
}
// Solution 10: Talking of arrays - this solution might look a bit like "brute force" ...
// but think about it: It uses a loop as demanded, it is very very easy to understand,
// and probably it has a better performance than all the previous suggestions ...
let fizzBuzz = ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz",
"13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "23", "Fizz", "Buzz",
"26", "Fizz", "28", "29", "FizzBuzz", "31", "32", "Fizz", "34", "Buzz", "Fizz", "37", "38",
"Fizz", "Buzz", "41", "Fizz", "43", "44", "FizzBuzz", "46", "47", "Fizz", "49", "Buzz", "Fizz",
"52", "53", "Fizz", "Buzz", "56", "Fizz", "58", "59", "FizzBuzz", "61", "62", "Fizz", "64",
"Buzz", "Fizz", "67", "68", "Fizz", "Buzz", "71", "Fizz", "73", "74", "FizzBuzz", "76", "77",
"Fizz", "79", "Buzz", "Fizz", "82", "83", "Fizz", "Buzz", "86", "Fizz", "88", "89", "FizzBuzz",
"91", "92", "Fizz", "94", "Buzz", "Fizz", "97", "98", "Fizz", "Buzz",
]
for i in 0..<100 {
print(fizzBuzz[i])
}
// That's it. Any other ideas? Which one do you prefer? What does your #100DaysOfSwiftUI ‘Checkpoint 3’
// solution look like?!? Would love to learn how others solved it!
@EvolvingParty
Copy link

Awesome work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment