Programming Challenge: Are You a Swift Ninja? Part 2
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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Programming Challenge: Are You a Swift Ninja? Part 2
20 mins
The Final Challenge
Time for the last challenge. This time there will be no hints and no tutorial. It's all on you, dear Ninja.
Approach this challenge carefully and design a beautiful solution. Then post your solution in the comments on this post. Note it's ideal if you post your solution as a gist so it has nice syntax highlighting.
I will select one solution as the winner. The winner will be immortalized in this post as the correct solution for this challenge! Remember to leave your name along the code, so you can live in infamy, forever known as a true Swift Ninja :]
In addition, the winner will will receive a a free copy of our upcoming Swift by Tutorials Bundle, which includes three books about programming in Swift!
In choosing a winner, I will consider correctness, brevity and use of Swift's language features. Embrace all the techniques you've explored in this post. Should you and another developer post the same winning solution, I'll choose the one that was posted first.
You have 2 weeks from the time this post goes live. Better get to it!
Let's get to coding! Here's the enumerations and a struct to get started:
enum Suit {
case Clubs, Diamonds, Hearts, Spades
}
enum Rank {
case Jack, Queen, King, Ace
case Num(Int)
}
struct Card {
let suit: Suit
let rank: Rank
}
Write a function called countHand
that takes in an array of Card
instances and counts the total value of the cards given.
The requirements for your solution are as follows:
- The function returns the value of the cards in the hand as an Int.
- Does not use loops or nested functions.
- The card values are as follows:
- Any Ace preceded by 5 of Diamonds is worth 100 points.
- Any odd numeric card (3, 5, 7, 9) of any suit's worth the double of its rank value in points when immediately preceded in the hand by any card of the Hearts. Examples:
- The hand 3♥, 7♣ has total value of 14.
- The hand 3♣, 7♥ has total value of 0.
- Since brevity is one of the points on which your code will be assessed, consider using one statement functions, closures and case statements.
- Any Ace preceded by 5 of Diamonds is worth 100 points.
- Any odd numeric card (3, 5, 7, 9) of any suit's worth the double of its rank value in points when immediately preceded in the hand by any card of the Hearts. Examples:
- The hand 3♥, 7♣ has total value of 14.
- The hand 3♣, 7♥ has total value of 0.
- The hand 3♥, 7♣ has total value of 14.
- The hand 3♣, 7♥ has total value of 0.
Here's an example of usage and its result. Use this to check your solution:
countHand([
Card(suit:Suit.Hearts, rank:Rank.Num(10)),
Card(suit:Suit.Hearts, rank:Rank.Num(6)),
Card(suit:Suit.Diamonds, rank:Rank.Num(5)),
Card(suit:Suit.Clubs, rank:Rank.Ace),
Card(suit:Suit.Diamonds, rank:Rank.Jack)
]) //--> 110
Where to Go From Here
First, check your challenge result!
Calculate how many shurikens you earned -- for the final challenge, give yourself 3 shurikens if you solved the problem and none if you didn't.
How ninja are you?
- 23 or more shurikens: Congratulations! You're a Swift ninja! Fantastic!
- 16 to 22 shurikens: You're doing well, but you'll benefit from delving into the nitty gritty details of Swift. Luckily, this website is a great resource to learn more about Swift.
- 15 or less shurikens: Though you may not have been able to beat the challenges without help, you certainly learned tons of new Swift techniques and are on your way to being a certifiable ninja. Props!
You can download the complete Playground solution to the first 8 challenges here: NinjaChallenge-Completed-beta6.playground
Even if you mastered the challenges, you can always learn more! Check out some additional Swift resources:
- The raywenderlich.com Swift style guide
- The Swift language highlights from Objective-C Developer's Perspective
- Check out our three new Swift books, covering Swift, iOS 8, and more
- Last but not least, Apple's own page is always a great source of Swift knowledge.
Remember to post your solution to the final challenge and your name, and good luck. Thanks for reading this tutorial, and if you have comments or questions, please join in the forum discussion below!
Credit: All images in this post are from the public domain, and are available at: www.openclipart.org