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

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

Към профила на Таня Христова

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __init__(self, symbol=None):
self.symbol = symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.symbol == other.symbol
def __setattr__(self, attribute, value):
if attribute is None:
object.__setattr__(self, attribute, value)
King = type("King", (Rank,), {'symbol': 'K'})
Queen = type("Queen", (Rank,), {'symbol': 'Q'})
Jack = type("Jack", (Rank,), {'symbol': 'J'})
Ace = type("Ace", (Rank,), {'symbol': 'A'})
Ten = type("Ten", (Rank,), {'symbol': '10'})
Nine = type("Nine", (Rank,), {'symbol': '9'})
Eight = type("Eight", (Rank,), {'symbol': '8'})
Seven = type("Seven", (Rank,), {'symbol': '7'})
Six = type("Six", (Rank,), {'symbol': '6'})
Five = type("Five", (Rank,), {'symbol': '5'})
Four = type("Four", (Rank,), {'symbol': '4'})
Three = type("Three", (Rank,), {'symbol': '3'})
Two = type("Two", (Rank,), {'symbol': '2'})
class Suit:
def __init__(self, color=None):
self.color = color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return (self.color == other.color) and (str(self) == str(other))
def __setattr__(self, attribute, value):
if attribute is None:
object.__setattr__(self, attribute, value)
Hearts = type("Hearts", (Suit,), {'color': 'red'})
Clubs = type("Clubs", (Suit,), {'color': 'red'})
Spades = type("Spades", (Suit,), {'color': 'black'})
Diamonds = type("Diamonds", (Suit,), {'color': 'black'})
class Card:
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
def __repr__(self):
return '<{0} {1}>'.format(self.__class__.__name__, str(self))
def __str__(self):
return '{0} of {1}'.format(self.rank, self.suit)
@property
def rank(self):
return self._rank()
@rank.setter
def rank(self, value):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def suit(self):
return self._suit()
@suit.setter
def suit(self, value):
raise AttributeError("can't set attribute")
class CardCollection:
def __init__(self, collection=None):
self.collection = collection
if self.collection is None:
self.collection = []
def __str__(self):
return str([str(card) for card in self.collection])
def __len__(self):
return len(self.collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
drawn_card = self.collection[index]
del self.collection[index]
return drawn_card
def draw_from_top(self):
drawn_card = self.collection[len(self) - 1]
del self.collection[len(self) - 1]
return drawn_card
def draw_from_bottom(self):
drawn_card = self.collection[0]
del self.collection[0]
return drawn_card
def top_card(self):
return self.collection[len(self) - 1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
cards_in_deck = [str(card) for card in self.collection]
return cards_in_deck.index(str(card))
RANKS = OrderedDict([('King', King), ('Queen', Queen), ('Jack', Jack), ('Ten', Ten), ('Nine', Nine), ('Eight', Eight),
('Seven', Seven), ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three), ('Two', Two), ('Ace', Ace)])
SUITS = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs), ('Hearts', Hearts), ('Spades', Spades)])
def StandardDeck():
standard_deck = []
for suit in SUITS:
for rank in RANKS:
standard_deck.append(Card(RANKS[rank], SUITS[suit]))
return standard_deck
def BeloteDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
belote_deck = []
for suit in SUITS:
for rank in ordered_ranks:
belote_deck.append(Card(RANKS[rank], SUITS[suit]))
return belote_deck
def SixtySixDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
sixty_six_deck = []
for suit in ordered_suits:
for rank in ordered_ranks:
sixty_six_deck.append(Card(RANKS[rank], SUITS[suit]))
return sixty_six_deck

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.021s

OK

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

Таня обнови решението на 24.03.2014 12:59 (преди около 10 години)

