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

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

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

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self.__class__.__name__
class Suite:
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self.__class__.__name__
RANK_NAMES = OrderedDict([('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')])
RANKS = {name: type(name, (Rank,), {'symbol': symbol})
for name, symbol in RANK_NAMES.items()}
SUIT_NAMES = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
('Hearts', 'red'), ('Spades', 'black')])
SUITS = {name: type(name, (Suite,), {'color': color})
for name, color in SUIT_NAMES.items()}
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 __str__(self):
return "{0} of {1}".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.cards = [card for card in collection]
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def draw(self, index):
card = self[index]
del self.cards[index]
return card
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self[-1]
def bottom_card(self):
return self[0]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
class StandardDeck(CardCollection):
def __init__(self):
self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_NAMES
for rank_name in RANK_NAMES]
class BeloteDeck(CardCollection):
def __init__(self):
self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_NAMES
for rank_name in RANK_NAMES
if self.is_belote_rank(rank_name)]
def is_belote_rank(self, rank):
return rank in {'King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace'}
class SixtySixDeck(CardCollection):
def __init__(self):
self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_NAMES
for rank_name in RANK_NAMES
if self.is_sixty_six_rank(rank_name)]
def is_sixty_six_rank(self, rank):
return rank in {'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'}

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.019s

OK

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

Никола обнови решението на 25.03.2014 00:00 (преди над 10 години)

+from collections import OrderedDict
+
+
+class Rank:
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suite:
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+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 __str__(self):
+ return "{0} of {1}".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.cards = [card for card in collection]
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def __len__(self):
+ return len(self.cards)
+
+ def draw(self, index):
+ card = self[index]
+ del self.cards[index]
+ return card
+
+ def draw_from_top(self):
+ return self.draw(len(self) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self[len(self) - 1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ return self.cards.index(card)
+
+
+RANK_NAMES = OrderedDict([('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')])
+
+RANKS = {name: type(name, (Rank,), {'symbol': symbol})
+ for name, symbol in RANK_NAMES.items()}
+
+
+SUIT_NAMES = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
+ ('Hearts', 'red'), ('Spades', 'black')])
+
+SUITS = {name: type(name, (Suite,), {'color': color})
+ for name, color in SUIT_NAMES.items()}
+
+
+class StandardDeck(CardCollection):
+ def __init__(self):
+ self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_NAMES
+ for rank_name in RANK_NAMES]
+
+
+class BeloteDeck(CardCollection):
+ def __init__(self):
+ self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_NAMES
+ for rank_name in RANK_NAMES
+ if self.is_belote_rank(rank_name)]
+
+ def is_belote_rank(self, rank):
+ return rank in {'King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace'}
+
+
+class SixtySixDeck(CardCollection):
+ def __init__(self):
+ self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_NAMES
+ for rank_name in RANK_NAMES
+ if self.is_sixty_six_rank(rank_name)]
+
+ def is_sixty_six_rank(self, rank):
+ return rank in {'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'}

Никола обнови решението на 25.03.2014 20:13 (преди над 10 години)

from collections import OrderedDict
class Rank:
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self.__class__.__name__
class Suite:
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self.__class__.__name__
+RANK_NAMES = OrderedDict([('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')])
+
+RANKS = {name: type(name, (Rank,), {'symbol': symbol})
+ for name, symbol in RANK_NAMES.items()}
+
+
+SUIT_NAMES = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
+ ('Hearts', 'red'), ('Spades', 'black')])
+
+SUITS = {name: type(name, (Suite,), {'color': color})
+ for name, color in SUIT_NAMES.items()}
+
+
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 __str__(self):
return "{0} of {1}".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.cards = [card for card in collection]
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def draw(self, index):
card = self[index]
del self.cards[index]
return card
def draw_from_top(self):
- return self.draw(len(self) - 1)
+ return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
- return self[len(self) - 1]
+ return self[-1]
def bottom_card(self):
return self[0]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
-
-
-RANK_NAMES = OrderedDict([('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')])
-
-RANKS = {name: type(name, (Rank,), {'symbol': symbol})
- for name, symbol in RANK_NAMES.items()}
-
-
-SUIT_NAMES = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
- ('Hearts', 'red'), ('Spades', 'black')])
-
-SUITS = {name: type(name, (Suite,), {'color': color})
- for name, color in SUIT_NAMES.items()}
class StandardDeck(CardCollection):
def __init__(self):
self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_NAMES
for rank_name in RANK_NAMES]
class BeloteDeck(CardCollection):
def __init__(self):
self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_NAMES
for rank_name in RANK_NAMES
if self.is_belote_rank(rank_name)]
def is_belote_rank(self, rank):
return rank in {'King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace'}
class SixtySixDeck(CardCollection):
def __init__(self):
self.cards = [Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_NAMES
for rank_name in RANK_NAMES
if self.is_sixty_six_rank(rank_name)]
def is_sixty_six_rank(self, rank):
return rank in {'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'}

collection=[] - не е добра идея да използваш mutable тип за подразбиращ се аргумент. Можеш да разгледаш за този въпрос, въпреки, че в този код няма да направи проблем. Правилно си се ориентирал да използваш type за динамично дефиниране на класове. Сравняването по тип не е достатъчно експлицитно: return type(self) == type(other). Бих ти препоръчал да сръвняваш по някой атрибут, например color.

За аргументите по подразбиране съм наясно и както каза съобразих, че в случая няма да има проблем. Разбира се ако се смята за ужасно нещо, което трябва да се избягва на всяка цена ще го сменя, просто реших, че е най-краткото и ясно нещо, което решава проблема. За type съм съгласен, че не е достатъчно говоримо, но не се сетих по какво друго може - color не работи, защото ще излезе, че купата е каро и спатията е пика.