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
#11
(Sep-03-2021, 04:54 AM)bowlofred Wrote: 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

Yeah, i did what you said and it works, but I still don't get why thy function return 'None'. the return statement of the function was "return possiblesuite, a" possiblesuite was set to False outside the function. So when the if statement didn't encounter a straight, because it sorted the list this way : ['10', 'h'], ['11','h'], ['12','h'],['13','h'],['9', 'h'] it should've returned False but instead it returned 'None'. Doesn't that happened only when a boolean value isn't set like possiblesuite = bool ? But mine was set to False...
Reply
#12
(Sep-03-2021, 06:04 PM)CompleteNewb Wrote: the return statement of the function was "return possiblesuite, a"

That is one possible return statement, but that statement is never reached for your example.

In lines 29-40, the outer if's all succeed (because the cards are unique), but the second-level ifs all fail (no straight is detected). So the function continues.

There's no ace, so line 41 fails and the rest of the code is skipped. Since the end of the function is reached with no return statement being executed, the function does a default return None. You could put a return False at the end of the function where it would be reached if all the other sections failed.
Reply
#13
(Sep-03-2021, 06:26 PM)bowlofred Wrote:
(Sep-03-2021, 06:04 PM)CompleteNewb Wrote: the return statement of the function was "return possiblesuite, a"

That is one possible return statement, but that statement is never reached for your example.

In lines 29-40, the outer if's all succeed (because the cards are unique), but the second-level ifs all fail (no straight is detected). So the function continues.

There's no ace, so line 41 fails and the rest of the code is skipped. Since the end of the function is reached with no return statement being executed, the function does a default return None. You could put a return False at the end of the function where it would be reached if all the other sections failed.

Oh okay, thank you very much you were very helpful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting the wrong answer, and not sure why riskeay 3 2,055 Nov-05-2020, 08:24 PM
Last Post: deanhystad
  Make the answer of input int and str enderfran2006 2 2,015 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  Keeps looping even after correct answer mcesmcsc 2 1,920 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,821 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE
  How to answer subprocess prompt Monty 8 17,407 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