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.

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.

Contents

Hide contents

The Final Challenge

Ninja_Swift3

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:
    1. Any Ace preceded by 5 of Diamonds is worth 100 points.
    2. 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:
      1. The hand 3♥, 7♣ has total value of 14.
      2. 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.
  1. Any Ace preceded by 5 of Diamonds is worth 100 points.
  2. 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:
    1. The hand 3♥, 7♣ has total value of 14.
    2. The hand 3♣, 7♥ has total value of 0.
  1. The hand 3♥, 7♣ has total value of 14.
  2. 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

3shurikens

Where to Go From Here

First, check your challenge result!

Calculate how many shurikens you earned -- for the final challenge, give yourself 3 shurikens 3shurikens 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:

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

Contributors

Over 300 content creators. Join our team.