Решение на Тесте карти от Иван Георгиев

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

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

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __init__(self, rank, symbol):
self._rank = rank
self.symbol = symbol
def __eq__(self, other):
return self._rank == other._rank
def __str__(self):
return str(self._rank)
class Suit:
def __init__(self, suit, color):
self._suit = suit
self.color = color
def __eq__(self, other):
return self._suit == other._suit
def __str__(self):
return str(self._suit)
class Two(Rank):
def __init__(self):
return Rank.__init__(self, 'Two', '2')
class Three(Rank):
def __init__(self):
return Rank.__init__(self, 'Three', '3')
class Four(Rank):
def __init__(self):
return Rank.__init__(self, 'Four', '4')
class Five(Rank):
def __init__(self):
return Rank.__init__(self, 'Five', '5')
class Six(Rank):
def __init__(self):
return Rank.__init__(self, 'Six', '6')
class Seven(Rank):
def __init__(self):
return Rank.__init__(self, 'Seven', '7')
class Eight(Rank):
def __init__(self):
return Rank.__init__(self, 'Eight', '8')
class Nine(Rank):
def __init__(self):
return Rank.__init__(self, 'Nine', '9')
class Ten(Rank):
def __init__(self):
return Rank.__init__(self, 'Ten', '10')
class Jack(Rank):
def __init__(self):
return Rank.__init__(self, 'Jack', 'J')
class Queen(Rank):
def __init__(self):
return Rank.__init__(self, 'Queen', 'Q')
class King(Rank):
def __init__(self):
return Rank.__init__(self, 'King', 'K')
class Ace(Rank):
def __init__(self):
return Rank.__init__(self, 'Ace', 'A')
class Spades(Suit):
def __init__(self):
return Suit.__init__(self, 'Spades', 'black')
class Hearts(Suit):
def __init__(self):
return Suit.__init__(self, 'Hearts', 'red')
class Clubs(Suit):
def __init__(self):
return Suit.__init__(self, 'Clubs', 'black')
class Diamonds(Suit):
def __init__(self):
return Suit.__init__(self, 'Diamonds', 'red')
RANKS = OrderedDict([('King', type(King())), ('Queen', type(Queen())),
('Jack', type(Jack())), ('Ten', type(Ten())),
('Nine', type(Nine())), ('Eight', type(Eight())),
('Seven', type(Seven())), ('Six', type(Six())),
('Five', type(Five())), ('Four', type(Four())),
('Three', type(Three())), ('Two', type(Two())),
('Ace', type(Ace()))])
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __eq__(self, other):
return [self._rank, self._suit] == [other._rank, other._suit]
def __str__(self):
return '{0} of {1}'.format(str(self._rank), str(self._suit))
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def belote_card(self):
if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9', '8', '7'):
return True
return False
def sixty_six_card(self):
if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9'):
return True
return False
class CardCollection:
def __init__(self, collection=[]):
self._deck = list(collection)
def __getitem__(self, index):
return self._deck[index]
def __len__(self):
return len(self._deck)
def __str__(self):
return str(self._deck)
def add(self, card):
self._deck.append(card)
def draw(self, index):
return self._deck.pop(index)
def draw_from_top(self):
return self._deck.pop(-1)
def draw_from_bottom(self):
return self._deck.pop(0)
def top_card(self):
return self._deck[-1]
def bottom_card(self):
return self._deck[0]
def index(self, card):
return self._deck.index(card)
def StandardDeck():
return CardCollection([Card(rank, suit) for suit
in SUITS.values() for rank
in RANKS.values()])
def BeloteDeck():
deck = StandardDeck()
return CardCollection([card for card in deck._deck
if card.belote_card()])
def SixtySixDeck():
deck = StandardDeck()
return CardCollection([card for card in deck._deck
if card.sixty_six_card()])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.027s

OK

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

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

