Skip to content

Instantly share code, notes, and snippets.

@zhaar
Last active August 29, 2015 14:04
Show Gist options
  • Save zhaar/a718253eb4246ce729a8 to your computer and use it in GitHub Desktop.
Save zhaar/a718253eb4246ce729a8 to your computer and use it in GitHub Desktop.
Final Swift challenge Zephyz
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()
}
}
}
@wottpal
Copy link

wottpal commented Aug 7, 2014

I think you should rename your function to coundHand (without '1'), otherwise line 22 will result in a compiler error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment