Решение на Тесте карти от Атанас Димитров

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

Към профила на Атанас Димитров

Резултати

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

Код

class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
King = type('King', (Rank,),
{'__init__': lambda self: Rank.__init__(self, 'K'),
'__str__': lambda self: 'King'})
Queen = type('Queen', (Rank,),
{'__init__': lambda self: Rank.__init__(self, 'Q'),
'__str__': lambda self: 'Queen'})
Jack = type('Jack', (Rank,),
{'__init__': lambda self: Rank.__init__(self, 'J'),
'__str__': lambda self: 'Jack'})
Ten = type('Ten', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '10'),
'__str__': lambda self: 'Ten'})
Nine = type('Nine', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '9'),
'__str__': lambda self: 'Nine'})
Eight = type('Eight', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '8'),
'__str__': lambda self: 'Eight'})
Seven = type('Seven', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '7'),
'__str__': lambda self: 'Seven'})
Six = type('Six', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '6'),
'__str__': lambda self: 'Six'})
Five = type('Five', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '5'),
'__str__': lambda self: 'Five'})
Four = type('Four', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '4'),
'__str__': lambda self: 'Four'})
Three = type('Three', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '3'),
'__str__': lambda self: 'Three'})
Two = type('Two', (Rank,),
{'__init__': lambda self: Rank.__init__(self, '2'),
'__str__': lambda self: 'Two'})
Ace = type('Ace', (Rank,),
{'__init__': lambda self: Rank.__init__(self, 'A'),
'__str__': lambda self: 'Ace'})
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
Hearts = type('Hearts', (Suit,),
{'__init__': lambda self: Suit.__init__(self, 'red'),
'__str__': lambda self: 'Hearts',
'__eq__': lambda self, other: str(self) == str(other)})
Diamonds = type('Diamonds', (Suit,),
{'__init__': lambda self: Suit.__init__(self, 'red'),
'__str__': lambda self: 'Diamonds',
'__eq__': lambda self, other: str(self) == str(other)})
Spades = type('Spades', (Suit,),
{'__init__': lambda self: Suit.__init__(self, 'black'),
'__str__': lambda self: 'Spades',
'__eq__': lambda self, other: str(self) == str(other)})
Clubs = type('Clubs', (Suit,),
{'__init__': lambda self: Suit.__init__(self, 'black'),
'__str__': lambda self: 'Clubs',
'__eq__': lambda self, other: str(self) == str(other)})
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return self.rank == other.rank and \
self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
RANKS = {
str(Two()): Two, str(Three()): Three, str(Four()): Four,
str(Five()): Five, str(Six()): Six, str(Seven()): Seven,
str(Eight()): Eight, str(Nine()): Nine, str(Ten()): Ten,
str(Jack()): Jack, str(Queen()): Queen, str(King()): King,
str(Ace()): Ace
}
SUITS = {
str(Clubs()): Clubs, str(Diamonds()): Diamonds,
str(Hearts()): Hearts, str(Spades()): Spades
}
class CardCollection:
def __init__(self, collection=()):
self.collection = list(collection)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def __repr__(self):
return str([card for card in self])
def draw(self, index):
drawn_card = self.collection[index]
self.collection.remove(drawn_card)
return drawn_card
def draw_from_top(self):
return self.draw(len(self) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def StandardDeck():
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
rank_order = [
'King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Six', 'Five', 'Four',
'Three', 'Two', 'Ace'
]
collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
for rank in rank_order]
deck = CardCollection(collection)
return deck
def BeloteDeck():
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
rank_order = [
'King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Ace'
]
collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
for rank in rank_order]
deck = CardCollection(collection)
return deck
def SixtySixDeck():
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
rank_order = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
for rank in rank_order]
deck = CardCollection(collection)
return deck

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.023s

OK

История (2 версии и 3 коментара)

Атанас обнови решението на 21.03.2014 13:22 (преди почти 11 години)

