Решение на Тесте карти от Милица Борисова

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

Към профила на Милица Борисова

Резултати

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

Код

class Rank:
symbol = 'None'
def __init__(self):
pass
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.symbol
class Suit:
suit = 'None'
def __init__(self):
pass
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.suit
class Two(Rank):
symbol = 'Two'
class Three(Rank):
symbol = 'Three'
class Four(Rank):
symbol = 'Four'
class Five(Rank):
symbol = 'Five'
class Six(Rank):
symbol = 'Six'
class Seven(Rank):
symbol = 'Seven'
class Eight(Rank):
symbol = 'Eight'
class Nine(Rank):
symbol = 'Nine'
class Ten(Rank):
symbol = 'Ten'
class Jack(Rank):
symbol = 'Jack'
class Queen(Rank):
symbol = 'Queen'
class King(Rank):
symbol = 'King'
class Ace(Rank):
symbol = 'Ace'
class Diamonds(Suit):
suit = 'Diamonds'
class Hearts(Suit):
suit = 'Hearts'
class Clubs(Suit):
suit = 'Clubs'
class Spades(Suit):
suit = 'Spades'
RANK_CLASSES = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
Ten, Jack, Queen, King]
SUIT_CLASSES = [Diamonds, Clubs, Hearts, Spades]
RANKS = {rank_class.symbol: rank_class for rank_class in RANK_CLASSES}
SUITS = {suit_class.suit: suit_class for suit_class in SUIT_CLASSES}
class Card():
def __setattr__(self, *args):
raise TypeError("can't set attribute")
__delattr__ = __setattr__
def __init__(self, Rank, Suit):
super(Card, self).__setattr__('rank', Rank())
super(Card, self).__setattr__('suit', Suit())
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __lt__(self, other):
if self.suit == other.suit:
return RANK_CLASSES.index(type(self.rank)) > \
RANK_CLASSES.index(type(other.rank))
else:
return SUIT_CLASSES.index(type(self.suit)) < \
SUIT_CLASSES.index(type(other.suit))
class CardCollection:
def __init__(self, collection = []):
if type(collection) == dict:
self.deck = [(item, collection[item]) for item in collection]
else:
self.deck = list(collection)
def __len__(self):
return len(self.deck)
def __getitem__(self, key):
if key >= len(self):
raise IndexError("list index out of range")
else:
return self.deck[key]
def draw(self, index):
return self.deck.pop(index)
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.deck[len(self) - 1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
if card not in self.deck:
raise ValueError("< Card " + str(card) + " > is not list")
else:
return self.deck.index(card)
def __str__(self):
return str(self.deck)
def __repr__(self):
return str(self)
def __iter__(self):
return iter(self.deck)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()]
deck.sort()
return CardCollection(deck)
def BeloteDeck():
return CardCollection([card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Six) or
type(card.rank) == Ace])
def SixtySixDeck():
return CardCollection([card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Eight) or
type(card.rank) == Ace])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.026s

OK

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

Милица обнови решението на 22.03.2014 01:04 (преди около 10 години)

+SYMBOLS_AND_RANKS = {'A': 'Ace', 'K': 'King', 'Q': 'Queen', 'J': 'Jack',
+ '10': 'Ten', '9': 'Nine', '8': 'Eight', '7': 'Seven',
+ '6': 'Six', '5': 'Five', '4': 'Four', '3': 'Three',
+ '2': 'Two'}
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.suit = 'Diamonds'
+
+ def __str__(self):
+ return self.suit
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.suit = 'Hearts'
+
+ def __str__(self):
+ return self.suit
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.suit = 'Clubs'
+
+ def __str__(self):
+ return self.suit
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.suit = 'Spades'
+
+ def __str__(self):
+ return self.suit
+
+
+RANK_CLASSES = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
+ Ten, Jack, Queen, King]
+
+
+SUIT_CLASSES = [Diamonds, Hearts, Clubs, Spades]
+
+
+RANKS = {rank_class().rank: rank_class for rank_class in RANK_CLASSES}
+
+
+SUITS = {suit_class().suit: suit_class for suit_class in SUIT_CLASSES}
+
+
+class Card():
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+ __delattr__ = __setattr__
+
+ def __init__(self, Rank, Suit):
+ super(Card, self).__setattr__('rank', Rank())
+ super(Card, self).__setattr__('suit', Suit())
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __lt__(self, other):
+ if SUIT_CLASSES.index(type(self.suit)) < \
+ SUIT_CLASSES.index(type(other.suit)):
+ return True
+ elif SUIT_CLASSES.index(type(self.suit)) == \
+ SUIT_CLASSES.index(type(other.suit)):
+ return RANK_CLASSES.index(type(self.rank)) > \
+ RANK_CLASSES.index(type(other.rank))
+ else:
+ return False
+
+ def __gt__(self, other):
+ return not self < other and self != other
+
+
+class CardCollection:
+ def __init__(self, collection = []):
+ if type(collection) == dict:
+ self.deck = [(item, collection[item]) for item in collection]
+ else:
+ self.deck = list(collection)
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __getitem__(self, key):
+ if key >= len(self):
+ raise IndexError("list index out of range")
+ else:
+ return self.deck[key]
+
+ def draw(self, index):
+ removed_card = self.deck[index]
+ self.deck.remove(self.deck[index])
+ return removed_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.deck[len(self) - 1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ if card not in self.deck:
+ raise ValueError("< Card " + card + " > is not list")
+ else:
+ return self.deck.index(card)
+
+ def __str__(self):
+ return str(self.deck)
+
+ def __repr__(self):
+ return str(self)
+
+
+def StandartDeck():
+ deck = [Card(rank, suit) for rank in RANKS.values()
+ for suit in SUITS.values()]
+ deck.sort()
+ return CardCollection(deck)
+
+
+def BeloteDeck():
+ return [card for card in StandartDeck() if
+ RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Six) or
+ type(card.rank) == Ace]
+
+
+def SixtySixDeck():
+ return [card for card in StandartDeck() if
+ RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Eight) or
+ type(card.rank) == Ace]

