Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fizz Buzz in Swift

Fizz Buzz in Swift

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

Abizer Nasir

June 13, 2014
Tweet

More Decks by Abizer Nasir

Other Decks in Programming

Transcript

  1. 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.
  2. 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 }
  3. 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)" } }
  4. FizzBuzzBar • Same as FizzBuzz, except • For multiples of

    7 add the string “Bar” • e.g. 105 -> “FizzBuzzBar”
  5. 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)" }
  6. 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 }
  7. for i in 1...105 { let options = [(3, "Fizz"),

    (5, "Buzz"), (7, "Bar")] println(fizzBuzzBar(i, options: options)) }
  8. 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 } }
  9. for i in 1...105 { let options = [(3, "Fizz"),

    (5, "Buzz"), (7, "Bar")] println(i.fizzBuzzBar(options)) }
  10. 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 } }
  11. 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”
  12. 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) }
  13. 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 } }
  14. 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 } }
  15. 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)) }
  16. 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) }
  17. fizzBuzz :: Integer -> String fizzBuzz n | n `mod`

    15 == 0 = "FizzBuzz" | n `mod` 5 == 0 = "Fizz" | n `mod` 3 == 0 = "Buzz" | otherwise = show n
  18. 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)” } }
  19. “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