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

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

Към профила на Евгени Евлогиев

Резултати

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

Код

STANDARD_DECK = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
BELOTE_DECK = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']
SIXTYSIX_DECK = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
SUITS_IN_DECK = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
class Rank:
def __init__(self, symbol=None):
self.symbol = symbol
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self, color=None):
self.color = color
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.__class__.__name__
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
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}
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
SUITS = {'Spades': Spades, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Clubs': Clubs}
class Card(object):
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __str__(self):
return "{} of {}".format(self._rank, self._suit)
def __repr__(self):
return '<Card ' + str(self._rank) + ' of ' + str(self._suit) + '>'
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def draw(self, index):
card = self.collection[index]
self.collection = self.collection[:index] + self.collection[index + 1:]
return card
def draw_from_top(self):
card = self.collection[-1]
self.collection = self.collection[:-1]
return card
def draw_from_bottom(self):
card = self.collection[0]
self.collection = self.collection[1:]
return card
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
for i in range(len(self.collection)):
if self.collection[i] == card:
return i
raise ValueError('%r is not in list' % (card))
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def __str__(self):
return '[' + ', '.join(repr(card) for card in self.collection) + ']'
def __repr__(self):
return self.__str__()
def StandardDeck():
return create_deck(STANDARD_DECK, SUITS_IN_DECK)
def BeloteDeck():
return create_deck(BELOTE_DECK, SUITS_IN_DECK)
def SixtySixDeck():
return create_deck(SIXTYSIX_DECK, SUITS_IN_DECK)
def create_deck(ranks, suits):
return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in suits
for rank in ranks])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.022s

OK

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