+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class King(Rank):
+ def __init__(self):
+ self.symbol = 'K'
+
+
+class Queen(Rank):
+ def __init__(self):
+ self.symbol = 'Q'
+
+
+class Jack(Rank):
+ def __init__(self):
+ self.symbol = 'J'
+
+
+class Ace(Rank):
+ def __init__(self):
+ self.symbol = 'A'
+
+
+class Ten(Rank):
+ def __init__(self):
+ self.symbol = '10'
+
+
+class Nine(Rank):
+ def __init__(self):
+ self.symbol = '9'
+
+
+class Eight(Rank):
+ def __init__(self):
+ self.symbol = '8'
+
+
+class Seven(Rank):
+ def __init__(self):
+ self.symbol = '7'
+
+
+class Six(Rank):
+ def __init__(self):
+ self.symbol = '6'
+
+
+class Five(Rank):
+ def __init__(self):
+ self.symbol = '5'
+
+
+class Four(Rank):
+ def __init__(self):
+ self.symbol = '4'
+
+
+class Three(Rank):
+ def __init__(self):
+ self.symbol = '3'
+
+
+class Two(Rank):
+ def __init__(self):
+ self.symbol = '2'
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return (self.color == other.color) and (str(self) == str(other))
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank
+ self._suit = suit
+
+ def __repr__(self):
+ return '<{0} {1}>'.format(self.__class__.__name__, str(self))
+
+ def __str__(self):
+ return '{0} of {1}'.format(self.rank, self.suit)
+
+ @property
+ def rank(self):
+ return self._rank()
+
+ @rank.setter
+ def rank(self, value):
+ raise AttributeError("can't set attribute")
+
+ def __eq__(self, other):
+ return self._rank == other._rank and self._suit == other._suit
+
+ @property
+ def suit(self):
+ return self._suit()
+
+ @suit.setter
+ def suit(self, value):
+ raise AttributeError("can't set attribute")
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __iter__(self):
+ for card in self.collection:
+ yield card
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def draw(self, index):
+ drawn_card = self.collection[index]
+ del self.collection[index]
+ return drawn_card
+
+ def draw_from_top(self):
+ drawn_card = self.collection[len(self) - 1]
+ del self.collection[len(self) - 1]
+ return drawn_card
+
+ def draw_from_bottom(self):
+ drawn_card = self.collection[0]
+ del self.collection[0]
+ return drawn_card
+
+ def top_card(self):
+ return self.collection[len(self) - 1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+
+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}
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Spades': Spades, 'Diamonds': Diamonds}
+
+
+def StandardDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Six', 'Five', 'Four',
+ 'Three', 'Two', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ standard_deck = []
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ standard_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return standard_deck
+
+
+def BeloteDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ belote_deck = []
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ belote_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ sixty_six_deck = []
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ sixty_six_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return sixty_six_deck

На подкласовете може би е по-добра идея да сетваш атрибутите color и symbol като атрибути на класа, не на инстансите.

Помисли за вариант динамично да създадеш всичките под-класове на Suit и Rank.

collection=[] - в Python не е добра идея да използваш mutable обект за аргумент по подразбиране. В този конкретен случай, няма да ти попречи, но не е добра практика.

2 теста не ти минават - единия е свързан с index метода, другия с bottom_card. Разгледай всички възможни сценарии.

Таня обнови решението на 25.03.2014 21:14 (преди около 10 години)

