Решение на Тесте карти от Дарина Кръстева

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

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

Резултати

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

Код

class Rank:
symbol = ''
def __repr__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
class Suit:
color = ''
def __repr__(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 Spades(Suit):
color = 'black'
class Clubs(Suit):
color = 'black'
class Diamonds(Suit):
color = 'red'
class Hearts(Suit):
color = 'red'
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, 'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts':
Hearts}
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 __repr__(self):
return "{0} of {1}".format(str(self._rank), str(self._suit))
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
drawn_card = self.cards.pop(index)
return drawn_card
def draw_from_top(self):
drawn_card = self.cards.pop()
return drawn_card
def draw_from_bottom(self):
drawn_card = self.cards.pop(0)
return drawn_card
def top_card(self):
card_on_top = self.cards[len(self.cards) - 1]
return card_on_top
def bottom_card(self):
card_on_bottom = self.cards[0]
return card_on_bottom
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def __repr__(self):
cards_in_deck = ["<Card {0}>".format(str(card)) for card
in self.cards]
return ''.join(symbol for symbol in str(cards_in_deck)
if symbol != "'") # remove the quotes around card names
RANKS_IN_ORDER = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
SUITS_IN_ORDER = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
RANKS_IN_BELOTE_DECK = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Ace')
RANKS_IN_SIXTY_SIX_DECK = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace')
def generate_deck(ranks_included):
deck = CardCollection()
for suit_name in SUITS_IN_ORDER:
for rank_name in ranks_included:
deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
return deck
def StandardDeck():
return generate_deck(RANKS_IN_ORDER)
def BeloteDeck():
return generate_deck(RANKS_IN_BELOTE_DECK)
def SixtySixDeck():
return generate_deck(RANKS_IN_SIXTY_SIX_DECK)

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

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

OK

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

Дарина обнови решението на 23.03.2014 15:32 (преди над 10 години)

+class Rank:
+ symbol = ''
+
+ def __repr__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Suit:
+ color = ''
+
+ def __repr__(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 Spades(Suit):
+ color = 'black'
+
+
+class Clubs(Suit):
+ color = 'black'
+
+
+class Diamonds(Suit):
+ color = 'red'
+
+
+class Hearts(Suit):
+ color = 'red'
+
+
+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, 'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts':
+ Hearts}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __repr__(self):
+ return "{0} of {1}".format(str(self.rank), str(self.suit))
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __setattr__(self):
+ raise "Can't set attribute"
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.cards = list(collection)
+
+ def draw(self, index):
+ drawn_card = self.cards.pop(index)
+ return drawn_card
+
+ def draw_from_top(self):
+ drawn_card = self.cards.pop()
+ return drawn_card
+
+ def draw_from_bottom(self):
+ drawn_card = self.cards.pop(0)
+ return drawn_card
+
+ def top_card(self):
+ card_on_top = self.cards[len(self.cards) - 1]
+ return card_on_top
+
+ def bottom_card(self):
+ card_on_bottom = self.cards[0]
+ return card_on_bottom
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ return self.cards.index(card)
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def __len__(self):
+ return len(self.cards)
+
+ def __repr__(self):
+ cards_in_deck = "[ <Card {0}>".format(str(self.cards[0]))
+ for index in range(1, len(self.cards)):
+ cards_in_deck += ", <Card {0}>".format(str(self.cards[index]))
+ cards_in_deck += " ]"
+ return cards_in_deck
+
+RANKS_IN_ORDER = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
+
+
+SUITS_IN_ORDER = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
+
+
+RANKS_NOT_IN_BELOTE_DECK = ('Six', 'Five', 'Four', 'Three', 'Two')
+
+
+RANKS_NOT_IN_SIXTY_SIX_DECK = ('Eight', 'Seven', 'Six', 'Five', 'Four',
+ 'Three', 'Two')
+
+
+def StandardDeck():
+ deck = CardCollection()
+ for suit_name in SUITS_IN_ORDER:
+ for rank_name in RANKS_IN_ORDER:
+ deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ for suit_name in SUITS_IN_ORDER:
+ for rank_name in RANKS_IN_ORDER:
+ if(rank_name not in RANKS_NOT_IN_BELOTE_DECK):
+ belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ for suit_name in SUITS_IN_ORDER:
+ for rank_name in RANKS_IN_ORDER:
+ if(rank_name not in RANKS_NOT_IN_SIXTY_SIX_DECK):
+ sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return sixty_six_deck