Милица обнови решението на 22.03.2014 22:24 (преди около 10 години)

SYMBOLS_AND_RANKS = {'A': 'Ace', 'K': 'King', 'Q': 'Queen', 'J': 'Jack',
'10': 'Ten', '9': 'Nine', '8': 'Eight', '7': 'Seven',
'6': 'Six', '5': 'Five', '4': 'Four', '3': 'Three',
'2': 'Two'}
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return SYMBOLS_AND_RANKS[self.symbol]
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
self.rank = SYMBOLS_AND_RANKS[self.symbol]
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
self.suit = 'Diamonds'
def __str__(self):
return self.suit
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
self.suit = 'Hearts'
def __str__(self):
return self.suit
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
self.suit = 'Clubs'
def __str__(self):
return self.suit
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
self.suit = 'Spades'
def __str__(self):
return self.suit
RANK_CLASSES = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
Ten, Jack, Queen, King]
SUIT_CLASSES = [Diamonds, Hearts, Clubs, Spades]
RANKS = {rank_class().rank: rank_class for rank_class in RANK_CLASSES}
SUITS = {suit_class().suit: suit_class for suit_class in SUIT_CLASSES}
class Card():
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
__delattr__ = __setattr__
def __init__(self, Rank, Suit):
super(Card, self).__setattr__('rank', Rank())
super(Card, self).__setattr__('suit', Suit())
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __lt__(self, other):
if SUIT_CLASSES.index(type(self.suit)) < \
SUIT_CLASSES.index(type(other.suit)):
return True
elif SUIT_CLASSES.index(type(self.suit)) == \
SUIT_CLASSES.index(type(other.suit)):
return RANK_CLASSES.index(type(self.rank)) > \
RANK_CLASSES.index(type(other.rank))
else:
return False
def __gt__(self, other):
return not self < other and self != other
class CardCollection:
def __init__(self, collection = []):
if type(collection) == dict:
self.deck = [(item, collection[item]) for item in collection]
else:
self.deck = list(collection)
def __len__(self):
return len(self.deck)
def __getitem__(self, key):
if key >= len(self):
raise IndexError("list index out of range")
else:
return self.deck[key]
def draw(self, index):
removed_card = self.deck[index]
self.deck.remove(self.deck[index])
return removed_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.deck[len(self) - 1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
if card not in self.deck:
- raise ValueError("< Card " + card + " > is not list")
+ raise ValueError("< Card " + str(card) + " > is not list")
else:
return self.deck.index(card)
def __str__(self):
return str(self.deck)
def __repr__(self):
return str(self)
-def StandartDeck():
+def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()]
deck.sort()
return CardCollection(deck)
def BeloteDeck():
- return [card for card in StandartDeck() if
+ return [card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Six) or
type(card.rank) == Ace]
def SixtySixDeck():
- return [card for card in StandartDeck() if
+ return [card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Eight) or
type(card.rank) == Ace]
  • Възползвай се напълно от това, че наследяваш някой клас. Не ти трябва да дефинираш __init__ във всеки наследник
  • Card.__lt__ е крайно нечетим. Рефакторирай го
  • Виж документацията на list.pop. Ще ти помогне да имплементираш по-добре метода си draw
  • Редът на картите ти в разните тестета е грешен

