Swift Ninja Programming Challenge: Winners Announced!
Check out the winners of our recent Swift NInja programming challenge! 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
Swift Ninja Programming Challenge: Winners Announced!
10 mins
And the winner is…
At long last, it’s time to announce the ultimate Swift Ninja: Dojomaroc!
It was a really close call but in the end Dojomaroc’s solution was just a tiny bit better overall.
The fact that he ignored the first card in the hand and thanks to that he didn’t have to use any optionals made his code clearer to read and fast both on a debug and release builds.
Congratulations Dojomaroc! Ray will be in touch soon to deliver your prize – a free copy of our three brand new Swift books coming out later this year.
Here’s Dojomaroc’s solution:
func countHand(cards: [Card]) -> Int {
// If the number of cards is inferior to 2, the value of the hand is automatically 0
// Note that the dropFirst() function doesn't mutate the cards array,
// so cards[0] can be passed as the initial value of "reduce()".
// Only cards that have a preceding card need to be evaluated by "reduce()",
// that's why reduce() is applied to dropFirst(cards).
// "previous" is a tuple that keeps track of (the total value of the hand, and the last card evaluated by "reduce()")
return cards.count < 2 ? 0 : dropFirst(cards).reduce((0, cards[0])) { (previous: (totalValue: Int, card: Card), currentCard: Card) in
// For the previous card, we are interested in its rank and suit.
// As for the current card we are only concerned with its rank.
switch (previous.card.rank, previous.card.suit, currentCard.rank) {
// Statistically speaking: to have an odd numeric card (3, 5, 7, 9)
// preceded by any rank of Hearts is more probable than
// having an Ace preceded by 5 of Diamonds .
// That explains the value associated (100), and more importantly
// the order in which the first two cases are evaluated.
case ( _, .Hearts, .Num(let rank)) where rank % 2 == 1:
return (previous.totalValue + 2 * rank, currentCard)
case (.Num(5), .Diamonds, .Ace):
return (previous.totalValue + 100, currentCard)
default:
return (previous.totalValue, currentCard)
}}.totalValue // Finally, return .totalValue of the tuple "previous"
}
Where to go from here?
I hope you enjoyed the Swift Ninja programming challenge and that you learned more about Swift along the way. Don't stop exploring Swift - it is shaping to become a really fantastic language!
If you want to get really deep insight into the Swift development process and get notified on cool new features you can:
- Subscribe to Chris Lattner's forum posts
- Keep an eye on #swiftlang hashtag
- Check out our free Swift tutorials
- Also be sure to snag our 3 new Swift books: Swift by Tutorials, iOS 8 by Tutorials, and Core Data by Tutorials!
If you have ideas for further Ninja challenges or just want to share your thoughts on the challenge and solutions provided, please leave a comment below!