Python Forum
why don't i get the answer i want
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why don't i get the answer i want
#6
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.

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
Reply


Messages In This Thread
why don't i get the answer i want - by CompleteNewb - Sep-02-2021, 10:59 PM
RE: why don't i get the answer i want - by bowlofred - Sep-03-2021, 04:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting the wrong answer, and not sure why riskeay 3 2,093 Nov-05-2020, 08:24 PM
Last Post: deanhystad
  Make the answer of input int and str enderfran2006 2 2,054 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  Keeps looping even after correct answer mcesmcsc 2 1,966 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  I'm getting a wrong answer don't know where the bug is 357mag 4 2,872 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE
  How to answer subprocess prompt Monty 8 17,528 Feb-14-2018, 09:59 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020