Имам въпрос относно конструкторите на класовете наследници. Например, ако искам да си направя обект от клас Two, нямам конструктор за него и трябва да подавам знака на двойката -> two = Two('2') и така при създаването му всеки може да направи някоя гадория и да сложи някой друг знак. Та това проблем ли е? Също така нужно ли е всеки клас да "знае" какъв е знакът му?

Милица обнови решението на 25.03.2014 19:34 (преди около 10 години)

-SYMBOLS_AND_RANKS = {'A': 'Ace', 'K': 'King', 'Q': 'Queen', 'J': 'Jack',
- '10': 'Ten', '9': 'Nine', '8': 'Eight', '7': 'Seven',
- '6': 'Six', '5': 'Five', '4': 'Four', '3': 'Three',
- '2': 'Two'}
-
-
class Rank:
- def __init__(self, symbol):
- self.symbol = symbol
+ symbol = 'None'
+ def __init__(self):
+ pass
+
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
- return SYMBOLS_AND_RANKS[self.symbol]
+ return self.symbol
class Suit:
- def __init__(self, color):
- self.color = color
+ suit = 'None'
+ def __init__(self):
+ pass
+
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
+ def __str__(self):
+ return self.suit
+
class Two(Rank):
- def __init__(self):
- Rank.__init__(self, '2')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Two'
class Three(Rank):
- def __init__(self):
- Rank.__init__(self, '3')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Three'
class Four(Rank):
- def __init__(self):
- Rank.__init__(self, '4')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Four'
class Five(Rank):
- def __init__(self):
- Rank.__init__(self, '5')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Five'
class Six(Rank):
- def __init__(self):
- Rank.__init__(self, '6')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Six'
class Seven(Rank):
- def __init__(self):
- Rank.__init__(self, '7')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Seven'
class Eight(Rank):
- def __init__(self):
- Rank.__init__(self, '8')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Eight'
class Nine(Rank):
- def __init__(self):
- Rank.__init__(self, '9')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Nine'
class Ten(Rank):
- def __init__(self):
- Rank.__init__(self, '10')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Ten'
class Jack(Rank):
- def __init__(self):
- Rank.__init__(self, 'J')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Jack'
class Queen(Rank):
- def __init__(self):
- Rank.__init__(self, 'Q')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Queen'
class King(Rank):
- def __init__(self):
- Rank.__init__(self, 'K')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'King'
class Ace(Rank):
- def __init__(self):
- Rank.__init__(self, 'A')
- self.rank = SYMBOLS_AND_RANKS[self.symbol]
+ symbol = 'Ace'
class Diamonds(Suit):
- def __init__(self):
- Suit.__init__(self, 'red')
- self.suit = 'Diamonds'
+ suit = 'Diamonds'
- def __str__(self):
- return self.suit
-
class Hearts(Suit):
- def __init__(self):
- Suit.__init__(self, 'red')
- self.suit = 'Hearts'
+ suit = 'Hearts'
- def __str__(self):
- return self.suit
-
class Clubs(Suit):
- def __init__(self):
- Suit.__init__(self, 'black')
- self.suit = 'Clubs'
+ suit = 'Clubs'
- def __str__(self):
- return self.suit
-
class Spades(Suit):
- def __init__(self):
- Suit.__init__(self, 'black')
- self.suit = 'Spades'
+ suit = 'Spades'
- def __str__(self):
- return self.suit
-
RANK_CLASSES = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
Ten, Jack, Queen, King]
-SUIT_CLASSES = [Diamonds, Hearts, Clubs, Spades]
+SUIT_CLASSES = [Diamonds, Clubs, Hearts, Spades]
-RANKS = {rank_class().rank: rank_class for rank_class in RANK_CLASSES}
+RANKS = {rank_class.symbol: rank_class for rank_class in RANK_CLASSES}
-SUITS = {suit_class().suit: suit_class for suit_class in SUIT_CLASSES}
+SUITS = {suit_class.suit: suit_class for suit_class in SUIT_CLASSES}
class Card():
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
__delattr__ = __setattr__
def __init__(self, Rank, Suit):
super(Card, self).__setattr__('rank', Rank())
super(Card, self).__setattr__('suit', Suit())
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
- return str(self.rank) + ' of ' + str(self.suit)
+ return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __lt__(self, other):
- if SUIT_CLASSES.index(type(self.suit)) < \
- SUIT_CLASSES.index(type(other.suit)):
- return True
- elif SUIT_CLASSES.index(type(self.suit)) == \
- SUIT_CLASSES.index(type(other.suit)):
+ if self.suit == other.suit:
return RANK_CLASSES.index(type(self.rank)) > \
RANK_CLASSES.index(type(other.rank))
else:
- return False
+ return SUIT_CLASSES.index(type(self.suit)) < \
+ SUIT_CLASSES.index(type(other.suit))
def __gt__(self, other):
return not self < other and self != other
class CardCollection:
def __init__(self, collection = []):
if type(collection) == dict:
self.deck = [(item, collection[item]) for item in collection]
else:
self.deck = list(collection)
def __len__(self):
return len(self.deck)
def __getitem__(self, key):
if key >= len(self):
raise IndexError("list index out of range")
else:
return self.deck[key]
def draw(self, index):
- removed_card = self.deck[index]
- self.deck.remove(self.deck[index])
- return removed_card
+ return self.deck.pop(index)
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.deck[len(self) - 1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
if card not in self.deck:
raise ValueError("< Card " + str(card) + " > is not list")
else:
return self.deck.index(card)
def __str__(self):
return str(self.deck)
def __repr__(self):
return str(self)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()]
deck.sort()
return CardCollection(deck)
def BeloteDeck():
- return [card for card in StandardDeck() if
+ return CardCollection([card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Six) or
- type(card.rank) == Ace]
+ type(card.rank) == Ace])
def SixtySixDeck():
- return [card for card in StandardDeck() if
+ return CardCollection([card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Eight) or
- type(card.rank) == Ace]
+ type(card.rank) == Ace])