Евгени обнови решението на 22.03.2014 22:08 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol=None):
+ self.symbol = symbol
+
+
+class Suit:
+ def __init__(self, color=None):
+ self.color = color
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Ace'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Jack'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'King'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Six'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Five'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Queen'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Ten'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Three'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Eight'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Four'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Two'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Seven'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Nine'
+
+
+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}
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Spades'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Diamonds'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Hearts'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return 'Clubs'
+
+
+SUITS = {'Spades': Spades, 'Diamonds': Diamonds,
+ 'Hearts': Hearts, 'Clubs': Clubs}
+
+
+class Card(object):
+ def __init__(self, rank, suit):
+ self._rank = rank
+ self._suit = suit
+
+ def __str__(self):
+ return "{} of {}".format(self._rank(), self._suit())
+
+ def __repr__(self):
+ return '<Card ' + str(self._rank()) + ' of ' + str(self._suit()) + '>'
+
+ def __eq__(self, other):
+ return self._rank == other._rank and self._suit == other._suit
+
+ @property
+ def rank(self):
+ return self._rank()
+
+ @property
+ def suit(self):
+ return self._suit()
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def draw(self, index):
+ card = self.collection[index]
+ self.collection = self.collection[:index] + self.collection[index + 1:]
+ return card
+
+ def draw_from_top(self):
+ card = self.collection[-1]
+ self.collection = self.collection[:-1]
+ return card
+
+ def draw_from_bottom(self):
+ card = self.collection[0]
+ self.collection = self.collection[1:]
+ return card
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ for i in range(len(self.collection)):
+ if self.collection[i] == card:
+ return i
+ raise ValueError('%r is not in list' % (card))
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def StandardDeck():
+ deck = CardCollection()
+ for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
+ for rank in ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
+ 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def BeloteDeck():
+ deck = CardCollection()
+ for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
+ for rank in ['Ace', 'Seven', 'Eight', 'Nine',
+ 'Ten', 'Jack', 'Queen', 'King']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def SixtySixDeck():
+ deck = CardCollection()
+ for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
+ for rank in ['Ace', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck

Евгени обнови решението на 23.03.2014 14:35 (преди над 10 години)

class Rank:
- def __init__(self, symbol=None):
- self.symbol = symbol
+ def __init__(self, symbol=None):
+ self.symbol = symbol
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
class Suit:
def __init__(self, color=None):
self.color = color
-
-class Ace(Rank):
- def __init__(self):
- Rank.__init__(self, 'A')
-
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
def __str__(self):
return 'Ace'
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Jack'
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'King'
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Six'
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Five'
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Queen'
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Ten'
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Three'
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Eight'
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Four'
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Two'
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Seven'
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Nine'
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}
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Spades'
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Diamonds'
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Hearts'
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
- def __eq__(self, other):
- if type(other) is type(self):
- return self.__dict__ == other.__dict__
- return False
-
def __str__(self):
return 'Clubs'
SUITS = {'Spades': Spades, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Clubs': Clubs}
class Card(object):
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
def __str__(self):
return "{} of {}".format(self._rank(), self._suit())
def __repr__(self):
return '<Card ' + str(self._rank()) + ' of ' + str(self._suit()) + '>'
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def rank(self):
return self._rank()
@property
def suit(self):
return self._suit()
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def draw(self, index):
card = self.collection[index]
self.collection = self.collection[:index] + self.collection[index + 1:]
return card
def draw_from_top(self):
card = self.collection[-1]
self.collection = self.collection[:-1]
return card
def draw_from_bottom(self):
card = self.collection[0]
self.collection = self.collection[1:]
return card
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
for i in range(len(self.collection)):
if self.collection[i] == card:
return i
raise ValueError('%r is not in list' % (card))
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def StandardDeck():
deck = CardCollection()
for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
for rank in ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck
+ return deck.collection
def BeloteDeck():
deck = CardCollection()
for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
for rank in ['Ace', 'Seven', 'Eight', 'Nine',
'Ten', 'Jack', 'Queen', 'King']:
deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck
+ return deck.collection
def SixtySixDeck():
deck = CardCollection()
for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
for rank in ['Ace', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck
+ return deck.collection

Евгени обнови решението на 23.03.2014 22:38 (преди над 10 години)

class Rank:
def __init__(self, symbol=None):
self.symbol = symbol
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
class Suit:
def __init__(self, color=None):
self.color = color
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
def __str__(self):
return 'Ace'
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
def __str__(self):
return 'Jack'
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
def __str__(self):
return 'King'
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
def __str__(self):
return 'Six'
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
def __str__(self):
return 'Five'
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
def __str__(self):
return 'Queen'
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
def __str__(self):
return 'Ten'
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
def __str__(self):
return 'Three'
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
def __str__(self):
return 'Eight'
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
def __str__(self):
return 'Four'
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
def __str__(self):
return 'Two'
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
def __str__(self):
return 'Seven'
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
def __str__(self):
return 'Nine'
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}
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
def __str__(self):
return 'Spades'
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
def __str__(self):
return 'Diamonds'
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
def __str__(self):
return 'Hearts'
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
def __str__(self):
return 'Clubs'
SUITS = {'Spades': Spades, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Clubs': Clubs}
class Card(object):
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
def __str__(self):
return "{} of {}".format(self._rank(), self._suit())
def __repr__(self):
return '<Card ' + str(self._rank()) + ' of ' + str(self._suit()) + '>'
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def rank(self):
return self._rank()
@property
def suit(self):
return self._suit()
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def draw(self, index):
card = self.collection[index]
self.collection = self.collection[:index] + self.collection[index + 1:]
return card
def draw_from_top(self):
card = self.collection[-1]
self.collection = self.collection[:-1]
return card
def draw_from_bottom(self):
card = self.collection[0]
self.collection = self.collection[1:]
return card
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
for i in range(len(self.collection)):
if self.collection[i] == card:
return i
raise ValueError('%r is not in list' % (card))
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def StandardDeck():
- deck = CardCollection()
+ deck = CardCollection([])
for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
for rank in ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck.collection
def BeloteDeck():
- deck = CardCollection()
+ deck = CardCollection([])
for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
for rank in ['Ace', 'Seven', 'Eight', 'Nine',
'Ten', 'Jack', 'Queen', 'King']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck.collection
def SixtySixDeck():
- deck = CardCollection()
+ deck = CardCollection([])
for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
for rank in ['Ace', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck.collection
+ return deck.collection

Евгени обнови решението на 25.03.2014 00:17 (преди над 10 години)

class Rank:
def __init__(self, symbol=None):
self.symbol = symbol
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
+ def __str__(self):
+ return self.__class__.__name__
+
class Suit:
def __init__(self, color=None):
self.color = color
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
+ def __str__(self):
+ return self.__class__.__name__
+
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
- def __str__(self):
- return 'Ace'
-
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
- def __str__(self):
- return 'Jack'
-
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
- def __str__(self):
- return 'King'
-
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
- def __str__(self):
- return 'Six'
-
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
- def __str__(self):
- return 'Five'
-
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
- def __str__(self):
- return 'Queen'
-
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
- def __str__(self):
- return 'Ten'
-
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
- def __str__(self):
- return 'Three'
-
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
- def __str__(self):
- return 'Eight'
-
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
- def __str__(self):
- return 'Four'
-
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
- def __str__(self):
- return 'Two'
-
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
- def __str__(self):
- return 'Seven'
-
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
- def __str__(self):
- return 'Nine'
-
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}
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
- def __str__(self):
- return 'Spades'
-
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
- def __str__(self):
- return 'Diamonds'
-
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
- def __str__(self):
- return 'Hearts'
-
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
- def __str__(self):
- return 'Clubs'
-
SUITS = {'Spades': Spades, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Clubs': Clubs}
class Card(object):
def __init__(self, rank, suit):
- self._rank = rank
- self._suit = suit
+ self._rank = rank()
+ self._suit = suit()
def __str__(self):
- return "{} of {}".format(self._rank(), self._suit())
+ return "{} of {}".format(self._rank, self._suit)
def __repr__(self):
- return '<Card ' + str(self._rank()) + ' of ' + str(self._suit()) + '>'
+ return '<Card ' + str(self._rank) + ' of ' + str(self._suit) + '>'
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def rank(self):
- return self._rank()
+ return self._rank
@property
def suit(self):
- return self._suit()
+ return self._suit
class CardCollection:
def __init__(self, collection=[]):
- self.collection = collection
+ self.collection = list(collection)
def draw(self, index):
card = self.collection[index]
self.collection = self.collection[:index] + self.collection[index + 1:]
return card
def draw_from_top(self):
card = self.collection[-1]
self.collection = self.collection[:-1]
return card
def draw_from_bottom(self):
card = self.collection[0]
self.collection = self.collection[1:]
return card
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
for i in range(len(self.collection)):
if self.collection[i] == card:
return i
raise ValueError('%r is not in list' % (card))
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def StandardDeck():
deck = CardCollection([])
- for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
- for rank in ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
- 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck.collection
def BeloteDeck():
deck = CardCollection([])
- for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
- for rank in ['Ace', 'Seven', 'Eight', 'Nine',
- 'Ten', 'Jack', 'Queen', 'King']:
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck.collection
def SixtySixDeck():
deck = CardCollection([])
- for suit in ['Spades', 'Hearts', 'Diamonds', 'Clubs']:
- for rank in ['Ace', 'Nine', 'Ten', 'Jack', 'Queen', 'King']:
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck.collection

Евгени обнови решението на 25.03.2014 20:41 (преди над 10 години)

+STANDARD_DECK = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+BELOTE_DECK = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']
+
+SIXTYSIX_DECK = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+
+SUITS_IN_DECK = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
class Rank:
def __init__(self, symbol=None):
self.symbol = symbol
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self, color=None):
self.color = color
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.__class__.__name__
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
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}
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
SUITS = {'Spades': Spades, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Clubs': Clubs}
class Card(object):
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __str__(self):
return "{} of {}".format(self._rank, self._suit)
def __repr__(self):
return '<Card ' + str(self._rank) + ' of ' + str(self._suit) + '>'
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def draw(self, index):
card = self.collection[index]
self.collection = self.collection[:index] + self.collection[index + 1:]
return card
def draw_from_top(self):
card = self.collection[-1]
self.collection = self.collection[:-1]
return card
def draw_from_bottom(self):
card = self.collection[0]
self.collection = self.collection[1:]
return card
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
for i in range(len(self.collection)):
if self.collection[i] == card:
return i
raise ValueError('%r is not in list' % (card))
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
+ def __str__(self):
+ return '[' + ', '.join(repr(card) for card in self.collection) + ']'
+ def __repr__(self):
+ return self.__str__()
+
+
def StandardDeck():
- deck = CardCollection([])
- for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
- for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
- 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
- deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck.collection
+ return create_deck(STANDARD_DECK, SUITS_IN_DECK)
def BeloteDeck():
- deck = CardCollection([])
- for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
- for rank in ['King', 'Queen', 'Jack', 'Ten',
- 'Nine', 'Eight', 'Seven', 'Ace']:
- deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck.collection
+ return create_deck(BELOTE_DECK, SUITS_IN_DECK)
def SixtySixDeck():
- deck = CardCollection([])
- for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ return create_deck(SIXTYSIX_DECK, SUITS_IN_DECK)
- for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']:
+
- deck.add(Card(RANKS[rank], SUITS[suit]))
+
- return deck.collection
+def create_deck(ranks, suits):
+ return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in suits
+ for rank in ranks])