Task

Create a program to parse a single five card poker hand and rank it according to this list of poker hands.



A poker hand is specified as a space separated list of five playing cards.

Each input card has two characters indicating face and suit.



Example

2d       (two of diamonds).


Faces are:    a, 2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, k

Suits are:    h (hearts),   d (diamonds),   c (clubs),   and   s (spades),   or

alternatively,   the unicode card-suit characters:     ♥ ♦ ♣ ♠ 



Duplicate cards are illegal.

The program should analyze a single hand and produce one of the following outputs:

 straight-flush
 four-of-a-kind
 full-house
 flush
 straight
 three-of-a-kind
 two-pair
 one-pair
 high-card
 invalid



Examples

   2♥ 2♦ 2♣ k♣ q♦:   three-of-a-kind
   2♥ 5♥ 7♦ 8♣ 9♠:   high-card
   a♥ 2♦ 3♣ 4♣ 5♦:   straight
   2♥ 3♥ 2♦ 3♣ 3♦:   full-house
   2♥ 7♥ 2♦ 3♣ 3♦:   two-pair
   2♥ 7♥ 7♦ 7♣ 7♠:   four-of-a-kind 
   10♥ j♥ q♥ k♥ a♥:  straight-flush
   4♥ 4♠ k♠ 5♦ 10♠:  one-pair
   q♣ 10♣ 7♣ 6♣ q♣:  invalid

The programs output for the above examples should be displayed here on this page.



Extra credit

 use the playing card characters introduced with Unicode 6.0 (U+1F0A1 - U+1F0DE).
 allow two jokers
 use the symbol   joker
 duplicates would be allowed (for jokers only)
 five-of-a-kind would then be the highest hand


More extra credit examples

   joker  2♦  2♠  k♠  q♦:     three-of-a-kind
   joker  5♥  7♦  8♠  9♦:     straight
   joker  2♦  3♠  4♠  5♠:     straight
   joker  3♥  2♦  3♠  3♦:     four-of-a-kind
   joker  7♥  2♦  3♠  3♦:     three-of-a-kind
   joker  7♥  7♦  7♠  7♣:     five-of-a-kind
   joker  j♥  q♥  k♥  A♥:     straight-flush
   joker  4♣  k♣  5♦ 10♠:     one-pair
   joker  k♣  7♣  6♣  4♣:     flush
   joker  2♦  joker  4♠  5♠:  straight
   joker  Q♦  joker  A♠ 10♠:  straight
   joker  Q♦  joker  A♦ 10♦:  straight-flush
   joker  2♦  2♠  joker  q♦:  four-of-a-kind



Related tasks

 Playing cards
 Card shuffles
 Deal cards_for_FreeCell
 War Card_Game
 Go Fish


F analyzeHandHelper(faceCount, suitCount)
   V
      p1 = 0B
      p2 = 0B
      t  = 0B
      f  = 0B
      fl = 0B
      st = 0B
 
   L(fc) faceCount
      S fc
         2 {I p1 {p2 = 1B} E p1 = 1B}
         3 {t = 1B}
         4 {f = 1B}
 
   L(sc) suitCount
      I sc == 5
         fl = 1B
 
   I !p1 & !p2 & !t & !f
      V s = 0
      L(fc) faceCount
         I fc != 0
            s++
         E
            s = 0
         I s == 5
            L.break
 
      st = (s == 5) | (s == 4 & faceCount[0] != 0 & faceCount[1] == 0)
 
   I st & fl   {R ‘straight-flush’}
   E I f       {R ‘four-of-a-kind’}
   E I p1 & t  {R ‘full-house’}
   E I fl      {R ‘flush’}
   E I st      {R ‘straight’}
   E I t       {R ‘three-of-a-kind’}
   E I p1 & p2 {R ‘two-pair’}
   E I p1      {R ‘one-pair’}
   E           {R ‘high-card’}
 
F analyzeHand(inHand)
   V handLen = 5
   V face = ‘A23456789TJQK’
   V suit = ‘SHCD’
   V errorMessage = ‘invalid hand.’
 
   V hand = sorted(inHand.split(‘ ’))
   I hand.len != handLen
      R errorMessage
   I Set(hand).len != handLen
      R errorMessage‘ Duplicated cards.’
 
   V faceCount = [0] * face.len
   V suitCount = [0] * suit.len
   L(card) hand
      I card.len != 2
         R errorMessage
      V? n = face.find(card[0])
      V? l = suit.find(card[1])
      I n == N | l == N
         R errorMessage
      faceCount[n]++
      suitCount[l]++
 
   R analyzeHandHelper(faceCount, suitCount)
 
L(hand) [‘2H 2D 2S KS QD’,
         ‘2H 5H 7D 8S 9D’,
         ‘AH 2D 3S 4S 5S’,
         ‘2H 3H 2D 3S 3D’,
         ‘2H 7H 2D 3S 3D’,
         ‘2H 7H 7D 7S 7C’,
         ‘TH JH QH KH AH’,
         ‘4H 4C KC 5D TC’,
         ‘QC TC 7C 6C 4C’]
   print(hand‘: ’analyzeHand(hand))