| | 27 | def is_n_kinds(cards): |
| | 28 | highest_n = 1 #greatest num in a row |
| | 29 | current_n = 1 |
| | 30 | highest_card = None #highest card for greatest num in a row |
| | 31 | last = cards[0][0] |
| | 32 | for (a,b) in cards[1:]: |
| | 33 | if a == last: |
| | 34 | current_n += 1 |
| | 35 | if current_n > highest_n: |
| | 36 | highest_n = current_n |
| | 37 | highest_card = a |
| | 38 | if highest_n == 1: return ["high"]+cards |
| | 39 | #check for full house |
| | 40 | elif highest_n == 2: |
| | 41 | #this is the hand withotu the pairs |
| | 42 | whats_left = [x for x in cards if x != highest_card] |
| | 43 | #now check if it's two pair |
| | 44 | ret = is_n_kinds(whats_left) |
| | 45 | |
| | 46 | #if the subset is a pair then this is a two pair |
| | 47 | if ret[0] == "pair": return ["two_pair",highest_card]+ret[1:] |
| | 48 | else: return ["pair",highest_card]+whats_left |
| | 49 | elif highest_n == 3: |
| | 50 | whats_left = [x for x in cards if x != highest_card] |
| | 51 | #now check if it's a full house |
| | 52 | ret = is_n_kinds(whats_left) |
| | 53 | if ret[0] == "pair": return ["full_house",highest_card]+ret[1:] |
| | 54 | #well, it's just a 3 of a kind then |
| | 55 | else: return ["three_of_a_kind",highest_card]+whats_left |
| | 56 | elif highest_n == 4: |
| | 57 | whats_left = [x for x in cards if x != highest_card] |
| | 58 | return ["four_of_a_kind",highest_card]+whats_left |
| | 59 | #we shouldn't get to this point |
| | 60 | assert(False) |