Programming Challenge: Are You a Swift Ninja? Part 1

Do you consider yourself a Swift Ninja? Take our programming challenge! Beginners are also welcome to follow through and learn the craft. By Marin Todorov.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Challenge #4

Write a function countFrom(from:Int, #to:Int) that will produce as output (eg. via print() or println()) the numbers from from to to. You can’t use any loop operators, variables, nor any built-in Array functions. Assume the value of from is less than to (eg. the input is valid).

Here’s a sample call and its output:

countFrom(1, to: 5) //--> 12345

3shurikens

[spoiler title=”Hints”]
Use recursion and increase the value of from on every call until equals the to parameter.

2shurikens

[/spoiler]

[spoiler title=”Tutorial”]
The solution for this will involve recursion. For each number starting with from, you’ll recursively call countFrom while increasing the value of from by 1 each time. When from equals to, you’ll stop the recursion. This will effectively turn the function into a simple loop.

Have a look at the complete solution:

func countFrom(from: Int, #to: Int) {
    print(from) // output to the assistant editor
    if from < to {
        countFrom(from + 1, to: to)
    }
}

countFrom(1, to: 5)

When you call the function, it prints the from parameter "1" and goes on to compare it to to. 1 is less than 5, so the function goes on and calls itself recursively countFrom(1 + 1, 5).

The second call prints out "2" and compares 2 with 5, then again calls itself: countFrom(2 + 1, 5).

The function will continue to recursively call itself and increase from until from equals 5, at which point the recursion will stop.

Recursion is a powerful concept, and some of the next problems will require recursion as well so make sure you understand the solution above!

Give yourself 1shuriken for learning how to use recursion in Swift.

[/spoiler]

Where To Go From Here?

Get your revenge in part 2!

Get your revenge in part 2!

Get your revenge in part 2!

Ninjas need to take breaks, too. And if you made it this far you’re doing a fantastic job — you deserve some rest!

I’d like to take this time to reflect a bit on the problems and the solutions so far. Swift is very powerful, and is a much more expressive and safe language than Objective-C is.

With the first 4 problems in this challenge, I wanted to push you into exploring various areas of Swift. I hope working on the first few problems so far has been fun and beneficial experience.

Let’s do a small recap:

  • So far, none of the solutions required you to declare any variables — you probably don’t need them as much as you think!
  • You didn’t use any loops either. Reflect on that!
  • Parameters with default values and variadic parameters make Swift functions incredibly powerful.
  • It’s very important to know the power of the basic built-in functions like map, reduce, sort, countElements, etc.

And if you’re truly looking to take your mind off programming for a bit you watch the classic opening sequence from the game Ninja Gaiden:

Stay tuned for part 2 of the Swift Ninja programming challenge – where you can get your revenge! :]

Contributors

Over 300 content creators. Join our team.