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

Update 8/5/14: Series updated for Xcode6-beta 5.

Although Swift has only been out for a little while and is still in beta, many of you have been digging in already.

How far have you come so far? Have you:

If you’ve scored 3 or more on this quiz, then you might be a Swift Ninja.

Well, this 2-part series is going to help you find out for sure!

I’ll give you a series of programming challenges in Swift, that you can use to test your Swift knowledge and see if you’re a true Swift Ninja.

And if by any chance you’re not feeling so ninja, you’ll have the chance to learn the craft! No matter whether you’re already advanced or still intermediate in Swift, you’ll likely still learn a thing or two.

In addition, part 2 of this tutorial has a special final challenge, where you will get a chance to win a free copy of our upcoming Swift by Tutorials Bundle, which includes three books about programming in Swift.

Get your shurikens and katana ready – the challenge begins!

Note: This post is for experienced programmers who are well-versed in the Swift language. If you don’t feel quite at ease with it yet, check out the rest of our Swift tutorials.

Note: This post is for experienced programmers who are well-versed in the Swift language. If you don’t feel quite at ease with it yet, check out the rest of our Swift tutorials.

The Challenge

This series is a bit different in style compared to the ones we usually post on this web site. It will present a series of problems in order of increasing complexity. Some of these problems reuse techniques from previous sections, so working through them in order is essential for your success.

Each of the problems highlight at least one feature, syntax oddity, or clever hack made possible by Swift.

Don’t worry, you’ll not be thrown to the wolves – there’s help when you need it. Each post has two levels of hints, and of course there’s always the Swift books from Apple, or your good friend Stack Overflow.

How to Approach Each Problem

Each problem is defined by stating what you need to accomplish in code, and what Swift features you can and can’t use. I recommend you use a Playground to work through the each challenge.

If you have difficulties, open up the Hints section. Though Hints won’t give you instant gratification, they offer direction.

In case you can’t muster the solution — open up the problem’s Tutorial section. There you’ll find the techniques to use and provide the code to solve the given problem. In any case, by the end of this series you’ll have the solutions to all the problems.

Oh – and remember to track your score!

  • If you solve the problem without peeking at the hints or tutorial sections you get 3 shurikens: 3shurikens
  • If you solve the problem by peeking at the hints section, you get 2 shurikens: 2shurikens
  • If you solve the problem by peeking at the tutorial section, you get 1 shuriken: 1shuriken

Even if you solved the problem yourself, take a few moments to see the solution provided in the Tutorial –it’s always great to compare code!

Some challenges offer an extra shuriken extrashuriken if you do the solution in a certain (more difficult) way.

Keep a piece of paper or your favorite tracking app handy and keep count of how many shurikens you got for each challenge.

Don’t cheat yourself by padding your score. That’s not the way of the noble ninja. At the end of the post you’ll have broadened your mind and moved boldly into the future, even if you don’t collect every single shuriken.

The Swift Ninja challenge

Challenge #1

In the Swift book from Apple, there are several examples of a function that swaps the values of two variables. The code always uses the “classic” solution of using an extra variable for storage. But you can do better than that.

Your first challenge is to write a function that takes two variables (of any type) as parameters and swaps their values.

Requirements:

  • For the function body use a single line of code

Give yourself 3shurikens if you don’t have to crack open the Hints or Tutorial spoilers.

[spoiler title=”Hints”]
Swift Tuples are very powerful – you group variables of any type into a tuple. Additionally, you can assign values to a number of variables in one shot if they were grouped in a tuple. One tuple, two tuples! :]

Remember since you peeked at the hint, now you can only give yourself 2shurikens for this challenge.

[/spoiler]

[spoiler title=”Tutorial”]
As a Swift ninja knows, one of the new features in Swift is called a Tuple that you can group variables in. The syntax is easy – just surround the list of variables (or constants, expressions, etc.) with brackets

var a = "Marin"
var b = "Todorov"
var myTuple = (a, b)

In the code example above a and b are value types (strings), so their values are copied when you create the tuple. An interesting fact is that you can also assign values to tuples, like so:

var name: String
var family: String

(name, family) = ("Marin", "Todorov")

The example above sets the values of both name and family at the same time from another tuple, providing the values for the assignment.

Combining what you’ve learned from the two examples above, you can now write a function that takes two variables of any type (as long as they are of the same data type) and swapping their values. Here’s the solution to the original problem and the code to test it in a playground:

func swap<T>(inout a:T, inout with b:T) {
    (a, b) = (b, a)
}

//demo code
var a = "Marin", b = "Todorov"
swap(&a, &b)

[a, b]

You define a function swap(a:, with:) that takes two read-write parameters of the same type T.

In the single line of code inside the function, you just make a tuple out of the two function parameters and assign their values to another tuple. Then you change the order of the two parameters in both tuples in order to exchange their values.

The code above also declares two variables a and b to showcase how to use swap(a:, with:). The final line of code will output the values of a and b to the playground output area on the right hand side of the window like so:

["Todorov", "Marin"]

Give yourself another 1shuriken if you ran the code in a Playground and learned how to swap values via tuples!
[/spoiler]

Contributors

Over 300 content creators. Join our team.