Решение на Тесте карти от Стефан Маринов

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

Към профила на Стефан Маринов

Резултати

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

Код

class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return self.__class__.__name__
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + self.__str__() + '>'
class CardCollection():
def __init__(self, collection=[]):
self._cards = list(collection)
def __getitem__(self, index):
# TODO; add custom IndexError
return self._cards[index]
def __iter__(self):
return iter(self._cards)
def __len__(self):
return len(self._cards)
def __str__(self):
return str(self._cards)
def __repr__(self):
return str(self)
def draw(self, index):
return self._cards.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self._cards[-1]
def bottom_card(self):
return self._cards[0]
def add(self, card):
return self._cards.append(card)
def index(self, card):
# TODO: add custom ValueError
return self._cards.index(card)
def StandardDeck():
deck = CardCollection([])
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
def BeloteDeck():
deck = StandardDeck()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ['Six', 'Five', 'Four', 'Three', 'Two']:
deck.draw(deck.index(Card(RANKS[rank], SUITS[suit])))
return deck
def SixtySixDeck():
deck = StandardDeck()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ['Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two']:
deck.draw(deck.index(Card(RANKS[rank], SUITS[suit])))
return deck
RANKS = {str(King()): King, str(Queen()): Queen, str(Jack()): Jack,
str(Ten()): Ten, str(Nine()): Nine, str(Eight()): Eight,
str(Seven()): Seven, str(Six()): Six, str(Five()): Five,
str(Four()): Four, str(Three()): Three, str(Two()): Two,
str(Ace()): Ace}
SUITS = {str(Diamonds()): Diamonds, str(Hearts()): Hearts,
str(Spades()): Spades, str(Clubs()): Clubs}

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.031s

OK

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

Стефан обнови решението на 26.03.2014 16:22 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + self.__str__() + '>'
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self._cards = list(collection)
+
+ def __getitem__(self, index):
+ # TODO; add custom IndexError
+ return self._cards[index]
+
+ def __iter__(self):
+ return iter(self._cards)
+
+ def __len__(self):
+ return len(self._cards)
+
+ def __str__(self):
+ return str(self._cards)
+
+ def __repr__(self):
+ return str(self)
+
+ def draw(self, index):
+ return self._cards.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self._cards[-1]
+
+ def bottom_card(self):
+ return self._cards[0]
+
+ def add(self, card):
+ return self._cards.append(card)
+
+ def index(self, card):
+ # TODO: add custom ValueError
+ return self._cards.index(card)
+
+
+def StandardDeck():
+ deck = CardCollection([])
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def BeloteDeck():
+ deck = StandardDeck()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['Six', 'Five', 'Four', 'Three', 'Two']:
+ deck.draw(deck.index(Card(RANKS[rank], SUITS[suit])))
+ return deck
+
+
+def SixtySixDeck():
+ deck = StandardDeck()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two']:
+ deck.draw(deck.index(Card(RANKS[rank], SUITS[suit])))
+ return deck
+
+RANKS = {str(King()): King, str(Queen()): Queen, str(Jack()): Jack,
+ str(Ten()): Ten, str(Nine()): Nine, str(Eight()): Eight,
+ str(Seven()): Seven, str(Six()): Six, str(Five()): Five,
+ str(Four()): Four, str(Three()): Three, str(Two()): Two,
+ str(Ace()): Ace}
+
+SUITS = {str(Diamonds()): Diamonds, str(Hearts()): Hearts,
+ str(Spades()): Spades, str(Clubs()): Clubs}

Този път малко я проспах задачата и за малко да изтърва срока. Видях няколко глупости, които в последствие май оправих:

  1. На 128-ми ред self.__str__() може да се замени просто със str(self).
  2. Мога веднъж да декларирам валидните стойности и бои в правилния ред и да ги ползвам направо при създаването на класовете за картите (Jack, Hearts и.т.н.) и трите функции за генерация на стандартни тестета.
  3. CardCollection може просто да наследява list.