Skip to content

Instantly share code, notes, and snippets.

@futureperfect
Last active May 25, 2019 21:50
Show Gist options
  • Save futureperfect/03e3ef86f16b7a855633 to your computer and use it in GitHub Desktop.
Save futureperfect/03e3ef86f16b7a855633 to your computer and use it in GitHub Desktop.
Swift Exercises
/*
Write a function that prints the numbers from 1 to 100. EXCEPT:
* If the number if a multiple of 3, print "Fizz" instead of the number
* If the number is a multiple of 5, print "Buzz" instead of the number
* If the number is a multiple of 3 AND 5, print "FizzBuzz" instead of the number
*/
func FizzBuzz() {
func applyLabel(number: Int) -> String {
switch (number % 3, number % 5) {
case (0, 0):
// number divides by both 3 and 5
return "FizzBuzz!"
case (0, _):
// number divides by 3
return "Fizz!"
case (_, 0):
// number divides by 5
return "Buzz!"
case (_, _):
// number does not divide by either 3 or 5
return "\(number)"
}
}
for value in 1...100 {
println(applyLabel(value))
}
}
FizzBuzz()

Swift Exercises

This is a collection of familiarization exercises in Swift. They'll suffer from ignorance of writing idiomatic code. My goal is to iterate on these as fluency develops and leave them up for the brave.

In short:

  • Caveat Lector
  • Don't Be A Jerk

Yours Truly,

Erik Hollembeak

@asifmimi1
Copy link

// Is that ok?
func number(){
for i in 1...100{
if i % 3 == 0 && i % 5 == 0{
print("Divided by both 3 & 5")
continue
}
else if i % 3 == 0{
print("Divided by 3")
continue
}
else if i % 5 == 0{
print("Divided by 5")
continue
}

    print(i)
}}

number()

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