+from collections import OrderedDict
+
+
class Rank:
- def __init__(self, symbol):
- self.symbol = symbol
+ def __init__(self):
+ self.symbol = self.__class__.__name__[0]
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.symbol == other.symbol
+King = type("King", (Rank,), {})
+Queen = type("Queen", (Rank,), {})
+Jack = type("Jack", (Rank,), {})
+Ace = type("Ace", (Rank,), {})
+Ten = type("Ten", (Rank,), {})
+Nine = type("Nine", (Rank,), {})
+Eight = type("Eight", (Rank,), {})
+Seven = type("Seven", (Rank,), {})
+Six = type("Six", (Rank,), {})
+Five = type("Five", (Rank,), {})
+Four = type("Four", (Rank,), {})
+Three = type("Three", (Rank,), {})
+Two = type("Two", (Rank,), {})
-class King(Rank):
- def __init__(self):
- self.symbol = 'K'
-
-class Queen(Rank):
+class Suit:
def __init__(self):
- self.symbol = 'Q'
+ pass
-
-class Jack(Rank):
- def __init__(self):
- self.symbol = 'J'
-
-
-class Ace(Rank):
- def __init__(self):
- self.symbol = 'A'
-
-
-class Ten(Rank):
- def __init__(self):
- self.symbol = '10'
-
-
-class Nine(Rank):
- def __init__(self):
- self.symbol = '9'
-
-
-class Eight(Rank):
- def __init__(self):
- self.symbol = '8'
-
-
-class Seven(Rank):
- def __init__(self):
- self.symbol = '7'
-
-
-class Six(Rank):
- def __init__(self):
- self.symbol = '6'
-
-
-class Five(Rank):
- def __init__(self):
- self.symbol = '5'
-
-
-class Four(Rank):
- def __init__(self):
- self.symbol = '4'
-
-
-class Three(Rank):
- def __init__(self):
- self.symbol = '3'
-
-
-class Two(Rank):
- def __init__(self):
- self.symbol = '2'
-
-
-class Suit:
- def __init__(self, color):
- self.color = color
-
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return (self.color == other.color) and (str(self) == str(other))
+Hearts = type("Hearts", (Suit,), {'color': 'red'})
+Clubs = type("Clubs", (Suit,), {'color': 'red'})
+Spades = type("Spades", (Suit,), {'color': 'black'})
+Diamonds = type("Diamonds", (Suit,), {'color': 'black'})
-class Hearts(Suit):
- def __init__(self):
- self.color = 'red'
-
-class Clubs(Suit):
- def __init__(self):
- self.color = 'red'
-
-
-class Spades(Suit):
- def __init__(self):
- self.color = 'black'
-
-
-class Diamonds(Suit):
- def __init__(self):
- self.color = 'black'
-
-
class Card:
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
def __repr__(self):
return '<{0} {1}>'.format(self.__class__.__name__, str(self))
def __str__(self):
return '{0} of {1}'.format(self.rank, self.suit)
@property
def rank(self):
return self._rank()
@rank.setter
def rank(self, value):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def suit(self):
return self._suit()
@suit.setter
def suit(self, value):
raise AttributeError("can't set attribute")
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
+ def __str__(self):
+ return str([str(card) for card in self.collection])
+
def __len__(self):
return len(self.collection)
- def __iter__(self):
- for card in self.collection:
- yield card
-
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
drawn_card = self.collection[index]
del self.collection[index]
return drawn_card
def draw_from_top(self):
drawn_card = self.collection[len(self) - 1]
del self.collection[len(self) - 1]
return drawn_card
def draw_from_bottom(self):
drawn_card = self.collection[0]
del self.collection[0]
return drawn_card
def top_card(self):
return self.collection[len(self) - 1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
- return self.collection.index(card)
+ cards_in_deck = [str(card) for card in self.collection]
+ return cards_in_deck.index(str(card))
-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}
-SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
- 'Spades': Spades, 'Diamonds': Diamonds}
+RANKS = OrderedDict([('King', King), ('Queen', Queen), ('Jack', Jack), ('Ten', Ten), ('Nine', Nine), ('Eight', Eight),
+ ('Seven', Seven), ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three), ('Two', Two), ('Ace', Ace)])
-def StandardDeck():
- ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
- 'Eight', 'Seven', 'Six', 'Five', 'Four',
- 'Three', 'Two', 'Ace']
- ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUITS = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs), ('Hearts', Hearts), ('Spades', Spades)])
+
+def StandardDeck():
standard_deck = []
- for suit in ordered_suits:
- for rank in ordered_ranks:
+ for suit in SUITS:
+ for rank in RANKS:
standard_deck.append(Card(RANKS[rank], SUITS[suit]))
return standard_deck
def BeloteDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
belote_deck = []
- for suit in ordered_suits:
+ for suit in SUITS:
for rank in ordered_ranks:
belote_deck.append(Card(RANKS[rank], SUITS[suit]))
return belote_deck
def SixtySixDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
sixty_six_deck = []
for suit in ordered_suits:
for rank in ordered_ranks:
sixty_six_deck.append(Card(RANKS[rank], SUITS[suit]))
return sixty_six_deck

