Sep-03-2021, 04:54 AM
Your first function logic assumes that all straights are either a,b,c,d,e; b,c,d,e,f; or c,d,e,f,g. So those are the only possibilities you try. And if the list were sorted, that would be correct.
But because this list is not sorted, your straight is actually made of the cards: a,b,c,d,g. Because g is smaller than the others, it isn't detected.
If you convert to int before you sort, it works for some cases, but you have other problems.
What I might do to detect a straight would be:
* sort all the unique ranks
* see if the card 4 slots away has a rank 4 less than the current card
But because this list is not sorted, your straight is actually made of the cards: a,b,c,d,g. Because g is smaller than the others, it isn't detected.
If you convert to int before you sort, it works for some cases, but you have other problems.
def checksuiteinplayershand(playershand, possiblesuite): suite = [[int(r), s] for r,s in playershand] suite.sort() a=int(suite[0][0]) b=int(suite[1][0]) ...
Output:$ python3 pokerhand.py
[['7', 'h'], ['5', 'h']]
[['13', 'h'], ['10', 'h'], ['12', 'h'], ['11', 'h'], ['9', 'h']]
[['7', 'h'], ['5', 'h'], ['13', 'h'], ['10', 'h'], ['12', 'h'], ['11', 'h'], ['9', 'h']]
checksuiteinplayershand: (True, 13)
But what would happen if the hand were 5, 10, 11, 12, 12, 12, 13? You wouldn't have a straight, but your function would say you did. You need to not only sort them, you need to remove duplicates. A set is a good way to do that. (you're sort of checking for duplicates, but incompletely)What I might do to detect a straight would be:
* sort all the unique ranks
* see if the card 4 slots away has a rank 4 less than the current card
hands = [ [[1, "s"], [2, "s"], [3, "s"], [4, "s"], [7, "s"], [8, "s"], [9, "s"]], # not a straight [[2, "s"], [8, "s"], [5, "s"], [7, "s"], [6, "s"], [10, "s"], [4, "s"]], # straight [[1, "s"], [10, "s"], [11, "s"], [3, "s"], [12, "s"], [3, "h"], [13, "s"]], # straight (ace high) [[2, "s"], [2, "h"], [5, "s"], [5, "h"], [4, "s"], [6, "s"], [3, "s"]], # straight (duplicate cards) ] def is_a_straight(hand): """Looks for straight in set of hands. Returns top card of highest straight""" size_straight = 5 # must have this many in a row # if we reverse sort, we'll find the highest straight when going front to back. sorted_ranks = sorted({x[0] for x in hand}, reverse=True) if len(sorted_ranks) < size_straight: # can't be a straight return None for start in range(len(sorted_ranks) - size_straight + 1): if sorted_ranks[start] - sorted_ranks[start + size_straight - 1] == size_straight - 1: return sorted_ranks[start] # Check for ace_high if 1 in sorted_ranks: new_hand = [h for h in hand if h[0] != 1] new_hand.append([14, "x"]) return is_a_straight(new_hand) for hand in hands: print(is_a_straight(hand))
Output:None
8
14
6