Skip to content

Instantly share code, notes, and snippets.

@vsinha
Created April 28, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsinha/5d79f9e579a3e5a6a021864b22a6e041 to your computer and use it in GitHub Desktop.
Save vsinha/5d79f9e579a3e5a6a021864b22a6e041 to your computer and use it in GitHub Desktop.
def searchForStraight(self, cards, length):
expected = cards[0].rank # initialize to first card
counter = 0 # keep track of length of# the current straight
for card in cards:
#print('rank: ', card.rank, ', expect: ', expected)
if card.rank == expected: # we are inside a "straight",
# potentially of length 1
counter += 1
elif card.rank == expected-1: # if this is another of the card we just saw
# ie: [2 2 3 3 4] has a straight
None # ignore this card, just go to the next one
else: # if card is not what we expected:
counter = 1 # reset
expected = card.rank # start looking again
expected += 1 # we expect the next rank
if counter >= length: # check if we have succeeded
return True
return False # if we are here, there are no straights
def hasStraight(self, length, sameSuit):
if sameSuit: #do a search for each suit
self.sortBySuit()
group = groupby(self.cards, key=lambda card: card.suit)
for suit, items in group:
search = self.searchForStraight(list(items), length)
if search == True:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment