Решение на Тесте карти от Весела Бандова

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

Към профила на Весела Бандова

Резултати

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

Код

class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return type(self) == type(other)
def __str__(self):
return self.__class__.__name__
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 Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
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.__init__(self, "red")
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
class Card(tuple):
__slots__ = []
def __new__(cls, rank, suit):
return tuple.__new__(cls, (rank(), suit()))
@property
def rank(self):
return tuple.__getitem__(self, 0)
@property
def suit(self):
return tuple.__getitem__(self, 1)
def __getitem__(self, item):
raise TypeError
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __repr__(self):
return "<Card {}>".format(str(self))
class CardCollection:
def __init__(self, collection=[]):
self.deck = [card for card in collection]
def draw(self, index):
card = self.deck.pop(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.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def __getitem__(self, index):
return self.deck[index]
def __iter__(self):
return iter(self.deck)
def __len__(self):
return len(self.deck)
def __str__(self):
return str(self.deck)
def __repr__(self):
return repr(self.deck)
def index(self, card):
for i in range(0, len(self.deck)):
if self.deck[i] == card:
return i
raise ValueError
SUITS = {"Diamonds": Diamonds, "Hearts": Hearts,
"Spades": Spades, "Clubs": Clubs}
RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five,
'Queen': Queen, 'Ten': Ten, 'Ace': Ace, 'Three': Three,
'Eight': Eight, 'Four': Four, 'Two': Two, 'Seven': Seven,
'Nine': Nine}
colors = [Diamonds, Clubs, Hearts, Spades]
ranking = [King, Queen, Jack, Ten, Nine, Eight, Seven,
Six, Five, Four, Three, Two, Ace]
def StandardDeck():
standart_deck = [Card(ranking[c % 13], colors[c // 13])
for c in range(0, 52)]
return CardCollection(standart_deck)
def BeloteDeck():
ranks = ranking[:7]
ranks.append(ranking[-1])
arr = [[r, c] for c in colors for r in ranks]
belote_deck = [Card(c[0], c[1]) for c in arr]
return CardCollection(belote_deck)
def SixtySixDeck():
ranks = ranking[:5]
ranks.append(ranking[-1])
arr = [[r, c] for c in colors for r in ranks]
sixty_six = [Card(c[0], c[1]) for c in arr]
return CardCollection(sixty_six)

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.024s

OK

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

Весела обнови решението на 26.03.2014 14:25 (преди над 10 години)

+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+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 Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, "A")
+
+
+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.__init__(self, "red")
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Card(tuple):
+ __slots__ = []
+
+ def __new__(cls, rank, suit):
+ return tuple.__new__(cls, (rank(), suit()))
+
+ @property
+ def rank(self):
+ return tuple.__getitem__(self, 0)
+
+ @property
+ def suit(self):
+ return tuple.__getitem__(self, 1)
+
+ def __getitem__(self, item):
+ raise TypeError
+
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __repr__(self):
+ return "<Card {}>".format(str(self))
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.deck = [card for card in collection]
+
+ def draw(self, index):
+ card = self.deck.pop(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.deck[-1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def __getitem__(self, index):
+ return self.deck[index]
+
+ def __iter__(self):
+ return iter(self.deck)
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __str__(self):
+ return str(self.deck)
+
+ def __repr__(self):
+ return repr(self.deck)
+
+ def index(self, card):
+ for i in range(0, len(self.deck)):
+ if self.deck[i] == card:
+ return i
+ raise ValueError
+
+
+SUITS = {"Diamonds": Diamonds, "Hearts": Hearts,
+ "Spades": Spades, "Clubs": Clubs}
+RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five,
+ 'Queen': Queen, 'Ten': Ten, 'Ace': Ace, 'Three': Three,
+ 'Eight': Eight, 'Four': Four, 'Two': Two, 'Seven': Seven,
+ 'Nine': Nine}
+
+colors = [Diamonds, Clubs, Hearts, Spades]
+ranking = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace]
+
+
+def StandardDeck():
+ standart_deck = [Card(ranking[c % 13], colors[c // 13])
+ for c in range(0, 52)]
+ return CardCollection(standart_deck)
+
+
+def BeloteDeck():
+ ranks = ranking[:7]
+ ranks.append(ranking[-1])
+ arr = [[r, c] for c in colors for r in ranks]
+ belote_deck = [Card(c[0], c[1]) for c in arr]
+ return CardCollection(belote_deck)
+
+
+def SixtySixDeck():
+ ranks = ranking[:5]
+ ranks.append(ranking[-1])
+ arr = [[r, c] for c in colors for r in ranks]
+ sixty_six = [Card(c[0], c[1]) for c in arr]
+ return CardCollection(sixty_six)
+