1

I've been making a poker simulator and I've managed to make a function which can identify an array which has consecutive numbers.

def straightCheck(playerHand):
    playerHand.sort()
    print(playerHand)
    for i in range(len(playerHand)-1):
        if playerHand[i] != playerHand [i+1] - 1:
            return False
            print(handstrength)
    return True
    print(handstrength)

The only problem is that I want the function to identify 5 consecutive numbers in the array when the length of the array is greater than 5. For example: I want the array [1,2,3,4,5,6,7] to return True but i also want the array [1,3,4,5,6,7,9] to return True.

0

4 Answers 4

2

You're returning False too soon. Instead, you could keep a running tally of the amount of consecutive numbers you've seen so far, and reset it when you come across a number that breaks the streak.

def straightCheck(playerHand):
    playerHand.sort()
    tally = 1
    for i in range(len(playerHand)-1):
        if playerHand[i] != playerHand [i+1] - 1:
            tally = 0
        tally += 1
        if tally >= 5:
            return True
    return False
Sign up to request clarification or add additional context in comments.

Comments

1

Right now you check if numbers are not consecutive and then return false. I think you could better check if numbers are consecutive and if so raise a counter and if not then reset it. That way you know how many numbers are consecutive. If that is 5 or higher you should return True.

Comments

1

If you have a working function, you could just process all the 5-card sets in a loop:

for i in range(len(values) - 4):
    is_straight = straightCheck(values[i:i+5])
    if is_straight:
        break
print(is_straight)

Comments

-2
def straightCheck(playerHand):
    playerHand.sort()
    print(playerHand)
    count = 0;
    for i in range(len(playerHand)-1):
        if playerHand[i] == playerHand [i+1] - 1:
            count += 1
            if count >= 5:
                return True
        else:
            count = 0

    return False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.