+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Ace(Rank):
+ def __init__(self):
+ self.symbol = 'A'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Ace'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Two(Rank):
+ def __init__(self):
+ self.symbol = '2'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Two'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Three(Rank):
+ def __init__(self):
+ self.symbol = '3'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Three'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Four(Rank):
+ def __init__(self):
+ self.symbol = '4'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Four'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Five(Rank):
+ def __init__(self):
+ self.symbol = '5'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Five'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Six(Rank):
+ def __init__(self):
+ self.symbol = '6'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Six'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Seven(Rank):
+ def __init__(self):
+ self.symbol = '7'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Seven'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Eight(Rank):
+ def __init__(self):
+ self.symbol = '8'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Eight'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Nine(Rank):
+ def __init__(self):
+ self.symbol = '9'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Nine'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Ten(Rank):
+ def __init__(self):
+ self.symbol = '10'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Ten'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Jack(Rank):
+ def __init__(self):
+ self.symbol = 'J'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Jack'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Queen(Rank):
+ def __init__(self):
+ self.symbol = 'Q'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'Queen'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class King(Rank):
+ def __init__(self):
+ self.symbol = 'K'
+
+ def __eq__(self, other):
+ return Rank.__eq__(self, other)
+
+ def __str__(self):
+ return 'King'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Hearts'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Diamonds'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Spades'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Clubs'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __eq__(self, other):
+ return self.rank == other.rank and \
+ self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ @classmethod
+ def name(cls):
+ return cls
+
+
+RANKS = {
+ str(Two()): Two.name(), str(Three()): Three.name(),
+ str(Four()): Four.name(), str(Five()): Five.name(),
+ str(Six()): Six.name(), str(Seven()): Seven.name(),
+ str(Eight()): Eight.name(), str(Nine()): Nine.name(),
+ str(Ten()): Ten.name(), str(Jack()): Jack.name(),
+ str(Queen()): Queen.name(), str(King()): King.name(),
+ str(Ace()): Ace.name()
+}
+
+
+SUITS = {
+ str(Clubs()): Clubs.name(), str(Diamonds()): Diamonds.name(),
+ str(Hearts()): Hearts.name(), str(Spades()): Spades.name()
+}
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = list(collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __repr__(self):
+ return str([repr(card) for card in self])
+
+ def draw(self, index):
+ drawn_card = self.collection[index]
+ self.collection.remove(drawn_card)
+ return drawn_card
+
+ def draw_from_top(self):
+ return self.draw(len(self) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self[len(self) - 1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+
+def StandardDeck():
+ suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ rank_order = [
+ 'King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Six', 'Five', 'Four',
+ 'Three', 'Two', 'Ace'
+ ]
+ collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
+ for rank in rank_order]
+ deck = CardCollection(collection)
+ return deck
+
+
+def BeloteDeck():
+ suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ rank_order = [
+ 'King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Ace'
+ ]
+ collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
+ for rank in rank_order]
+ deck = CardCollection(collection)
+ return deck
+
+
+def SixtySixDeck():
+ suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ rank_order = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
+ for rank in rank_order]
+ deck = CardCollection(collection)
+ return deck

Няма нужда на всеки подклас на Rank да предефинираш __eq__. Този метод се наследява от базовия клас. Същото важи и за подкласовете на Suit.

def __init__(self, collection=[]): - това е много лоша идея. За подразбираща се стойност на използваш list, което е mutable

Освен тези неща, кода ти е разбираем. Въпреки това, бих те посъветвал да помислиш как да съкратиш решението си.

Hint: с type можеш да си дефинираш класове.

Да разбирам ли, че ако използвам кортеж за подразбираща се стойност т.е.def init(self, collection=()): гореспоменатият проблем не е на лице? Също така, защо да не трябва да се предефинира eq за наследяващите Suit, като двата метода сравняват различни неща- цветове в единия случай, и имена в другия?

