Skip to content

Instantly share code, notes, and snippets.

@futureperfect
Last active May 25, 2019 21:50
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 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

@CQH
Copy link

CQH commented Oct 1, 2014

return "\(number)" instead of return String(number)

@CQH
Copy link

CQH commented Oct 1, 2014

Another way to skin this cat:

func FizzBuzz2() {
    func applyLabel(number: Int) -> String {
        var returnString: String = ""
        if number % 3 == 0 {
            returnString += "Fizz"
        }
        if number % 5 == 0 {
            returnString += "Buzz"
        }
        if returnString == "" {
            returnString = "\(number)"
        }
        return returnString
    }
    for value in 1...100 {
        println(applyLabel(value))
    }
}

FizzBuzz2()

@futureperfect
Copy link
Author

@CQH still trying to suss out idiom as far as the appropriateness of string concatenation in the setting of the FizzBuzz problem. I'm sure there's also constantization nonsense that I could be doing, but that might be my brain being broken from Java.

There's also the question of the cyclomatic complexity of three if statements in a row versus the if/else construction in the original version.

The BIG Question I have is whether the nested function here is asinine because nominally I did it so I could test it was returning the right strings for the right values, but I don't think it's actually testable in it's current state.

@CQH
Copy link

CQH commented Oct 2, 2014

Have you seen this yet:

result = x == y ? true : false
Which is the same as:

if x == y {
  result = true
} else {
  result = false
}

So FizzBuzz can also be written as such using Swift:

func FizzBuzz2() {
    func applyLabel(number: Int) -> String {
        var returnString: String = ""
        returnString += number % 3 == 0 ? "Fizz" : ""
        returnString += number % 5 == 0 ? "Buzz" : ""
        returnString = returnString == "" ? "\(number)" : returnString
        return returnString
    }
    for value in 1...100 {
        println(applyLabel(value))
    }
}

FizzBuzz2()

@futureperfect
Copy link
Author

^ this looks way too close to C to keep a clear conscience ^

@futureperfect
Copy link
Author

Now here's what I was talking about:

PATTERN MATCHING

func fizzBuzz(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)"
    }
}

This feels more in keeping with the language as I've observed it so far

@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