Таня обнови решението на 25.03.2014 21:23 (преди около 10 години)

from collections import OrderedDict
class Rank:
def __init__(self):
self.symbol = self.__class__.__name__[0]
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.symbol == other.symbol
King = type("King", (Rank,), {})
Queen = type("Queen", (Rank,), {})
Jack = type("Jack", (Rank,), {})
Ace = type("Ace", (Rank,), {})
Ten = type("Ten", (Rank,), {})
Nine = type("Nine", (Rank,), {})
Eight = type("Eight", (Rank,), {})
Seven = type("Seven", (Rank,), {})
Six = type("Six", (Rank,), {})
Five = type("Five", (Rank,), {})
Four = type("Four", (Rank,), {})
Three = type("Three", (Rank,), {})
Two = type("Two", (Rank,), {})
class Suit:
def __init__(self):
pass
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return (self.color == other.color) and (str(self) == str(other))
Hearts = type("Hearts", (Suit,), {'color': 'red'})
Clubs = type("Clubs", (Suit,), {'color': 'red'})
Spades = type("Spades", (Suit,), {'color': 'black'})
Diamonds = type("Diamonds", (Suit,), {'color': 'black'})
class Card:
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
def __repr__(self):
return '<{0} {1}>'.format(self.__class__.__name__, str(self))
def __str__(self):
return '{0} of {1}'.format(self.rank, self.suit)
@property
def rank(self):
return self._rank()
@rank.setter
def rank(self, value):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def suit(self):
return self._suit()
@suit.setter
def suit(self, value):
raise AttributeError("can't set attribute")
class CardCollection:
- def __init__(self, collection=[]):
+ def __init__(self, collection=None):
self.collection = collection
+ if self.collection is None:
+ self.collection = []
def __str__(self):
return str([str(card) for card in self.collection])
def __len__(self):
return len(self.collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
drawn_card = self.collection[index]
del self.collection[index]
return drawn_card
def draw_from_top(self):
drawn_card = self.collection[len(self) - 1]
del self.collection[len(self) - 1]
return drawn_card
def draw_from_bottom(self):
drawn_card = self.collection[0]
del self.collection[0]
return drawn_card
def top_card(self):
return self.collection[len(self) - 1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
cards_in_deck = [str(card) for card in self.collection]
return cards_in_deck.index(str(card))
RANKS = OrderedDict([('King', King), ('Queen', Queen), ('Jack', Jack), ('Ten', Ten), ('Nine', Nine), ('Eight', Eight),
('Seven', Seven), ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three), ('Two', Two), ('Ace', Ace)])
SUITS = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs), ('Hearts', Hearts), ('Spades', Spades)])
def StandardDeck():
standard_deck = []
for suit in SUITS:
for rank in RANKS:
standard_deck.append(Card(RANKS[rank], SUITS[suit]))
return standard_deck
def BeloteDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
belote_deck = []
for suit in SUITS:
for rank in ordered_ranks:
belote_deck.append(Card(RANKS[rank], SUITS[suit]))
return belote_deck
def SixtySixDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
sixty_six_deck = []
for suit in ordered_suits:
for rank in ordered_ranks:
sixty_six_deck.append(Card(RANKS[rank], SUITS[suit]))
return sixty_six_deck

Таня обнови решението на 26.03.2014 09:21 (преди около 10 години)

