Created
April 28, 2016 17:11
-
-
Save vsinha/5d79f9e579a3e5a6a021864b22a6e041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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