Милица обнови решението на 26.03.2014 01:10 (преди около 10 години)

class Rank:
symbol = 'None'
def __init__(self):
pass
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.symbol
class Suit:
suit = 'None'
def __init__(self):
pass
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return self.suit
class Two(Rank):
symbol = 'Two'
class Three(Rank):
symbol = 'Three'
class Four(Rank):
symbol = 'Four'
class Five(Rank):
symbol = 'Five'
class Six(Rank):
symbol = 'Six'
class Seven(Rank):
symbol = 'Seven'
class Eight(Rank):
symbol = 'Eight'
class Nine(Rank):
symbol = 'Nine'
class Ten(Rank):
symbol = 'Ten'
class Jack(Rank):
symbol = 'Jack'
class Queen(Rank):
symbol = 'Queen'
class King(Rank):
symbol = 'King'
class Ace(Rank):
symbol = 'Ace'
class Diamonds(Suit):
suit = 'Diamonds'
class Hearts(Suit):
suit = 'Hearts'
class Clubs(Suit):
suit = 'Clubs'
class Spades(Suit):
suit = 'Spades'
RANK_CLASSES = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
Ten, Jack, Queen, King]
SUIT_CLASSES = [Diamonds, Clubs, Hearts, Spades]
RANKS = {rank_class.symbol: rank_class for rank_class in RANK_CLASSES}
SUITS = {suit_class.suit: suit_class for suit_class in SUIT_CLASSES}
class Card():
def __setattr__(self, *args):
- raise AttributeError("can't set attribute")
+ raise TypeError("can't set attribute")
__delattr__ = __setattr__
def __init__(self, Rank, Suit):
super(Card, self).__setattr__('rank', Rank())
super(Card, self).__setattr__('suit', Suit())
def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __lt__(self, other):
if self.suit == other.suit:
return RANK_CLASSES.index(type(self.rank)) > \
RANK_CLASSES.index(type(other.rank))
else:
return SUIT_CLASSES.index(type(self.suit)) < \
SUIT_CLASSES.index(type(other.suit))
- def __gt__(self, other):
- return not self < other and self != other
-
class CardCollection:
def __init__(self, collection = []):
if type(collection) == dict:
self.deck = [(item, collection[item]) for item in collection]
else:
self.deck = list(collection)
def __len__(self):
return len(self.deck)
def __getitem__(self, key):
if key >= len(self):
raise IndexError("list index out of range")
else:
return self.deck[key]
def draw(self, index):
return self.deck.pop(index)
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.deck[len(self) - 1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
if card not in self.deck:
raise ValueError("< Card " + str(card) + " > is not list")
else:
return self.deck.index(card)
def __str__(self):
return str(self.deck)
def __repr__(self):
return str(self)
+
+ def __iter__(self):
+ return iter(self.deck)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()]
deck.sort()
return CardCollection(deck)
def BeloteDeck():
return CardCollection([card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Six) or
type(card.rank) == Ace])
def SixtySixDeck():
return CardCollection([card for card in StandardDeck() if
RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Eight) or
type(card.rank) == Ace])