Решение на Тесте карти от Лъчезар Николов

Обратно към всички решения

Към профила на Лъчезар Николов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 16 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Rank:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
class Ace(Rank):
symbol = 'A'
class King(Rank):
symbol = 'K'
class Queen(Rank):
symbol = 'Q'
class Jack(Rank):
symbol = 'J'
class Ten(Rank):
symbol = '10'
class Nine(Rank):
symbol = '9'
class Eight(Rank):
symbol = '8'
class Seven(Rank):
symbol = '7'
class Six(Rank):
symbol = '6'
class Five(Rank):
symbol = '5'
class Four(Rank):
symbol = '4'
class Three(Rank):
symbol = '3'
class Two(Rank):
symbol = '2'
class Suit:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
class Spades(Suit):
color = 'black'
class Hearts(Suit):
color = 'red'
class Diamonds(Suit):
color = 'red'
class Clubs(Suit):
color = 'black'
RANKS = {'Ace': Ace, 'King': King, 'Queen': Queen, 'Jack': Jack, 'Ten': Ten,
'Nine': Nine, 'Eight': Eight, 'Seven': Seven, 'Six': Six,
'Five': Five, 'Four': Four, 'Three': Three, 'Two': Two}
SUITS = {'Spades': Spades, 'Hearts': Hearts,
'Diamonds': Diamonds, 'Clubs': Clubs}
class Card:
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __str__(self):
return '{} of {}'.format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self.__deck = list(collection)
def __getitem__(self, index):
return self.__deck[index]
def __len__(self):
return len(self.__deck)
def draw(self, index):
return self.__deck.pop(index)
def draw_from_top(self):
return self.draw(len(self.__deck) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.__deck[len(self.__deck) - 1]
def bottom_card(self):
return self.__deck[0]
def add(self, card):
self.__deck.append(card)
def index(self, card):
return self.__deck.index(card)
ALL_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']
SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ALL_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def generate_deck(ranks, suits):
deck = []
for suit in suits:
for rank in ranks:
deck.append(Card(RANKS[rank], SUITS[suit]))
return deck
class StandardDeck(CardCollection):
def __init__(self):
standard_deck = generate_deck(ALL_RANKS, ALL_SUITS)
CardCollection.__init__(self, standard_deck)
class BeloteDeck(CardCollection):
def __init__(self):
belote_deck = generate_deck(BELOTE_RANKS, ALL_SUITS)
CardCollection.__init__(self, belote_deck)
class SixtySixDeck(CardCollection):
def __init__(self):
sixty_six_deck = generate_deck(SIXTY_SIX_RANKS, ALL_SUITS)
CardCollection.__init__(self, sixty_six_deck)

Лог от изпълнението

................
----------------------------------------------------------------------
Ran 16 tests in 0.020s

OK

История (1 версия и 0 коментара)

Лъчезар обнови решението на 26.03.2014 07:28 (преди над 10 години)

+class Rank:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Ace(Rank):
+ symbol = 'A'
+
+
+class King(Rank):
+ symbol = 'K'
+
+
+class Queen(Rank):
+ symbol = 'Q'
+
+
+class Jack(Rank):
+ symbol = 'J'
+
+
+class Ten(Rank):
+ symbol = '10'
+
+
+class Nine(Rank):
+ symbol = '9'
+
+
+class Eight(Rank):
+ symbol = '8'
+
+
+class Seven(Rank):
+ symbol = '7'
+
+
+class Six(Rank):
+ symbol = '6'
+
+
+class Five(Rank):
+ symbol = '5'
+
+
+class Four(Rank):
+ symbol = '4'
+
+
+class Three(Rank):
+ symbol = '3'
+
+
+class Two(Rank):
+ symbol = '2'
+
+
+class Suit:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Spades(Suit):
+ color = 'black'
+
+
+class Hearts(Suit):
+ color = 'red'
+
+
+class Diamonds(Suit):
+ color = 'red'
+
+
+class Clubs(Suit):
+ color = 'black'
+
+
+RANKS = {'Ace': Ace, 'King': King, 'Queen': Queen, 'Jack': Jack, 'Ten': Ten,
+ 'Nine': Nine, 'Eight': Eight, 'Seven': Seven, 'Six': Six,
+ 'Five': Five, 'Four': Four, 'Three': Three, 'Two': Two}
+
+
+SUITS = {'Spades': Spades, 'Hearts': Hearts,
+ 'Diamonds': Diamonds, 'Clubs': Clubs}
+
+
+class Card:
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ def __str__(self):
+ return '{} of {}'.format(self.rank, self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.__deck = list(collection)
+
+ def __getitem__(self, index):
+ return self.__deck[index]
+
+ def __len__(self):
+ return len(self.__deck)
+
+ def draw(self, index):
+ return self.__deck.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(len(self.__deck) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.__deck[len(self.__deck) - 1]
+
+ def bottom_card(self):
+ return self.__deck[0]
+
+ def add(self, card):
+ self.__deck.append(card)
+
+ def index(self, card):
+ return self.__deck.index(card)
+
+
+ALL_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']
+
+SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+
+ALL_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+def generate_deck(ranks, suits):
+ deck = []
+ for suit in suits:
+ for rank in ranks:
+ deck.append(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+class StandardDeck(CardCollection):
+ def __init__(self):
+ standard_deck = generate_deck(ALL_RANKS, ALL_SUITS)
+ CardCollection.__init__(self, standard_deck)
+
+
+class BeloteDeck(CardCollection):
+ def __init__(self):
+ belote_deck = generate_deck(BELOTE_RANKS, ALL_SUITS)
+ CardCollection.__init__(self, belote_deck)
+
+
+class SixtySixDeck(CardCollection):
+ def __init__(self):
+ sixty_six_deck = generate_deck(SIXTY_SIX_RANKS, ALL_SUITS)
+ CardCollection.__init__(self, sixty_six_deck)