Last active
November 22, 2017 04:33
-
-
Save miknoj/88950dddded791cb1e9591d5d8830869 to your computer and use it in GitHub Desktop.
Revisions
-
miknoj revised this gist
Nov 22, 2017 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,6 @@ def shuffle(self): # Add cards onto deck in order. for card in self.__generate_ordered_cards(): self.deck.append(card) shuffle(self.deck) def fan(self): @@ -39,7 +38,7 @@ def fan(self): def isOrdered(self): ''' Returns true if deck is in order and false if not. This method makes sure to account for gaps. Therefore, if there is a card or a couple missing in the deck, but the remaining cards are still in order, this will return True. ''' -
miknoj created this gist
Nov 22, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ from collections import OrderedDict from random import shuffle class FairDeck(object): def __init__(self): ''' Default constructor. ''' self.deck = [] self.shuffle() def deal(self): ''' Pops and returns the top card of the deck. ''' # Check for emptiness. if self.deck: return self.deck.pop() else: print("Deck is empty.") def shuffle(self): ''' Returns all 52 cards back to the deck and shuffles them. ''' self.deck = [] # Clear the deck. # Add cards onto deck in order. for card in self.__generate_ordered_cards(): self.deck.append(card) shuffle(self.deck) def fan(self): ''' Prints cards in deck from first to last, all on one line. ''' for card in self.deck: print(card, end=' ') print("") # Newline. def isOrdered(self): ''' Returns true if deck is in order and false if not. This method makes sure to account for gaps. Therefore, there is a card or a couple missing in the deck, but the remaining cards are still in order, this will return True. ''' # An empty deck is an ordered deck. if not self.deck: return True ordered_cards = self.__sort_cards(self.deck) return ordered_cards == self.deck def Order(self): ''' Sorts the remaining cards of the deck. Relies on the hidden method __sort_cards to do the actual work. ''' # Just exit if deck is empty. if not self.deck: return self.deck = self.__sort_cards(self.deck) # Hidden helper methods. def __generate_ordered_cards(self): ''' Hidden method that generates ordered iterator of 52 card deck. ''' suits = ['C','D','H','S'] nums = list(range(2, 11)) faces = ['J','Q','K','A'] for suit in suits: for rank in nums + faces: yield str(rank)+suit def __sort_cards(self,deck): ''' Hidden method that sorts remaining cards of the deck. ''' ordered_dict = OrderedDict.fromkeys(self.__generate_ordered_cards()) for card in deck: ordered_dict[card] = True return [key for key in ordered_dict.keys() if ordered_dict[card]]