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

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

Към профила на Георги Георгиев

Резултати

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

Код

RANK_SYMBOLS = {'King': 'K', 'Queen': 'Q', 'Jack': 'J',
'Ten': '10', 'Nine': '9', 'Eight': '8',
'Seven': '7', 'Six': '6', 'Five': '5',
'Four': '4', 'Three': '3', 'Two': '2',
'Ace': 'A'}
SUIT_COLORS = {'Diamonds': 'red', 'Hearts': 'red',
'Spades': 'black', 'Clubs': 'black'}
class ComparableDisplayable:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Rank(ComparableDisplayable):
pass
class Suit(ComparableDisplayable):
pass
RANKS = {rank: type(rank, (Rank,), {"symbol": symbol})
for (rank, symbol) in RANK_SYMBOLS.items()}
SUITS = {suit: type(suit, (Suit,), {"color": color})
for (suit, color) in SUIT_COLORS.items()}
class Card:
def __init__(self, rank, suit):
self._rank, self._suit = rank(), suit()
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
def __str__(self):
return '%s of %s' % (str(self._rank), 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._collection = list(collection)
def __len__(self):
return len(self._collection)
def __getitem__(self, key):
return self._collection[key]
def __iter__(self):
for card in self._collection:
yield card
def __repr__(self):
return str([str(card) for card in self._collection])
def add(self, card):
self._collection.append(card)
def draw(self, index):
return self._collection.pop(index)
def draw_from_top(self):
return self._collection.pop()
def draw_from_bottom(self):
return self._collection.pop(0)
def top_card(self):
return self._collection[-1]
def bottom_card(self):
return self._collection[0]
def index(self, card):
return self._collection.index(card)
class StandardDeck(CardCollection):
ranks = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two',
'Ace')
suits = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
def __init__(self):
cards = [Card(RANKS[rank], SUITS[suit])
for suit in self.suits
for rank in self.ranks]
super(StandardDeck, self).__init__(cards)
class BeloteDeck(StandardDeck):
ranks = ('King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace')
class SixtySixDeck(StandardDeck):
ranks = ('King', 'Queen', 'Jack',
'Ten', 'Nine', 'Ace')

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.017s

OK

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

Георги обнови решението на 26.03.2014 14:20 (преди над 10 години)

