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

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

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

Резултати

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

Код

class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __str__(self):
return type(self).__name__
def __eq__(self, other):
return type(self).__name__ == type(other).__name__
class Suit:
def __init__(self, color):
self.color = color
def __str__(self):
return type(self).__name__
def __eq__(self, other):
return type(self).__name__ == type(other).__name__
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Hearts(Suit):
def __init__(self):
Suit.color = 'red'
class Clubs(Suit):
def __init__(self):
Suit.color = 'black'
class Spades(Suit):
def __init__(self):
Suit.color = 'black'
class Diamonds(Suit):
def __init__(self):
Suit.color = 'red'
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}
SUITS = {'Hearts': Hearts,
'Clubs': Clubs,
'Spades': Spades,
'Diamonds': Diamonds}
class Card():
def __setattr__(self, *args):
raise TypeError("can't modify immutable instance")
__delattr__ = __setattr__
def __init__(self, Rank, Suit):
super(Card, self).__setattr__('rank', Rank())
super(Card, self).__setattr__('suit', Suit())
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return '{} of {}'.format(type(self.rank).__name__, self.suit)
def __repr__(self):
return 'Card {}'.format(self.__str__())
class CardCollection():
def __init__(self, collection=[]):
self.collection = [item for item in collection]
def add(self, card):
self.collection.append(card)
def __getitem__(self, index):
return self.collection[index]
def __iter__(self):
return iter(self.collection)
def __len__(self):
return len(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(len(self.collection)-1)
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[len(self.collection)-1]
def bottom_card(self):
return self.collection[0]
def index(self, index):
return self.collection.index(index)
def StandardDeck():
standard_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
standard_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
deck = []
for suit in standard_suits:
for rank in standard_ranks:
deck.append(Card(RANKS[rank], SUITS[suit]))
return CardCollection(deck)
def BeloteDeck():
standard_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Ace']
standard_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
deck = []
for suit in standard_suits:
for rank in standard_ranks:
deck.append(Card(RANKS[rank], SUITS[suit]))
return CardCollection(deck)
def SixtySixDeck():
standard_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
standard_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
deck = []
for suit in standard_suits:
for rank in standard_ranks:
deck.append(Card(RANKS[rank], SUITS[suit]))
return CardCollection(deck)

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

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

OK

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

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

+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __str__(self):
+ return type(self).__name__
+
+ def __eq__(self, other):
+ return type(self).__name__ == type(other).__name__
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __str__(self):
+ return type(self).__name__
+
+ def __eq__(self, other):
+ return type(self).__name__ == type(other).__name__
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.color = 'red'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.color = 'black'
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.color = 'black'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.color = 'red'
+
+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}
+
+SUITS = {'Hearts': Hearts,
+ 'Clubs': Clubs,
+ 'Spades': Spades,
+ 'Diamonds': Diamonds}
+
+
+class Card():
+ def __setattr__(self, *args):
+ raise TypeError("can't modify immutable instance")
+ __delattr__ = __setattr__
+
+ def __init__(self, Rank, Suit):
+ super(Card, self).__setattr__('rank', Rank())
+ super(Card, self).__setattr__('suit', Suit())
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return '{} of {}'.format(type(self.rank).__name__, self.suit)
+
+ def __repr__(self):
+ return 'Card {}'.format(self.__str__())
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.collection = [item for item in collection]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop(len(self.collection)-1)
+
+ def draw_from_bottom(self):
+ return self.collection.pop(0)
+
+ def top_card(self):
+ return self.collection[len(self.collection)-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def index(self, index):
+ return self.collection.index(index)
+
+
+def StandardDeck():
+ standard_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+ standard_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ deck = []
+ for suit in standard_suits:
+ for rank in standard_ranks:
+ deck.append(Card(RANKS[rank], SUITS[suit]))
+ return CardCollection(deck)
+
+
+def BeloteDeck():
+ standard_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Ace']
+ standard_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ deck = []
+ for suit in standard_suits:
+ for rank in standard_ranks:
+ deck.append(Card(RANKS[rank], SUITS[suit]))
+ return CardCollection(deck)
+
+
+def SixtySixDeck():
+ standard_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ standard_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ deck = []
+ for suit in standard_suits:
+ for rank in standard_ranks:
+ deck.append(Card(RANKS[rank], SUITS[suit]))
+ return CardCollection(deck)