Reputation: 9536
The following code checks for all straights in a five-card hand (except 5-high, which I add in as an elif)
def straight(values,vset):
if (max(vset) - min(vset) == 4) and numpair(values) == False and detset(values) == False and quads(values) == False:
#STRAIGHT DETECTED
Vset is just a set containing the values. The problem is, I cannot figure out a way to adapt this code to evaluate a 7-card holdem hand. Any advice?
Upvotes: 0
Views: 765
Reputation: 106523
While @JohnGordon's solution works it wastefully iterates 5 times for each rank value.
A more efficient approach would be to iterate the rank from 2 to 14 and simply use a counter to keep track of the number of times so far the rank exists in the cards consecutively, and if a consecutive rank doesn't exist, reset the counter. Determine that there is a straight if the counter reaches 5. To account for the fact that an Ace (assuming its rank is 14 here) can be regarded as a 1 to form a straight with 2, 3, 4, and 5 as well, you can prepend 14 to the range of 2 to 14 for iteration:
count = 0
for rank in (14, *range(2, 15)):
if rank in vset:
count += 1
if count == 5:
print('Straight found')
break
else:
count = 0
else:
print('Straight not found')
Upvotes: 3
Reputation: 33335
I don't know how the code works now, because you haven't shown us the code for numpair()
, detset()
and quads()
.
However, working from a clean slate, this is how I would do it:
# assume rank values are numeric 2-10, J=11, Q=12, K=13, A=14
# iterate over each rank 2 thru 10
for rank in range(2, 11):
# if rank+0, rank+1, rank+2, rank+3, and rank+4 are all present, we have a straight
if all(rank+n in vset for n in range(0,5)):
print 'we have a straight'
break
# if we never broke out of the loop, we never found a straight
else:
print 'we do not have a straight'
Upvotes: 0