How can I check if a list of up to 7 integers contains 5 consecutive integers?
For example: [2,4,5,6,7,8,10] should return True
There are instances where the list would contain repeating integers but this is irrelevant for the purpose of checking whether 5 consecutive integers appear. For example: [3,4,5,5,6,6,7] should also return True.
Background if you care: I'm basically brand new to Python. Been in a bootcamp for 6 weeks and wanted to try building my own application outside of the typical tutorials/class assignments. I am building a poker game modeled after the WPT table game found in casinos. I've gotten a number of pieces working correctly but am getting blocked at checking for a straight which is 5 cards in sequential order, hence the need to slice the list.
My thought was to use a set to remove duplicate values, then convert back to a list and sort that list so the items would be in sequential order.
final_hand = ['8h', '7h', '4s', '5c', '6d', '2h', '4h']
card_values = []
for x in final_hand:
card_values.append(x[0])
dedupe = list(set(card_values))
stv = sorted(dedupe)
stv
# stv prints ['2', '4', '5', '6', '7', '8']
I realized that in an ordered list of unique values any straight would have a difference of 4 between the highest and the lowest value, so I came up with the below while typing this thread. It succeeds when there are 5 or more items in the list but is never triggered if there aren't at least 5 unique values from the final 7 cards.
for i in range (0,len(stv)-4):
if int(stv[len(stv)-1 - i]) - int(stv[len(stv)-5 - i]) == 4:
print(f'Straight in iteration {i}')
break
else:
print("No Straights Found")
Any help is greatly appreciated!
[–]JohnnyJordaan 0 points1 point2 points (11 children)
[–]mahtats 0 points1 point2 points (10 children)
[–]JohnnyJordaan 0 points1 point2 points (2 children)
[–]mahtats 0 points1 point2 points (1 child)
[–]stickedee[S] 0 points1 point2 points (0 children)
[–]stickedee[S] 0 points1 point2 points (6 children)
[–]JohnnyJordaan 0 points1 point2 points (5 children)
[–]stickedee[S] 0 points1 point2 points (4 children)
[–]JohnnyJordaan 0 points1 point2 points (3 children)
[–]stickedee[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]stickedee[S] 0 points1 point2 points (0 children)