Fizz Buzz in Swift

Fizz Buzz in Swift

A short presentation from FruitConf 2014 describing some approaches to writing a Fizz Buzz in Swift.

F28c4835c9e2277b6e04b86574532a2d?s=128

Abizer Nasir

June 13, 2014
Tweet

Transcript

  1. 2.
  2. 4.

    FizzBuzz • For some set of input numbers: • If

    the number is a multiple of 3 print “Fizz” • If the number is a multiple of 5 print “Buzz” • If the number is a multiple of 3 and 5 print “FizzBuzz” • Otherwise, Print the number.
  3. 5.
  4. 6.
  5. 7.

    Simple Implementation func fizzBuzz (number: Int) -> String { var

    output = "" ! if number % 3 == 0 { output += "Fizz" } ! if number % 5 == 0 { output += "Buzz" } ! if output.isEmpty { output = "\(number)" } return output }
  6. 8.

    Pattern Matching func fizzBuzz (number: Int) -> String { switch

    (number % 5, number % 3) { case (0, 0): return "FizzBuzz" case (0, _): return "Buzz" case (_, 0): return "Fizz" default: return "\(number)" } }
  7. 9.

    FizzBuzzBar • Same as FizzBuzz, except • For multiples of

    7 add the string “Bar” • e.g. 105 -> “FizzBuzzBar”
  8. 10.

    Simple solution func fizzBuzzBar(number: Int) -> String { switch (number

    % 7, number % 5, number % 3) { case (0, 0, 0): return "FizzBuzzBar" case (0, 0, _): return "BuzzBar" case (_, 0, 0): return "FizzBuzz" case (0, _, 0): return "FizzBar" case (0, _, _): return "Bar" case (_, 0, _): return "Buzz" case (_, _, 0): return "Fizz" default: return "\(number)" }
  9. 11.

    Pass Options func fizzBuzzBar(number: Int, #options: Array<(Int, String)>) -> String

    { var output = "" for (divisor, description) in options { if number % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(number)" } ! return output }
  10. 12.

    for i in 1...105 { let options = [(3, "Fizz"),

    (5, "Buzz"), (7, "Bar")] println(fizzBuzzBar(i, options: options)) }
  11. 13.

    Extension extension Int { func fizzBuzzBar(options: Array<(Int, String)>) -> String

    { var output = "" for (divisor, description) in options { if self % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(self)" } return output } }
  12. 14.

    for i in 1...105 { let options = [(3, "Fizz"),

    (5, "Buzz"), (7, "Bar")] println(i.fizzBuzzBar(options)) }
  13. 16.

    extension Int: FizzBuzzable { func fizzBuzzBar(options: Array<(Int, String)>) -> String

    { var output = "" for (divisor, description) in options { if self % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(self)" } return output } }
  14. 18.

    FizzBuzz with Strings • For a list of strings: •

    If the string contains the word “Three” print “Fizz” • If the string contains the word “Five” print “Buzz” • If the string contains the words “Three” and “Five” print “FizzBuzz”
  15. 19.

    let stringInput = ["one", "three", "ThreeFive", "EleventyOne"] for string in

    stringInput { var result = "" ! if string.rangeOfString("three", options: .CaseInsensitiveSearch) { result += "Fizz" } ! if string.rangeOfString("five", options: .CaseInsensitiveSearch) { result += "Buzz" } ! if result.isEmpty { result = string } ! println(result) }
  16. 21.

    extension Int: FizzBuzzable { typealias ItemType = Int func fizzBuzz(options:

    Array<(Int, String)>) -> String { var output = "" for (divisor, description) in options { if self % divisor == 0 { output += description } } ! if output.isEmpty { output = "\(self)" } ! return output } }
  17. 22.

    extension String: FizzBuzzable { typealias ItemType = String func fizzBuzz(options:

    Array<(String, String)>) -> String { var output = "" for (substring, description) in options { if self.rangeOfString(substring, options: .CaseInsensitiveSearch) { output += description } ! if output.isEmpty { output = self } ! return output } }
  18. 23.

    for x in 1..20 { let options = [(3, "Fizz"),

    (5, "Buzz")]; println(x.fizzBuzz(options)) } ! for x in ["three", "six", "ninetyfive"] { let options = [("three", "Fizz"), ("five", "Buzz")] println(x.fizzBuzz(options)) }
  19. 26.

    for x in typedArray { let intOptions = [(3, "Fizz"),

    (5, "Buzz")] let strOptions = [("three", "Fizz"), ("five", "Buzz")] var output = "" ! if let num = x as? Int { output = num.fizzBuzz(intOptions) } else if let str = x as? String { output = str.fizzBuzz(strOptions) } ! println(output) }
  20. 30.

    fizzBuzz :: Integer -> String fizzBuzz n | n `mod`

    15 == 0 = "FizzBuzz" | n `mod` 5 == 0 = "Fizz" | n `mod` 3 == 0 = "Buzz" | otherwise = show n
  21. 31.

    fizzBuzz :: Integer -> String fizzBuzz n | n `mod`

    15 == 0 = "FizzBuzz" | n `mod` 5 == 0 = "Fizz" | n `mod` 3 == 0 = "Buzz" | otherwise = show n func fizzBuzz (number: Int) -> String { if number % 15 == 0 { return “FizzBuzz” } else if number % 5 == 0 { return "Buzz" } else if number % 3 == 0 { return "Fizz" } else { return “\(number)” } }
  22. 35.

    “We have every opportunity and every encouragement before us, to

    form the noblest purest constitution on the face of the earth. We have it in our power to begin the world over again” Thomas Paine, Common Sense, 1776