Решението ти е валидно. Но:

  • Защо използваш __setattr__, след като имаш синтаксис за това?
  • Функциите на трите тестета са ти абсолютно еднакви. Трябва да намериш начин да ги генерализираш :)
  • Не ми харесва как си имплементирала __repr__. Не конкратенирай стрингове

П.П.: Още веднъж, извинения за късния отговор :)

Дарина обнови решението на 26.03.2014 12:24 (преди над 10 години)

class Rank:
symbol = ''
def __repr__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
class Suit:
color = ''
def __repr__(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 Spades(Suit):
color = 'black'
class Clubs(Suit):
color = 'black'
class Diamonds(Suit):
color = 'red'
class Hearts(Suit):
color = 'red'
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, 'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts':
Hearts}
class Card:
def __init__(self, rank, suit):
- object.__setattr__(self, 'rank', rank())
- object.__setattr__(self, 'suit', suit())
+ self._rank = rank()
+ self._suit = suit()
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
def __repr__(self):
- return "{0} of {1}".format(str(self.rank), str(self.suit))
+ return "{0} of {1}".format(str(self._rank), str(self._suit))
def __eq__(self, other):
- return self.rank == other.rank and self.suit == other.suit
+ return self._rank == other._rank and self._suit == other._suit
- def __setattr__(self):
- raise "Can't set attribute"
-
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
drawn_card = self.cards.pop(index)
return drawn_card
def draw_from_top(self):
drawn_card = self.cards.pop()
return drawn_card
def draw_from_bottom(self):
drawn_card = self.cards.pop(0)
return drawn_card
def top_card(self):
card_on_top = self.cards[len(self.cards) - 1]
return card_on_top
def bottom_card(self):
card_on_bottom = self.cards[0]
return card_on_bottom
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def __repr__(self):
- cards_in_deck = "[ <Card {0}>".format(str(self.cards[0]))
- for index in range(1, len(self.cards)):
- cards_in_deck += ", <Card {0}>".format(str(self.cards[index]))
- cards_in_deck += " ]"
- return cards_in_deck
+ cards_in_deck = ["<Card {0}>".format(str(card)) for card
+ in self.cards]
+ return ''.join(symbol for symbol in str(cards_in_deck)
+ if symbol != "'") # remove the quotes around card names
+
RANKS_IN_ORDER = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
SUITS_IN_ORDER = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
-RANKS_NOT_IN_BELOTE_DECK = ('Six', 'Five', 'Four', 'Three', 'Two')
+RANKS_IN_BELOTE_DECK = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Ace')
-RANKS_NOT_IN_SIXTY_SIX_DECK = ('Eight', 'Seven', 'Six', 'Five', 'Four',
- 'Three', 'Two')
+RANKS_IN_SIXTY_SIX_DECK = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace')
-def StandardDeck():
+def generate_deck(ranks_included):
deck = CardCollection()
for suit_name in SUITS_IN_ORDER:
- for rank_name in RANKS_IN_ORDER:
+ for rank_name in ranks_included:
deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
return deck
+def StandardDeck():
+ return generate_deck(RANKS_IN_ORDER)
+
+
def BeloteDeck():
- belote_deck = CardCollection()
- for suit_name in SUITS_IN_ORDER:
- for rank_name in RANKS_IN_ORDER:
- if(rank_name not in RANKS_NOT_IN_BELOTE_DECK):
- belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
- return belote_deck
+ return generate_deck(RANKS_IN_BELOTE_DECK)
def SixtySixDeck():
- sixty_six_deck = CardCollection()
- for suit_name in SUITS_IN_ORDER:
+ return generate_deck(RANKS_IN_SIXTY_SIX_DECK)
- for rank_name in RANKS_IN_ORDER:
- if(rank_name not in RANKS_NOT_IN_SIXTY_SIX_DECK):
- sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
- return sixty_six_deck