from collections import OrderedDict
class Rank:
- def __init__(self):
- self.symbol = self.__class__.__name__[0]
+ def __init__(self, symbol=None):
+ self.symbol = symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.symbol == other.symbol
-King = type("King", (Rank,), {})
-Queen = type("Queen", (Rank,), {})
-Jack = type("Jack", (Rank,), {})
-Ace = type("Ace", (Rank,), {})
-Ten = type("Ten", (Rank,), {})
-Nine = type("Nine", (Rank,), {})
-Eight = type("Eight", (Rank,), {})
-Seven = type("Seven", (Rank,), {})
-Six = type("Six", (Rank,), {})
-Five = type("Five", (Rank,), {})
-Four = type("Four", (Rank,), {})
-Three = type("Three", (Rank,), {})
-Two = type("Two", (Rank,), {})
+ def __setattr__(self, attribute, value):
+ if attribute is None:
+ object.__setattr__(self, attribute, value)
+King = type("King", (Rank,), {'symbol': 'K'})
+Queen = type("Queen", (Rank,), {'symbol': 'Q'})
+Jack = type("Jack", (Rank,), {'symbol': 'J'})
+Ace = type("Ace", (Rank,), {'symbol': 'A'})
+Ten = type("Ten", (Rank,), {'symbol': '10'})
+Nine = type("Nine", (Rank,), {'symbol': '9'})
+Eight = type("Eight", (Rank,), {'symbol': '8'})
+Seven = type("Seven", (Rank,), {'symbol': '7'})
+Six = type("Six", (Rank,), {'symbol': '6'})
+Five = type("Five", (Rank,), {'symbol': '5'})
+Four = type("Four", (Rank,), {'symbol': '4'})
+Three = type("Three", (Rank,), {'symbol': '3'})
+Two = type("Two", (Rank,), {'symbol': '2'})
+
+
class Suit:
- def __init__(self):
- pass
+ def __init__(self, color=None):
+ self.color = color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return (self.color == other.color) and (str(self) == str(other))
+
+ def __setattr__(self, attribute, value):
+ if attribute is None:
+ object.__setattr__(self, attribute, value)
Hearts = type("Hearts", (Suit,), {'color': 'red'})
Clubs = type("Clubs", (Suit,), {'color': 'red'})
Spades = type("Spades", (Suit,), {'color': 'black'})
Diamonds = type("Diamonds", (Suit,), {'color': 'black'})
class Card:
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
def __repr__(self):
return '<{0} {1}>'.format(self.__class__.__name__, str(self))
def __str__(self):
return '{0} of {1}'.format(self.rank, self.suit)
@property
def rank(self):
return self._rank()
@rank.setter
def rank(self, value):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self._rank == other._rank and self._suit == other._suit
@property
def suit(self):
return self._suit()
@suit.setter
def suit(self, value):
raise AttributeError("can't set attribute")
class CardCollection:
def __init__(self, collection=None):
self.collection = collection
if self.collection is None:
self.collection = []
def __str__(self):
return str([str(card) for card in self.collection])
def __len__(self):
return len(self.collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
drawn_card = self.collection[index]
del self.collection[index]
return drawn_card
def draw_from_top(self):
drawn_card = self.collection[len(self) - 1]
del self.collection[len(self) - 1]
return drawn_card
def draw_from_bottom(self):
drawn_card = self.collection[0]
del self.collection[0]
return drawn_card
def top_card(self):
return self.collection[len(self) - 1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
cards_in_deck = [str(card) for card in self.collection]
return cards_in_deck.index(str(card))
RANKS = OrderedDict([('King', King), ('Queen', Queen), ('Jack', Jack), ('Ten', Ten), ('Nine', Nine), ('Eight', Eight),
('Seven', Seven), ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three), ('Two', Two), ('Ace', Ace)])
SUITS = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs), ('Hearts', Hearts), ('Spades', Spades)])
def StandardDeck():
standard_deck = []
for suit in SUITS:
for rank in RANKS:
standard_deck.append(Card(RANKS[rank], SUITS[suit]))
return standard_deck
def BeloteDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
belote_deck = []
for suit in SUITS:
for rank in ordered_ranks:
belote_deck.append(Card(RANKS[rank], SUITS[suit]))
return belote_deck
def SixtySixDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
sixty_six_deck = []
for suit in ordered_suits:
for rank in ordered_ranks:
sixty_six_deck.append(Card(RANKS[rank], SUITS[suit]))
return sixty_six_deck