Решение на Тесте карти от Станислав Гатев

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

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

Резултати

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

Код

class Rank:
def __init__(self, name, symbol):
self._name = name
self._symbol = symbol
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self._name
@property
def symbol(self):
return self._symbol
class King(Rank):
def __init__(self):
super().__init__('King', 'K')
class Six(Rank):
def __init__(self):
super().__init__('Six', '6')
class Jack(Rank):
def __init__(self):
super().__init__('Jack', 'J')
class Five(Rank):
def __init__(self):
super().__init__('Five', '5')
class Queen(Rank):
def __init__(self):
super().__init__('Queen', 'Q')
class Ten(Rank):
def __init__(self):
super().__init__('Ten', '10')
class Ace(Rank):
def __init__(self):
super().__init__('Ace', 'A')
class Three(Rank):
def __init__(self):
super().__init__('Three', '3')
class Eight(Rank):
def __init__(self):
super().__init__('Eight', '8')
class Four(Rank):
def __init__(self):
super().__init__('Four', '4')
class Two(Rank):
def __init__(self):
super().__init__('Two', '2')
class Seven(Rank):
def __init__(self):
super().__init__('Seven', '7')
class Nine(Rank):
def __init__(self):
super().__init__('Nine', '9')
class Suit:
def __init__(self, name, color):
self._name = name
self._color = color
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self._name
@property
def color(self):
return self._color
class Hearts(Suit):
def __init__(self):
super().__init__('Hearts', 'red')
class Clubs(Suit):
def __init__(self):
super().__init__('Clubs', 'black')
class Spades(Suit):
def __init__(self):
super().__init__('Spades', 'black')
class Diamonds(Suit):
def __init__(self):
super().__init__('Diamonds', 'red')
RANKS = {rank.__name__: rank for rank in Rank.__subclasses__()}
SUITS = {suit.__name__: suit for suit in Suit.__subclasses__()}
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return str(self)
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
class CardCollection:
def __init__(self, collection=[]):
self._cards = [card for card in collection]
def __len__(self):
return len(self._cards)
def __getitem__(self, index):
return self._cards[index]
def __iter__(self):
for card in self._cards:
yield card
def __repr__(self):
return str(self._cards)
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):
self._cards.append(card)
def index(self, card):
return self._cards.index(card)
def StandardDeck():
ranks_order = ["King", "Queen", "Jack", "Ten",
"Nine", "Eight", "Seven", "Six",
"Five", "Four", "Three", "Two", "Ace"]
suits_order = ["Diamonds", "Clubs", "Hearts", "Spades"]
return CardCollection([Card(RANKS[rank], SUITS[suit])
for suit in suits_order
for rank in ranks_order])
def ranks_for(*names):
return [RANKS[name]() for name in names]
def filter_cards(cards, ranks=[]):
return CardCollection(list(filter(
lambda card: card.rank not in ranks, cards)))
def BeloteDeck():
return filter_cards(StandardDeck(), ranks=ranks_for(
"Six", "Five", "Four", "Three", "Two"))
def SixtySixDeck():
return filter_cards(BeloteDeck(), ranks=ranks_for("Seven", "Eight"))

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.029s

OK

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

Станислав обнови решението на 26.03.2014 16:30 (преди около 10 години)

+class Rank:
+ def __init__(self, name, symbol):
+ self._name = name
+ self._symbol = symbol
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self._name
+
+ @property
+ def symbol(self):
+ return self._symbol
+
+
+class King(Rank):
+ def __init__(self):
+ super().__init__('King', 'K')
+
+
+class Six(Rank):
+ def __init__(self):
+ super().__init__('Six', '6')
+
+
+class Jack(Rank):
+ def __init__(self):
+ super().__init__('Jack', 'J')
+
+
+class Five(Rank):
+ def __init__(self):
+ super().__init__('Five', '5')
+
+
+class Queen(Rank):
+ def __init__(self):
+ super().__init__('Queen', 'Q')
+
+
+class Ten(Rank):
+ def __init__(self):
+ super().__init__('Ten', '10')
+
+
+class Ace(Rank):
+ def __init__(self):
+ super().__init__('Ace', 'A')
+
+
+class Three(Rank):
+ def __init__(self):
+ super().__init__('Three', '3')
+
+
+class Eight(Rank):
+ def __init__(self):
+ super().__init__('Eight', '8')
+
+
+class Four(Rank):
+ def __init__(self):
+ super().__init__('Four', '4')
+
+
+class Two(Rank):
+ def __init__(self):
+ super().__init__('Two', '2')
+
+
+class Seven(Rank):
+ def __init__(self):
+ super().__init__('Seven', '7')
+
+
+class Nine(Rank):
+ def __init__(self):
+ super().__init__('Nine', '9')
+
+
+class Suit:
+ def __init__(self, name, color):
+ self._name = name
+ self._color = color
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self._name
+
+ @property
+ def color(self):
+ return self._color
+
+
+class Hearts(Suit):
+ def __init__(self):
+ super().__init__('Hearts', 'red')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ super().__init__('Clubs', 'black')
+
+
+class Spades(Suit):
+ def __init__(self):
+ super().__init__('Spades', 'black')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ super().__init__('Diamonds', 'red')
+
+
+RANKS = {rank.__name__: rank for rank in Rank.__subclasses__()}
+SUITS = {suit.__name__: suit for suit in Suit.__subclasses__()}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank) + " of " + str(self.suit)
+
+ def __repr__(self):
+ return str(self)
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self._cards = [card for card in collection]
+
+ def __len__(self):
+ return len(self._cards)
+
+ def __getitem__(self, index):
+ return self._cards[index]
+
+ def __iter__(self):
+ for card in self._cards:
+ yield card
+
+ def __repr__(self):
+ return str(self._cards)
+
+ 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):
+ self._cards.append(card)
+
+ def index(self, card):
+ return self._cards.index(card)
+
+
+def StandardDeck():
+ ranks_order = ["King", "Queen", "Jack", "Ten",
+ "Nine", "Eight", "Seven", "Six",
+ "Five", "Four", "Three", "Two", "Ace"]
+ suits_order = ["Diamonds", "Clubs", "Hearts", "Spades"]
+
+ return CardCollection([Card(RANKS[rank], SUITS[suit])
+ for suit in suits_order
+ for rank in ranks_order])
+
+
+def ranks_for(*names):
+ return [RANKS[name]() for name in names]
+
+
+def filter_cards(cards, ranks=[]):
+ return CardCollection(list(filter(
+ lambda card: card.rank not in ranks, cards)))
+
+
+def BeloteDeck():
+ return filter_cards(StandardDeck(), ranks=ranks_for(
+ "Six", "Five", "Four", "Three", "Two"))
+
+
+def SixtySixDeck():
+ return filter_cards(BeloteDeck(), ranks=ranks_for("Seven", "Eight"))