Решение на Тесте карти от Данаил Койчев

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

Към профила на Данаил Койчев

Резултати

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

Код

class RANK:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
class Ace(RANK):
symbol = 'A'
class Two(RANK):
symbol = '2'
class Three(RANK):
symbol = '3'
class Four(RANK):
symbol = '4'
class Five(RANK):
symbol = '5'
class Six(RANK):
symbol = '6'
class Seven(RANK):
symbol = '7'
class Eight(RANK):
symbol = '8'
class Nine(RANK):
symbol = '9'
class Ten(RANK):
symbol = '10'
class Jack(RANK):
symbol = 'J'
class Queen(RANK):
symbol = 'Q'
class King(RANK):
symbol = 'K'
class Suit:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
class Spades(Suit):
color = 'black'
class Hearts(Suit):
color = 'red'
class Diamonds(Suit):
color = 'red'
class Clubs(Suit):
color = 'black'
class Card:
def __init__(self, card_rank, card_suit):
object.__setattr__(self, 'rank', card_rank())
object.__setattr__(self, 'suit', card_suit())
def __str__(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, *args):
raise AttributeError("can't modify immutable instance")
__delattr__ = __setattr__
class CardCollection:
def __init__(self, collection=[]):
self.cards = [card for card in collection]
def __getitem__(self, key):
return self.cards[key]
def __iter__(self):
for card in self.cards:
yield card
def __len__(self):
return len(self.cards)
def add(self, card):
self.cards.append(card)
def draw(self, index):
return self.cards.pop(index)
def draw_from_top(self):
return self.draw(len(self.cards) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.cards[len(self.cards) - 1]
def bottom_card(self):
return self.cards[0]
def index(self, card):
return self.cards.index(card)
def SortedDeck(lowest_rank=2):
sorted_ranks = [King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five,
Four, Three, Two, Ace]
sorted_suits = [Diamonds, Clubs, Hearts, Spades]
sorted_deck = CardCollection()
for suit in sorted_suits:
for rank_number in range(0, 14 - lowest_rank):
sorted_deck.add(Card(sorted_ranks[rank_number], suit))
sorted_deck.add(Card(Ace, suit))
return sorted_deck
def StandardDeck():
return SortedDeck(2)
def BeloteDeck():
return SortedDeck(7)
def SixtySixDeck():
return SortedDeck(9)
SUITS = {'Spades': Spades, 'Hearts': Hearts, 'Diamonds': Diamonds,
'Clubs': Clubs}
RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
'Jack': Jack, 'Queen': Queen, 'King': King}

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

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

OK

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

Данаил обнови решението на 23.03.2014 22:26 (преди над 10 години)

+class RANK:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Ace(RANK):
+ symbol = 'A'
+
+
+class Two(RANK):
+ symbol = '2'
+
+
+class Three(RANK):
+ symbol = '3'
+
+
+class Four(RANK):
+ symbol = '4'
+
+
+class Five(RANK):
+ symbol = '5'
+
+
+class Six(RANK):
+ symbol = '6'
+
+
+class Seven(RANK):
+ symbol = '7'
+
+
+class Eight(RANK):
+ symbol = '8'
+
+
+class Nine(RANK):
+ symbol = '9'
+
+
+class Ten(RANK):
+ symbol = '10'
+
+
+class Jack(RANK):
+ symbol = 'J'
+
+
+class Queen(RANK):
+ symbol = 'Q'
+
+
+class King(RANK):
+ symbol = 'K'
+
+
+class Suit:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Spades(Suit):
+ color = 'black'
+
+
+class Hearts(Suit):
+ color = 'red'
+
+
+class Diamonds(Suit):
+ color = 'red'
+
+
+class Clubs(Suit):
+ color = 'black'
+
+
+class Card:
+ def __init__(self, card_rank, card_suit):
+ object.__setattr__(self, 'rank', card_rank())
+ object.__setattr__(self, 'suit', card_suit())
+
+ def __str__(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, *args):
+ raise AttributeError("can't modify immutable instance")
+
+ __delattr__ = __setattr__
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.cards = [card for card in collection]
+
+ def __getitem__(self, key):
+ return self.cards[key]
+
+ def __iter__(self):
+ for card in self.cards:
+ yield card
+
+ def __len__(self):
+ return len(self.cards)
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def draw(self, index):
+ return self.cards.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(len(self.cards) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.cards[len(self.cards) - 1]
+
+ def bottom_card(self):
+ return self.cards[0]
+
+ def index(self, card):
+ return self.cards.index(card)
+
+
+def SortedDeck(lowest_rank=2):
+ sorted_ranks = [King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five,
+ Four, Three, Two, Ace]
+ sorted_suits = [Diamonds, Clubs, Hearts, Spades]
+ sorted_deck = CardCollection()
+ for suit in sorted_suits:
+ for rank_number in range(0, 14 - lowest_rank):
+ sorted_deck.add(Card(sorted_ranks[rank_number], suit))
+ sorted_deck.add(Card(Ace, suit))
+ return sorted_deck
+
+
+def StandardDeck():
+ return SortedDeck(2)
+
+
+def BeloteDeck():
+ return SortedDeck(7)
+
+
+def SixtySixDeck():
+ return SortedDeck(9)
+
+
+SUITS = {'Spades': Spades, 'Hearts': Hearts, 'Diamonds': Diamonds,
+ 'Clubs': Clubs}
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
+ 'Jack': Jack, 'Queen': Queen, 'King': King}
+