Last active
August 29, 2015 14:04
-
-
Save zhaar/a718253eb4246ce729a8 to your computer and use it in GitHub Desktop.
Final Swift challenge Zephyz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
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 | |
} | |
//clearest solution I could come up with 11 lines | |
func countHand1(cards: [Card], previousCard: Card = Card(suit:Suit.Spades, rank:Rank.Num(0))) -> Int { | |
if cards.isEmpty { return 0 } | |
else { | |
let fn : () -> Int = { return countHand(Array(dropFirst(cards)), previousCard: cards[0])} | |
switch (previousCard.suit, previousCard.rank, cards[0].suit, cards[0].rank) { | |
case (Suit.Diamonds, Rank.Num(5), _ , Rank.Ace) : return 100 + fn() | |
case (Suit.Hearts, _ , _ , Rank.Num(let a)) where a % 2 == 1 : return a*2 + fn() | |
default: return fn() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you should rename your function to coundHand (without '1'), otherwise line 22 will result in a compiler error.