+RANK_SYMBOLS = {'King': 'K', 'Queen': 'Q', 'Jack': 'J',
+ 'Ten': '10', 'Nine': '9', 'Eight': '8',
+ 'Seven': '7', 'Six': '6', 'Five': '5',
+ 'Four': '4', 'Three': '3', 'Two': '2',
+ 'Ace': 'A'}
+
+RANK_ORDER = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two',
+ 'Ace']
+
+SUIT_COLORS = {'Diamonds': 'red', 'Hearts': 'red',
+ 'Spades': 'black', 'Clubs': 'black'}
+
+SUIT_ORDER = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+class ComparableDisplayable:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+
+class Rank(ComparableDisplayable):
+ pass
+
+
+class Suit(ComparableDisplayable):
+ pass
+
+RANKS = {rank: type(rank, (Rank,), {"symbol": symbol})
+ for (rank, symbol) in RANK_SYMBOLS.items()}
+
+SUITS = {suit: type(suit, (Suit,), {"color": color})
+ for (suit, color) in SUIT_COLORS.items()}
+
+
+class Card:
+ __RANK_ORDER__ = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two',
+ 'Ace']
+
+ __SUIT_ORDER__ = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ def __init__(self, rank, suit):
+ self._rank, self._suit = rank(), suit()
+
+ def __eq__(self, other):
+ return self._rank == other._rank and self._suit == other._suit
+
+ def __str__(self):
+ return '%s of %s' % (str(self._rank), 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.__collection = list(collection)
+
+ def __len__(self):
+ return len(self.__collection)
+
+ def __getitem__(self, key):
+ return self.__collection[key]
+
+ def __iter__(self):
+ for card in self.__collection:
+ yield card
+
+ def __repr__(self):
+ return str([str(card) for card in self.__collection])
+
+ def add(self, card):
+ self.__collection.append(card)
+
+ def draw(self, index):
+ return self.__collection.pop(index)
+
+ def draw_from_top(self):
+ return self.__collection.pop()
+
+ def draw_from_bottom(self):
+ return self.__collection.pop(0)
+
+ def top_card(self):
+ return self.__collection[-1]
+
+ def bottom_card(self):
+ return self.__collection[0]
+
+ def index(self, card):
+ return self.__collection.index(card)
+
+
+def StandardDeck():
+ return CardCollection([Card(RANKS[rank], SUITS[suit])
+ for suit in SUIT_ORDER
+ for rank in RANK_ORDER])
+
+def BeloteDeck():
+ return CardCollection([Card(RANKS[rank], SUITS[suit])
+ for suit in SUIT_ORDER
+ for rank in RANK_ORDER[:7] + RANK_ORDER[-1::]])
+
+def SixtySixDeck():
+ return CardCollection([Card(RANKS[rank], SUITS[suit])
+ for suit in SUIT_ORDER
+ for rank in RANK_ORDER[:5] + RANK_ORDER[-1::]])

Георги обнови решението на 26.03.2014 14:57 (преди над 10 години)

RANK_SYMBOLS = {'King': 'K', 'Queen': 'Q', 'Jack': 'J',
'Ten': '10', 'Nine': '9', 'Eight': '8',
'Seven': '7', 'Six': '6', 'Five': '5',
'Four': '4', 'Three': '3', 'Two': '2',
'Ace': 'A'}
-RANK_ORDER = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two',
- 'Ace']
-
SUIT_COLORS = {'Diamonds': 'red', 'Hearts': 'red',
'Spades': 'black', 'Clubs': 'black'}
-SUIT_ORDER = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-
class ComparableDisplayable:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Rank(ComparableDisplayable):
pass
class Suit(ComparableDisplayable):
pass
+
RANKS = {rank: type(rank, (Rank,), {"symbol": symbol})
for (rank, symbol) in RANK_SYMBOLS.items()}
SUITS = {suit: type(suit, (Suit,), {"color": color})
for (suit, color) in SUIT_COLORS.items()}
class Card:
- __RANK_ORDER__ = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two',
- 'Ace']
-
- __SUIT_ORDER__ = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-
def __init__(self, rank, suit):
self._rank, self._suit = rank(), suit()
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
def __str__(self):
return '%s of %s' % (str(self._rank), 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.__collection = list(collection)
+ self._collection = list(collection)
def __len__(self):
- return len(self.__collection)
+ return len(self._collection)
def __getitem__(self, key):
- return self.__collection[key]
+ return self._collection[key]
def __iter__(self):
- for card in self.__collection:
+ for card in self._collection:
yield card
def __repr__(self):
- return str([str(card) for card in self.__collection])
+ return str([str(card) for card in self._collection])
def add(self, card):
- self.__collection.append(card)
+ self._collection.append(card)
def draw(self, index):
- return self.__collection.pop(index)
+ return self._collection.pop(index)
def draw_from_top(self):
- return self.__collection.pop()
+ return self._collection.pop()
def draw_from_bottom(self):
- return self.__collection.pop(0)
+ return self._collection.pop(0)
def top_card(self):
- return self.__collection[-1]
+ return self._collection[-1]
def bottom_card(self):
- return self.__collection[0]
+ return self._collection[0]
def index(self, card):
- return self.__collection.index(card)
+ return self._collection.index(card)
-def StandardDeck():
- return CardCollection([Card(RANKS[rank], SUITS[suit])
- for suit in SUIT_ORDER
- for rank in RANK_ORDER])
+class StandardDeck(CardCollection):
+ ranks = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two',
+ 'Ace')
-def BeloteDeck():
- return CardCollection([Card(RANKS[rank], SUITS[suit])
- for suit in SUIT_ORDER
- for rank in RANK_ORDER[:7] + RANK_ORDER[-1::]])
+ suits = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
-def SixtySixDeck():
- return CardCollection([Card(RANKS[rank], SUITS[suit])
+ def __init__(self):
- for suit in SUIT_ORDER
+ cards = [Card(RANKS[rank], SUITS[suit])
- for rank in RANK_ORDER[:5] + RANK_ORDER[-1::]])
+ for suit in self.suits
+ for rank in self.ranks]
+ super(StandardDeck, self).__init__(cards)
+
+
+class BeloteDeck(StandardDeck):
+ ranks = ('King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace')
+
+
+class SixtySixDeck(StandardDeck):
+ ranks = ('King', 'Queen', 'Jack',
+ 'Ten', 'Nine', 'Ace')