PS: "__" около init и eq не излизат като хората

Атанас обнови решението на 24.03.2014 06:33 (преди почти 11 години)

class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
-class Ace(Rank):
- def __init__(self):
- self.symbol = 'A'
+King = type('King', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, 'K'),
+ '__str__': lambda self: 'King'})
- def __eq__(self, other):
- return Rank.__eq__(self, other)
+Queen = type('Queen', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, 'Q'),
+ '__str__': lambda self: 'Queen'})
- def __str__(self):
- return 'Ace'
+Jack = type('Jack', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, 'J'),
+ '__str__': lambda self: 'Jack'})
- @classmethod
- def name(cls):
- return cls
+Ten = type('Ten', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '10'),
+ '__str__': lambda self: 'Ten'})
+Nine = type('Nine', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '9'),
+ '__str__': lambda self: 'Nine'})
-class Two(Rank):
- def __init__(self):
- self.symbol = '2'
+Eight = type('Eight', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '8'),
+ '__str__': lambda self: 'Eight'})
- def __eq__(self, other):
- return Rank.__eq__(self, other)
+Seven = type('Seven', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '7'),
+ '__str__': lambda self: 'Seven'})
- def __str__(self):
- return 'Two'
+Six = type('Six', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '6'),
+ '__str__': lambda self: 'Six'})
- @classmethod
- def name(cls):
- return cls
+Five = type('Five', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '5'),
+ '__str__': lambda self: 'Five'})
+Four = type('Four', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '4'),
+ '__str__': lambda self: 'Four'})
-class Three(Rank):
- def __init__(self):
- self.symbol = '3'
+Three = type('Three', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '3'),
+ '__str__': lambda self: 'Three'})
- def __eq__(self, other):
- return Rank.__eq__(self, other)
+Two = type('Two', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, '2'),
+ '__str__': lambda self: 'Two'})
- def __str__(self):
- return 'Three'
+Ace = type('Ace', (Rank,),
+ {'__init__': lambda self: Rank.__init__(self, 'A'),
+ '__str__': lambda self: 'Ace'})
- @classmethod
- def name(cls):
- return cls
-
-class Four(Rank):
- def __init__(self):
- self.symbol = '4'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Four'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Five(Rank):
- def __init__(self):
- self.symbol = '5'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Five'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Six(Rank):
- def __init__(self):
- self.symbol = '6'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Six'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Seven(Rank):
- def __init__(self):
- self.symbol = '7'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Seven'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Eight(Rank):
- def __init__(self):
- self.symbol = '8'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Eight'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Nine(Rank):
- def __init__(self):
- self.symbol = '9'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Nine'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Ten(Rank):
- def __init__(self):
- self.symbol = '10'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Ten'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Jack(Rank):
- def __init__(self):
- self.symbol = 'J'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Jack'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Queen(Rank):
- def __init__(self):
- self.symbol = 'Q'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'Queen'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class King(Rank):
- def __init__(self):
- self.symbol = 'K'
-
- def __eq__(self, other):
- return Rank.__eq__(self, other)
-
- def __str__(self):
- return 'King'
-
- @classmethod
- def name(cls):
- return cls
-
-
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
-class Hearts(Suit):
- def __init__(self):
- self.color = 'red'
+Hearts = type('Hearts', (Suit,),
+ {'__init__': lambda self: Suit.__init__(self, 'red'),
+ '__str__': lambda self: 'Hearts',
+ '__eq__': lambda self, other: str(self) == str(other)})
- def __eq__(self, other):
- return str(self) == str(other)
+Diamonds = type('Diamonds', (Suit,),
+ {'__init__': lambda self: Suit.__init__(self, 'red'),
+ '__str__': lambda self: 'Diamonds',
+ '__eq__': lambda self, other: str(self) == str(other)})
- def __str__(self):
- return 'Hearts'
+Spades = type('Spades', (Suit,),
+ {'__init__': lambda self: Suit.__init__(self, 'black'),
+ '__str__': lambda self: 'Spades',
+ '__eq__': lambda self, other: str(self) == str(other)})
- @classmethod
- def name(cls):
- return cls
+Clubs = type('Clubs', (Suit,),
+ {'__init__': lambda self: Suit.__init__(self, 'black'),
+ '__str__': lambda self: 'Clubs',
+ '__eq__': lambda self, other: str(self) == str(other)})
-class Diamonds(Suit):
- def __init__(self):
- self.color = 'red'
-
- def __eq__(self, other):
- return str(self) == str(other)
-
- def __str__(self):
- return 'Diamonds'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Spades(Suit):
- def __init__(self):
- self.color = 'black'
-
- def __eq__(self, other):
- return str(self) == str(other)
-
- def __str__(self):
- return 'Spades'
-
- @classmethod
- def name(cls):
- return cls
-
-
-class Clubs(Suit):
- def __init__(self):
- self.color = 'black'
-
- def __eq__(self, other):
- return str(self) == str(other)
-
- def __str__(self):
- return 'Clubs'
-
- @classmethod
- def name(cls):
- return cls
-
-
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return self.rank == other.rank and \
self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
- @classmethod
- def name(cls):
- return cls
-
RANKS = {
- str(Two()): Two.name(), str(Three()): Three.name(),
- str(Four()): Four.name(), str(Five()): Five.name(),
- str(Six()): Six.name(), str(Seven()): Seven.name(),
- str(Eight()): Eight.name(), str(Nine()): Nine.name(),
- str(Ten()): Ten.name(), str(Jack()): Jack.name(),
- str(Queen()): Queen.name(), str(King()): King.name(),
- str(Ace()): Ace.name()
+ str(Two()): Two, str(Three()): Three, str(Four()): Four,
+ str(Five()): Five, str(Six()): Six, str(Seven()): Seven,
+ str(Eight()): Eight, str(Nine()): Nine, str(Ten()): Ten,
+ str(Jack()): Jack, str(Queen()): Queen, str(King()): King,
+ str(Ace()): Ace
}
SUITS = {
- str(Clubs()): Clubs.name(), str(Diamonds()): Diamonds.name(),
- str(Hearts()): Hearts.name(), str(Spades()): Spades.name()
+ str(Clubs()): Clubs, str(Diamonds()): Diamonds,
+ str(Hearts()): Hearts, str(Spades()): Spades
}
class CardCollection:
- def __init__(self, collection=[]):
+ def __init__(self, collection=()):
self.collection = list(collection)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def __repr__(self):
- return str([repr(card) for card in self])
+ return str([card for card in self])
def draw(self, index):
drawn_card = self.collection[index]
self.collection.remove(drawn_card)
return drawn_card
def draw_from_top(self):
return self.draw(len(self) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def StandardDeck():
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
rank_order = [
'King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Six', 'Five', 'Four',
'Three', 'Two', 'Ace'
]
collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
for rank in rank_order]
deck = CardCollection(collection)
return deck
def BeloteDeck():
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
rank_order = [
'King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Ace'
]
collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
for rank in rank_order]
deck = CardCollection(collection)
return deck
def SixtySixDeck():
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
rank_order = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
collection = [Card(RANKS[rank], SUITS[suit]) for suit in suit_order
for rank in rank_order]
deck = CardCollection(collection)
return deck

Използването на кортеж донякъде решева проблема, но най-добрия вариант е, ако имаш подразбиращ се аргумент, да го сетваш на None, а след това в кода да правиш следната проверка:

if a is None:
    a = []

Казвам ти го само, за да го имаш впредвид занапред.

Относно втория въпрос, имах впредвид, че този код е напълно излишен: return Rank.__eq__(self, other). Python сам ще стигне до там от базовия клас.