+from collections import OrderedDict
+
+
+SYMBOLS = OrderedDict([('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
+ ('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
+ ('Seven', '7'), ('Six', '6'), ('Five', '5'),
+ ('Four', '4'), ('Three', '3'), ('Two', '2'),
+ ('Ace', 'A')])
+
+
+COLORS = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
+ ('Hearts', 'red'), ('Spades', 'black')])
+
+
+class Rank:
+ def __init__(self, rank_of_card):
+ self.rank_of_card = rank_of_card
+ self.symbol = SYMBOLS[rank_of_card]
+
+ def __eq__(self, other):
+ return self.rank_of_card == other.rank_of_card
+
+ def __str__(self):
+ return str(self.rank_of_card)
+
+
+class Suit:
+ def __init__(self, suit_of_card):
+ self.suit_of_card = suit_of_card
+ self.color = COLORS[suit_of_card]
+
+ def __eq__(self, other):
+ return self.suit_of_card == other.suit_of_card
+
+ def __str__(self):
+ return str(self.suit_of_card)
+
+
+class Two(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Two')
+
+
+class Three(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Three')
+
+
+class Four(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Four')
+
+
+class Five(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Five')
+
+
+class Six(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Six')
+
+
+class Seven(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Seven')
+
+
+class Eight(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Eight')
+
+
+class Nine(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Nine')
+
+
+class Ten(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Ten')
+
+
+class Jack(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Jack')
+
+
+class Queen(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Queen')
+
+
+class King(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'King')
+
+
+class Ace(Rank):
+ def __init__(self):
+ return Rank.__init__(self, 'Ace')
+
+
+class Spades(Suit):
+ def __init__(self):
+ return Suit.__init__(self, 'Spades')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ return Suit.__init__(self, 'Hearts')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ return Suit.__init__(self, 'Clubs')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ return Suit.__init__(self, 'Diamonds')
+
+
+RANKS = OrderedDict([('King', type(King())), ('Queen', type(Queen())),
+ ('Jack', type(Jack())), ('Ten', type(Ten())),
+ ('Nine', type(Nine())), ('Eight', type(Eight())),
+ ('Seven', type(Seven())), ('Six', type(Six())),
+ ('Five', type(Five())), ('Four', type(Four())),
+ ('Three', type(Three())), ('Two', type(Two())),
+ ('Ace', type(Ace()))])
+
+
+SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
+ ('Hearts', type(Hearts())), ('Spades', type(Spades()))])
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return [self.rank, self.suit] == [other.rank, other.suit]
+
+ def __str__(self):
+ return '{0} of {1}'.format(str(self.rank), str(self.suit))
+
+
+class CardCollection:
+ def __init__(self, collection):
+ self.deck = list(collection)
+
+ def __getitem__(self, index):
+ return self.deck[index]
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __str__(self):
+ return str(self.deck)
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def draw(self, index):
+ return self.deck.pop(index)
+
+ def draw_from_top(self):
+ return self.deck.pop(-1)
+
+ def draw_from_bottom(self):
+ return self.deck.pop(0)
+
+ def top_card(self):
+ return self.deck[-1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def index(self, card):
+ return self.deck.index(card)
+
+
+def StandardDeck():
+ return CardCollection([str(Card(rank, suit)) for suit in SUITS.values()
+ for rank in RANKS.values()])
+
+
+def BeloteDeck():
+ deck = StandardDeck()
+ first_half = deck.deck[:7] + deck.deck[12:20] + deck.deck[25:33]
+ second_half = deck.deck[38:46] + deck.deck[51:52]
+ return CardCollection(first_half + second_half)
+
+
+def SixtySixDeck():
+ deck = StandardDeck()
+ first_half = deck.deck[:5] + deck.deck[12:18] + deck[25:31]
+ second_half = deck.deck[38:44] + deck.deck[51:52]
+ return CardCollection(first_half + second_half)

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

from collections import OrderedDict
SYMBOLS = OrderedDict([('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
('Seven', '7'), ('Six', '6'), ('Five', '5'),
('Four', '4'), ('Three', '3'), ('Two', '2'),
('Ace', 'A')])
COLORS = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
('Hearts', 'red'), ('Spades', 'black')])
class Rank:
def __init__(self, rank_of_card):
self.rank_of_card = rank_of_card
self.symbol = SYMBOLS[rank_of_card]
def __eq__(self, other):
return self.rank_of_card == other.rank_of_card
def __str__(self):
return str(self.rank_of_card)
class Suit:
def __init__(self, suit_of_card):
self.suit_of_card = suit_of_card
self.color = COLORS[suit_of_card]
def __eq__(self, other):
return self.suit_of_card == other.suit_of_card
def __str__(self):
return str(self.suit_of_card)
class Two(Rank):
def __init__(self):
return Rank.__init__(self, 'Two')
class Three(Rank):
def __init__(self):
return Rank.__init__(self, 'Three')
class Four(Rank):
def __init__(self):
return Rank.__init__(self, 'Four')
class Five(Rank):
def __init__(self):
return Rank.__init__(self, 'Five')
class Six(Rank):
def __init__(self):
return Rank.__init__(self, 'Six')
class Seven(Rank):
def __init__(self):
return Rank.__init__(self, 'Seven')
class Eight(Rank):
def __init__(self):
return Rank.__init__(self, 'Eight')
class Nine(Rank):
def __init__(self):
return Rank.__init__(self, 'Nine')
class Ten(Rank):
def __init__(self):
return Rank.__init__(self, 'Ten')
class Jack(Rank):
def __init__(self):
return Rank.__init__(self, 'Jack')
class Queen(Rank):
def __init__(self):
return Rank.__init__(self, 'Queen')
class King(Rank):
def __init__(self):
return Rank.__init__(self, 'King')
class Ace(Rank):
def __init__(self):
return Rank.__init__(self, 'Ace')
class Spades(Suit):
def __init__(self):
return Suit.__init__(self, 'Spades')
class Hearts(Suit):
def __init__(self):
return Suit.__init__(self, 'Hearts')
class Clubs(Suit):
def __init__(self):
return Suit.__init__(self, 'Clubs')
class Diamonds(Suit):
def __init__(self):
return Suit.__init__(self, 'Diamonds')
RANKS = OrderedDict([('King', type(King())), ('Queen', type(Queen())),
('Jack', type(Jack())), ('Ten', type(Ten())),
('Nine', type(Nine())), ('Eight', type(Eight())),
('Seven', type(Seven())), ('Six', type(Six())),
('Five', type(Five())), ('Four', type(Four())),
('Three', type(Three())), ('Two', type(Two())),
('Ace', type(Ace()))])
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
class Card:
def __init__(self, rank, suit):
- self.rank = rank()
- self.suit = suit()
+ self.__rank = rank()
+ self.__suit = suit()
def __eq__(self, other):
- return [self.rank, self.suit] == [other.rank, other.suit]
+ return [self.__rank, self.__suit] == [other.__rank, other.__suit]
def __str__(self):
- return '{0} of {1}'.format(str(self.rank), str(self.suit))
+ return '{0} of {1}'.format(str(self.__rank), str(self.__suit))
+ @property
+ def rank(self):
+ return self.__rank
+ @property
+ def suit(self):
+ return self.__suit
+
+ def belote_card(self):
+ if self.__rank.symbol in ('A', 'K', 'Q', 'J', '10', '9', '8', '7'):
+ return True
+ return False
+
+ def sixty_six_card(self):
+ if self.__rank.symbol in ('A', 'K', 'Q', 'J', '10', '9'):
+ return True
+ return False
+
+
class CardCollection:
- def __init__(self, collection):
+ def __init__(self, collection=[]):
self.deck = list(collection)
def __getitem__(self, index):
return self.deck[index]
def __len__(self):
return len(self.deck)
def __str__(self):
return str(self.deck)
def add(self, card):
self.deck.append(card)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop(-1)
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def index(self, card):
return self.deck.index(card)
-def StandardDeck():
- return CardCollection([str(Card(rank, suit)) for suit in SUITS.values()
- for rank in RANKS.values()])
+class StandardDeck(CardCollection):
+ def __init__(self):
+ return CardCollection.__init__(self, [Card(rank, suit) for suit
+ in SUITS.values() for rank
+ in RANKS.values()])
-def BeloteDeck():
- deck = StandardDeck()
- first_half = deck.deck[:7] + deck.deck[12:20] + deck.deck[25:33]
- second_half = deck.deck[38:46] + deck.deck[51:52]
- return CardCollection(first_half + second_half)
+class BeloteDeck(CardCollection):
+ def __init__(self):
+ deck = StandardDeck()
+ return CardCollection.__init__(self, [card for card in deck.deck
+ if card.belote_card()])
-def SixtySixDeck():
- deck = StandardDeck()
+class SixtySixDeck(CardCollection):
- first_half = deck.deck[:5] + deck.deck[12:18] + deck[25:31]
+ def __init__(self):
- second_half = deck.deck[38:44] + deck.deck[51:52]
+ deck = StandardDeck()
- return CardCollection(first_half + second_half)
+ return CardCollection.__init__(self, [card for card in deck.deck
+ if card.sixty_six_card()])

Иван обнови решението на 24.03.2014 23:45 (преди над 10 години)

from collections import OrderedDict
-SYMBOLS = OrderedDict([('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
- ('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
- ('Seven', '7'), ('Six', '6'), ('Five', '5'),
- ('Four', '4'), ('Three', '3'), ('Two', '2'),
- ('Ace', 'A')])
-
-
-COLORS = OrderedDict([('Diamonds', 'red'), ('Clubs', 'black'),
- ('Hearts', 'red'), ('Spades', 'black')])
-
-
class Rank:
- def __init__(self, rank_of_card):
- self.rank_of_card = rank_of_card
- self.symbol = SYMBOLS[rank_of_card]
+ def __init__(self, rank, symbol):
+ self._rank = rank
+ self.symbol = symbol
def __eq__(self, other):
- return self.rank_of_card == other.rank_of_card
+ return self._rank == other._rank
def __str__(self):
- return str(self.rank_of_card)
+ return str(self._rank)
class Suit:
- def __init__(self, suit_of_card):
- self.suit_of_card = suit_of_card
- self.color = COLORS[suit_of_card]
+ def __init__(self, suit, color):
+ self._suit = suit
+ self.color = color
def __eq__(self, other):
- return self.suit_of_card == other.suit_of_card
+ return self._suit == other._suit
def __str__(self):
- return str(self.suit_of_card)
+ return str(self._suit)
class Two(Rank):
def __init__(self):
- return Rank.__init__(self, 'Two')
+ return Rank.__init__(self, 'Two', '2')
class Three(Rank):
def __init__(self):
- return Rank.__init__(self, 'Three')
+ return Rank.__init__(self, 'Three', '3')
class Four(Rank):
def __init__(self):
- return Rank.__init__(self, 'Four')
+ return Rank.__init__(self, 'Four', '4')
class Five(Rank):
def __init__(self):
- return Rank.__init__(self, 'Five')
+ return Rank.__init__(self, 'Five', '5')
class Six(Rank):
def __init__(self):
- return Rank.__init__(self, 'Six')
+ return Rank.__init__(self, 'Six', '6')
class Seven(Rank):
def __init__(self):
- return Rank.__init__(self, 'Seven')
+ return Rank.__init__(self, 'Seven', '7')
class Eight(Rank):
def __init__(self):
- return Rank.__init__(self, 'Eight')
+ return Rank.__init__(self, 'Eight', '8')
class Nine(Rank):
def __init__(self):
- return Rank.__init__(self, 'Nine')
+ return Rank.__init__(self, 'Nine', '9')
class Ten(Rank):
def __init__(self):
- return Rank.__init__(self, 'Ten')
+ return Rank.__init__(self, 'Ten', '10')
class Jack(Rank):
def __init__(self):
- return Rank.__init__(self, 'Jack')
+ return Rank.__init__(self, 'Jack', 'J')
class Queen(Rank):
def __init__(self):
- return Rank.__init__(self, 'Queen')
+ return Rank.__init__(self, 'Queen', 'Q')
class King(Rank):
def __init__(self):
- return Rank.__init__(self, 'King')
+ return Rank.__init__(self, 'King', 'K')
class Ace(Rank):
def __init__(self):
- return Rank.__init__(self, 'Ace')
+ return Rank.__init__(self, 'Ace', 'A')
class Spades(Suit):
def __init__(self):
- return Suit.__init__(self, 'Spades')
+ return Suit.__init__(self, 'Spades', 'black')
class Hearts(Suit):
def __init__(self):
- return Suit.__init__(self, 'Hearts')
+ return Suit.__init__(self, 'Hearts', 'red')
class Clubs(Suit):
def __init__(self):
- return Suit.__init__(self, 'Clubs')
+ return Suit.__init__(self, 'Clubs', 'black')
class Diamonds(Suit):
def __init__(self):
- return Suit.__init__(self, 'Diamonds')
+ return Suit.__init__(self, 'Diamonds', 'red')
RANKS = OrderedDict([('King', type(King())), ('Queen', type(Queen())),
('Jack', type(Jack())), ('Ten', type(Ten())),
('Nine', type(Nine())), ('Eight', type(Eight())),
('Seven', type(Seven())), ('Six', type(Six())),
('Five', type(Five())), ('Four', type(Four())),
('Three', type(Three())), ('Two', type(Two())),
('Ace', type(Ace()))])
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
class Card:
def __init__(self, rank, suit):
- self.__rank = rank()
- self.__suit = suit()
+ self._rank = rank()
+ self._suit = suit()
def __eq__(self, other):
- return [self.__rank, self.__suit] == [other.__rank, other.__suit]
+ return [self._rank, self._suit] == [other._rank, other._suit]
def __str__(self):
- return '{0} of {1}'.format(str(self.__rank), str(self.__suit))
+ return '{0} of {1}'.format(str(self._rank), str(self._suit))
@property
def rank(self):
- return self.__rank
+ return self._rank
@property
def suit(self):
- return self.__suit
+ return self._suit
def belote_card(self):
- if self.__rank.symbol in ('A', 'K', 'Q', 'J', '10', '9', '8', '7'):
+ if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9', '8', '7'):
return True
return False
def sixty_six_card(self):
- if self.__rank.symbol in ('A', 'K', 'Q', 'J', '10', '9'):
+ if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9'):
return True
return False
class CardCollection:
def __init__(self, collection=[]):
- self.deck = list(collection)
+ self._deck = list(collection)
def __getitem__(self, index):
- return self.deck[index]
+ return self._deck[index]
def __len__(self):
- return len(self.deck)
+ return len(self._deck)
def __str__(self):
- return str(self.deck)
+ return str(self._deck)
def add(self, card):
- self.deck.append(card)
+ self._deck.append(card)
def draw(self, index):
- return self.deck.pop(index)
+ return self._deck.pop(index)
def draw_from_top(self):
- return self.deck.pop(-1)
+ return self._deck.pop(-1)
def draw_from_bottom(self):
- return self.deck.pop(0)
+ return self._deck.pop(0)
def top_card(self):
- return self.deck[-1]
+ return self._deck[-1]
def bottom_card(self):
- return self.deck[0]
+ return self._deck[0]
def index(self, card):
- return self.deck.index(card)
+ return self._deck.index(card)
-class StandardDeck(CardCollection):
- def __init__(self):
- return CardCollection.__init__(self, [Card(rank, suit) for suit
- in SUITS.values() for rank
- in RANKS.values()])
+def StandardDeck():
+ return CardCollection([Card(rank, suit) for suit
+ in SUITS.values() for rank
+ in RANKS.values()])
-class BeloteDeck(CardCollection):
- def __init__(self):
- deck = StandardDeck()
- return CardCollection.__init__(self, [card for card in deck.deck
- if card.belote_card()])
+def BeloteDeck():
+ deck = StandardDeck()
+ return CardCollection([card for card in deck._deck
+ if card.belote_card()])
-class SixtySixDeck(CardCollection):
- def __init__(self):
+def SixtySixDeck():
- deck = StandardDeck()
+ deck = StandardDeck()
- return CardCollection.__init__(self, [card for card in deck.deck
+ return CardCollection([card for card in deck._deck
- if card.sixty_six_card()])
+ if card.sixty_six_card()])

Иван обнови решението на 26.03.2014 16:23 (преди над 10 години)

from collections import OrderedDict
class Rank:
def __init__(self, rank, symbol):
self._rank = rank
self.symbol = symbol
def __eq__(self, other):
return self._rank == other._rank
def __str__(self):
return str(self._rank)
class Suit:
def __init__(self, suit, color):
self._suit = suit
self.color = color
def __eq__(self, other):
return self._suit == other._suit
def __str__(self):
return str(self._suit)
class Two(Rank):
def __init__(self):
return Rank.__init__(self, 'Two', '2')
class Three(Rank):
def __init__(self):
return Rank.__init__(self, 'Three', '3')
class Four(Rank):
def __init__(self):
return Rank.__init__(self, 'Four', '4')
class Five(Rank):
def __init__(self):
return Rank.__init__(self, 'Five', '5')
class Six(Rank):
def __init__(self):
return Rank.__init__(self, 'Six', '6')
class Seven(Rank):
def __init__(self):
return Rank.__init__(self, 'Seven', '7')
class Eight(Rank):
def __init__(self):
return Rank.__init__(self, 'Eight', '8')
class Nine(Rank):
def __init__(self):
return Rank.__init__(self, 'Nine', '9')
class Ten(Rank):
def __init__(self):
return Rank.__init__(self, 'Ten', '10')
class Jack(Rank):
def __init__(self):
return Rank.__init__(self, 'Jack', 'J')
class Queen(Rank):
def __init__(self):
return Rank.__init__(self, 'Queen', 'Q')
class King(Rank):
def __init__(self):
return Rank.__init__(self, 'King', 'K')
class Ace(Rank):
def __init__(self):
return Rank.__init__(self, 'Ace', 'A')
class Spades(Suit):
def __init__(self):
return Suit.__init__(self, 'Spades', 'black')
class Hearts(Suit):
def __init__(self):
return Suit.__init__(self, 'Hearts', 'red')
class Clubs(Suit):
def __init__(self):
return Suit.__init__(self, 'Clubs', 'black')
class Diamonds(Suit):
def __init__(self):
return Suit.__init__(self, 'Diamonds', 'red')
RANKS = OrderedDict([('King', type(King())), ('Queen', type(Queen())),
('Jack', type(Jack())), ('Ten', type(Ten())),
('Nine', type(Nine())), ('Eight', type(Eight())),
('Seven', type(Seven())), ('Six', type(Six())),
('Five', type(Five())), ('Four', type(Four())),
('Three', type(Three())), ('Two', type(Two())),
('Ace', type(Ace()))])
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __eq__(self, other):
return [self._rank, self._suit] == [other._rank, other._suit]
def __str__(self):
return '{0} of {1}'.format(str(self._rank), str(self._suit))
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def belote_card(self):
if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9', '8', '7'):
return True
return False
def sixty_six_card(self):
if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9'):
return True
return False
class CardCollection:
def __init__(self, collection=[]):
self._deck = list(collection)
def __getitem__(self, index):
return self._deck[index]
+ def __setitem__(self, index, value):
+ self._deck[index] = value
+ pass
+
def __len__(self):
return len(self._deck)
def __str__(self):
return str(self._deck)
def add(self, card):
self._deck.append(card)
def draw(self, index):
return self._deck.pop(index)
def draw_from_top(self):
return self._deck.pop(-1)
def draw_from_bottom(self):
return self._deck.pop(0)
def top_card(self):
return self._deck[-1]
def bottom_card(self):
return self._deck[0]
def index(self, card):
return self._deck.index(card)
def StandardDeck():
return CardCollection([Card(rank, suit) for suit
in SUITS.values() for rank
in RANKS.values()])
def BeloteDeck():
deck = StandardDeck()
return CardCollection([card for card in deck._deck
if card.belote_card()])
def SixtySixDeck():
deck = StandardDeck()
return CardCollection([card for card in deck._deck
if card.sixty_six_card()])

Иван обнови решението на 26.03.2014 16:40 (преди над 10 години)

from collections import OrderedDict
class Rank:
def __init__(self, rank, symbol):
self._rank = rank
self.symbol = symbol
def __eq__(self, other):
return self._rank == other._rank
def __str__(self):
return str(self._rank)
class Suit:
def __init__(self, suit, color):
self._suit = suit
self.color = color
def __eq__(self, other):
return self._suit == other._suit
def __str__(self):
return str(self._suit)
class Two(Rank):
def __init__(self):
return Rank.__init__(self, 'Two', '2')
class Three(Rank):
def __init__(self):
return Rank.__init__(self, 'Three', '3')
class Four(Rank):
def __init__(self):
return Rank.__init__(self, 'Four', '4')
class Five(Rank):
def __init__(self):
return Rank.__init__(self, 'Five', '5')
class Six(Rank):
def __init__(self):
return Rank.__init__(self, 'Six', '6')
class Seven(Rank):
def __init__(self):
return Rank.__init__(self, 'Seven', '7')
class Eight(Rank):
def __init__(self):
return Rank.__init__(self, 'Eight', '8')
class Nine(Rank):
def __init__(self):
return Rank.__init__(self, 'Nine', '9')
class Ten(Rank):
def __init__(self):
return Rank.__init__(self, 'Ten', '10')
class Jack(Rank):
def __init__(self):
return Rank.__init__(self, 'Jack', 'J')
class Queen(Rank):
def __init__(self):
return Rank.__init__(self, 'Queen', 'Q')
class King(Rank):
def __init__(self):
return Rank.__init__(self, 'King', 'K')
class Ace(Rank):
def __init__(self):
return Rank.__init__(self, 'Ace', 'A')
class Spades(Suit):
def __init__(self):
return Suit.__init__(self, 'Spades', 'black')
class Hearts(Suit):
def __init__(self):
return Suit.__init__(self, 'Hearts', 'red')
class Clubs(Suit):
def __init__(self):
return Suit.__init__(self, 'Clubs', 'black')
class Diamonds(Suit):
def __init__(self):
return Suit.__init__(self, 'Diamonds', 'red')
RANKS = OrderedDict([('King', type(King())), ('Queen', type(Queen())),
('Jack', type(Jack())), ('Ten', type(Ten())),
('Nine', type(Nine())), ('Eight', type(Eight())),
('Seven', type(Seven())), ('Six', type(Six())),
('Five', type(Five())), ('Four', type(Four())),
('Three', type(Three())), ('Two', type(Two())),
('Ace', type(Ace()))])
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
def __eq__(self, other):
return [self._rank, self._suit] == [other._rank, other._suit]
def __str__(self):
return '{0} of {1}'.format(str(self._rank), str(self._suit))
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def belote_card(self):
if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9', '8', '7'):
return True
return False
def sixty_six_card(self):
if self._rank.symbol in ('A', 'K', 'Q', 'J', '10', '9'):
return True
return False
class CardCollection:
def __init__(self, collection=[]):
self._deck = list(collection)
def __getitem__(self, index):
return self._deck[index]
- def __setitem__(self, index, value):
- self._deck[index] = value
- pass
-
def __len__(self):
return len(self._deck)
def __str__(self):
return str(self._deck)
def add(self, card):
self._deck.append(card)
def draw(self, index):
return self._deck.pop(index)
def draw_from_top(self):
return self._deck.pop(-1)
def draw_from_bottom(self):
return self._deck.pop(0)
def top_card(self):
return self._deck[-1]
def bottom_card(self):
return self._deck[0]
def index(self, card):
return self._deck.index(card)
def StandardDeck():
return CardCollection([Card(rank, suit) for suit
in SUITS.values() for rank
in RANKS.values()])
def BeloteDeck():
deck = StandardDeck()
return CardCollection([card for card in deck._deck
if card.belote_card()])
def SixtySixDeck():
deck = StandardDeck()
return CardCollection([card for card in deck._deck
if card.sixty_six_card()])