Йордан обнови решението на 23.03.2014 03:32 (преди над 10 години)
+class Rank:
+ def __init__(self, card_value):
+ self.symbol = card_value
+
+ def __eq__(self, right):
+ return self.symbol == right.symbol
+
+
+class Suit:
+ def __init__(self, suit):
+ self.color = suit
+
+ def __eq__(self, right):
+ return self.color == right.color
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __str__(self):
+ return 'Six'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __str__(self):
+ return 'Four'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __str__(self):
+ return 'King'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __str__(self):
+ return 'Jack'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __str__(self):
+ return 'Five'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __str__(self):
+ return 'Queen'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __str__(self):
+ return 'Ten'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __str__(self):
+ return 'Three'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __str__(self):
+ return 'Eight'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __str__(self):
+ return 'Two'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __str__(self):
+ return 'Seven'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __str__(self):
+ return 'Nine'
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __str__(self):
+ return 'Ace'
+
+RANKS = {'Six': Six, 'Four': Four, 'King': King,
+ 'Jack': Jack, 'Five': Five, 'Queen': Queen,
+ 'Ten': Ten, 'Three': Three, 'Eight': Eight,
+ 'Two': Two, 'Seven': Seven, 'Nine': Nine,
+ 'Ace': Ace}
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __str__(self):
+ return 'Hearts'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __str__(self):
+ return 'Clubs'
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __str__(self):
+ return 'Spades'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __str__(self):
+ return 'Diamonds'
+
+
+SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
+ 'Clubs': Clubs, 'Spades': Spades}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<' + str(self.rank) + ' of ' + str(self.suit) + '>'
+
+ def __eq__(self, right):
+ return str(self) == str(right)
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.card_collection = collection
+
+ def draw(self, index):
+ if index < len(self.card_collection):
+ return self.card_collection.pop(index)
+ else:
+ return self.card_collection[index]
+
+ def draw_from_top(self):
+ return self.card_collection.pop(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.card_collection[-1]
+
+ def bottom_card(self):
+ return self.card_collection[0]
+
+ def add(self, card):
+ self.card_collection.append(card)
+
+ def index(self, card):
+ return self.card_collection.index(card)
+
+ def __getitem__(self, i):
+ return self.card_collection[i]
+
+ def __setitem__(self, i, value):
+ self.card_collection[i] = value
+
+ def __len__(self):
+ return len(self.card_collection)
+
+
+def StandardDeck():
+ collection = []
+ for card_suit in SUITS:
+ for card_rank in RANKS:
+ collection.append(Card(RANKS[card_rank], SUITS[card_suit]))
+ return collection
+
+NOT_IN_BELOTE = ['Two', 'Three', 'Four', 'Five', 'Six']
+
+NOTE_IN_SIXTYSIX = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
+
+
+def BeloteDeck():
+ collection = []
+ for card_suit in SUITS:
+ for card_rank in RANKS:
+ if card_rank not in NOT_IN_BELOTE:
+ collection.append(Card(RANKS[card_rank], SUITS[card_suit]))
+ return collection
+
+
+def SixtySixDeck():
+ collection = []
+ for card_suit in SUITS:
+ for card_rank in RANKS:
+ if card_rank not in NOTE_IN_SIXTYSIX:
+ collection.append(Card(RANKS[card_rank], SUITS[card_suit]))
+ return collection
Подредбата на стандартните тестета ти е различна всеки път като си пуснеш програмата. Пробвай! ;)
Иначе решението ти е ОК, но вместо да пишеш няколко вложени for
-a и if
-ове можеш да ползваш list comprehension.