Programming exercises

As you continue down the path of learning Swift, you may one day apply for a developer position where you’ll get to use Swift at work. The classic company interview questions are the Fibonacci and FizzBuzz algorithms. Pattern matching can come in handy for both of these challenges.

Note: These algorithms are call-intensive. If you’re following along in a playground, you should start a new playground and use it for the rest of this chapter. That way the playground won’t stutter with all the work you’re asking it to do.

Fibonacci

In the Fibonacci sequence, every element is the sum of the two preceding elements. The sequence starts with 0, 1, 1, 2, 3, 5, 8 ...

Your interviewer instructs you to find the 15th number of the Fibonacci sequence. Here’s how you could solve the challenge:

func fibonacci(position: Int) -> Int { switch position {

// 1

case let n where n <= 1: return 0

// 2

case 2:

return 1

// 3

case let n:

return fibonacci(position: n - 1) + fibonacci(position: n - 2)

}

}

let fib15 = fibonacci(position: 15) // 377

Here’s what’s happening in this code:

  1. If the current sequence position is less than two, the function will return 0.
  2. If the current sequence position is equal to two, the function will return 1.
  3. Otherwise, the function will use recursion to call itself and sum up all the numbers. This code is also an example of a way to avoid the default case in a switch statement. The let n case matches all values, so the default case is not needed.

FizzBuzz

In the FizzBuzz algorithm, your objective is to print the numbers from 1 to 100, except:

  • On multiples of three print "Fizz" instead of the number
  • On multiples of five print "Buzz" instead of the number
  • On multiples of both three and five print "FizzBuzz" instead of the number

for i in 1...100 {

// 1

switch (i % 3, i % 5) {

// 2

case (0, 0):

print("FizzBuzz", terminator: " ") case (0, _):

print("Fizz", terminator: " ") case (_, 0):

print("Buzz", terminator: " ")

// 3

case (, ):

print(i, terminator: " ")

}

}

print("")

Note: To see the result of this code in a playground, show the Debug area by clicking the button at the bottom left of the playground window:

Here’s what’s going on:

  1. You construct a tuple in the switch expression.
  2. Each of the cases checks a result of the modulo operation. The underscore means you don’t care and it matches any value.
  3. In this code, you learn another equivalent way to avoid writing the default case of a switch statement. A tuple pattern with all underscores, (, ), matches any value.

The terminator parameter of the print call tells the compiler to end each line with a space character instead of a new line. All the numbers in the algorithm will get printed on one line in your debug area. The final print("") call adds an empty string with a new line so that the code you add in the future will start printing on a new line.

Now you know how to beat those job interview questions in a surprisingly elegant fashion using pattern matching. You can thank me later for your new Swift job. ;]

results matching ""

    No results matching ""