Решение на Тесте карти от Божидар Михайлов

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

Към профила на Божидар Михайлов

Резултати

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

Код

from collections import deque, OrderedDict
def ClassFactory(name, arg, base):
def __init__(self):
base.__init__(self, arg)
return type(name, (base,), {"__init__": __init__})
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
RANKS_DATA = [("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')]
RANKS = OrderedDict([(name, ClassFactory(name, symbol, Rank))
for name, symbol in RANKS_DATA])
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
SUITS_DATA = [("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
("Spades", "black")]
SUITS = OrderedDict([(name, ClassFactory(name, color, Suit))
for name, color in SUITS_DATA])
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@rank.setter
def rank(self, rank):
raise TypeError('can\'t delete attribute')
@property
def suit(self):
return self.__suit
@suit.setter
def suit(self, suit):
raise TypeError('can\'t set attribute')
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)
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.__card_buffer = deque(collection)
else:
self.__card_buffer = deque()
def __len__(self):
return len(self.__card_buffer)
def __getitem__(self, index):
return self.__card_buffer[index]
def __str__(self):
return "[" + ', '.join(map(str, self)) + "]"
def draw(self, index):
drawn = self[index]
self.__card_buffer.remove(drawn)
return drawn
def draw_from_top(self):
return self.__card_buffer.pop()
def draw_from_bottom(self):
return self.__card_buffer.popleft()
def top_card(self):
return self.__card_buffer[-1]
def bottom_card(self):
return self.__card_buffer[0]
def add(self, card):
self.__card_buffer.append(card)
def index(self, card):
for i, element in enumerate(self.__card_buffer):
if card == element:
return i
raise ValueError("<Card {}> is not in the list".format(card))
def StandardDeck():
return CardCollection(Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values())
def BeloteDeck():
def is_belote_card(card):
return card.rank.symbol not in map(str, range(2, 7))
return CardCollection(card for card in StandardDeck()
if is_belote_card(card))
def SixtySixDeck():
def is_sixty_six_card(card):
return card.rank.symbol not in map(str, range(2, 9))
return CardCollection(card for card in StandardDeck()
if is_sixty_six_card(card))

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

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

OK

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

Божидар обнови решението на 22.03.2014 16:26 (преди над 10 години)

+from collections import deque
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+RANKS_LIST = [King, Queen, Jack, Ten, Nine, Eight,
+ Seven, Six, Five, Four, Three, Two, Ace]
+RANKS = {str(rank()): rank for rank in RANKS_LIST}
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+SUITS_LIST = [Diamonds, Clubs, Hearts, Spades]
+SUITS = {str(suit()): suit for suit in SUITS_LIST}
+
+
+class Card:
+ def __setattr__(self, *args):
+ raise TypeError('can\'t set attribute')
+
+ def __delattr__(self, *args):
+ raise TypeError('can\'t delete attribute')
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', 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)
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ if collection is not None:
+ self.__card_buffer = deque(collection)
+ else:
+ self.__card_buffer = deque()
+
+ def __len__(self):
+ return len(self.__card_buffer)
+
+ def __getitem__(self, index):
+ return self.__card_buffer[index]
+
+ def __str__(self):
+ representation = "["
+ for i in range(len(self.__card_buffer)-1):
+ representation += str(self.__card_buffer[i]) + ", "
+ representation += str(self.__card_buffer[-1]) + "]"
+ return representation
+
+ def draw(self, index):
+ drawn = self[index]
+ self.__card_buffer.remove(drawn)
+ return drawn
+
+ def draw_from_top(self):
+ return self.__card_buffer.pop()
+
+ def draw_from_bottom(self):
+ return self.__card_buffer.popleft()
+
+ def top_card(self):
+ return self.__card_buffer[-1]
+
+ def bottom_card(self):
+ return self.__card_buffer[0]
+
+ def add(self, card):
+ self.__card_buffer.append(card)
+
+ def index(self, card):
+ for i, element in enumerate(self.__card_buffer):
+ if card == element:
+ return i
+ raise ValueError("<Card {}> is not in the list".format(card))
+
+
+def StandardDeck():
+ return CardCollection(Card(rank, suit) for suit in SUITS_LIST
+ for rank in RANKS_LIST)
+
+
+def BeloteDeck():
+ def is_belote_card(card):
+ return card.rank not in [rank() for rank in RANKS_LIST[7:12]]
+ return CardCollection(card for card in StandardDeck()
+ if is_belote_card(card))
+
+
+def SixtySixDeck():
+ def is_sixty_six_card(card):
+ return card.rank not in [rank() for rank in RANKS_LIST[5:12]]
+ return CardCollection(card for card in StandardDeck()
+ if is_sixty_six_card(card))
  • Възползвай се от това, че наследяваш клас и не дефинирай __init__ във всеки наследник
  • Защо правиш това object.__setattr__(self, 'rank', rank())? В python си има синтаксис за тази операция
  • CardCollection.__str__ методът е сравнително нечетим. Виж документацията на str.format

Божидар обнови решението на 24.03.2014 13:36 (преди над 10 години)

from collections import deque
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
class Ace(Rank):
- def __init__(self):
- Rank.__init__(self, 'A')
+ def __init__(self, symbol='A'):
+ pass
class Two(Rank):
- def __init__(self):
- Rank.__init__(self, '2')
+ def __init__(self, symbol='2'):
+ pass
class Three(Rank):
- def __init__(self):
- Rank.__init__(self, '3')
+ def __init__(self, symbol='3'):
+ pass
class Four(Rank):
- def __init__(self):
- Rank.__init__(self, '4')
+ def __init__(self, symbol='4'):
+ pass
class Five(Rank):
- def __init__(self):
- Rank.__init__(self, '5')
+ def __init__(self, symbol='5'):
+ pass
class Six(Rank):
- def __init__(self):
- Rank.__init__(self, '6')
+ def __init__(self, symbol='6'):
+ pass
class Seven(Rank):
- def __init__(self):
- Rank.__init__(self, '7')
+ def __init__(self, symbol='7'):
+ pass
class Eight(Rank):
- def __init__(self):
- Rank.__init__(self, '8')
+ def __init__(self, symbol='8'):
+ pass
class Nine(Rank):
- def __init__(self):
- Rank.__init__(self, '9')
+ def __init__(self, symbol='9'):
+ pass
class Ten(Rank):
- def __init__(self):
- Rank.__init__(self, '10')
+ def __init__(self, symbol='10'):
+ pass
class Jack(Rank):
- def __init__(self):
- Rank.__init__(self, 'J')
+ def __init__(self, symbol='J'):
+ pass
class Queen(Rank):
- def __init__(self):
- Rank.__init__(self, 'Q')
+ def __init__(self, symbol='Q'):
+ pass
class King(Rank):
- def __init__(self):
- Rank.__init__(self, 'K')
+ def __init__(self, symbol='K'):
+ pass
+
RANKS_LIST = [King, Queen, Jack, Ten, Nine, Eight,
Seven, Six, Five, Four, Three, Two, Ace]
RANKS = {str(rank()): rank for rank in RANKS_LIST}
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
class Hearts(Suit):
- def __init__(self):
- Suit.__init__(self, "red")
+ def __init__(self, symbol="red"):
+ pass
class Clubs(Suit):
- def __init__(self):
- Suit.__init__(self, "black")
+ def __init__(self, symbol="black"):
+ pass
class Spades(Suit):
- def __init__(self):
- Suit.__init__(self, "black")
+ def __init__(self, symbol="black"):
+ pass
class Diamonds(Suit):
- def __init__(self):
- Suit.__init__(self, "red")
+ def __init__(self, symbol="red"):
+ pass
SUITS_LIST = [Diamonds, Clubs, Hearts, Spades]
SUITS = {str(suit()): suit for suit in SUITS_LIST}
class Card:
def __setattr__(self, *args):
raise TypeError('can\'t set attribute')
def __delattr__(self, *args):
raise TypeError('can\'t delete attribute')
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', 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)
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.__card_buffer = deque(collection)
else:
self.__card_buffer = deque()
def __len__(self):
return len(self.__card_buffer)
def __getitem__(self, index):
return self.__card_buffer[index]
def __str__(self):
- representation = "["
- for i in range(len(self.__card_buffer)-1):
- representation += str(self.__card_buffer[i]) + ", "
- representation += str(self.__card_buffer[-1]) + "]"
- return representation
+ return "[" + ', '.join(map(str, self)) + "]"
def draw(self, index):
drawn = self[index]
self.__card_buffer.remove(drawn)
return drawn
def draw_from_top(self):
return self.__card_buffer.pop()
def draw_from_bottom(self):
return self.__card_buffer.popleft()
def top_card(self):
return self.__card_buffer[-1]
def bottom_card(self):
return self.__card_buffer[0]
def add(self, card):
self.__card_buffer.append(card)
def index(self, card):
for i, element in enumerate(self.__card_buffer):
if card == element:
return i
raise ValueError("<Card {}> is not in the list".format(card))
def StandardDeck():
return CardCollection(Card(rank, suit) for suit in SUITS_LIST
for rank in RANKS_LIST)
def BeloteDeck():
def is_belote_card(card):
return card.rank not in [rank() for rank in RANKS_LIST[7:12]]
return CardCollection(card for card in StandardDeck()
if is_belote_card(card))
def SixtySixDeck():
def is_sixty_six_card(card):
return card.rank not in [rank() for rank in RANKS_LIST[5:12]]
return CardCollection(card for card in StandardDeck()
if is_sixty_six_card(card))

Божидар обнови решението на 24.03.2014 13:44 (преди над 10 години)

from collections import deque
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
class Ace(Rank):
- def __init__(self, symbol='A'):
- pass
+ def __init__(self):
+ Rank.__init__(self, 'A')
class Two(Rank):
- def __init__(self, symbol='2'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '2')
class Three(Rank):
- def __init__(self, symbol='3'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '3')
class Four(Rank):
- def __init__(self, symbol='4'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '4')
class Five(Rank):
- def __init__(self, symbol='5'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '5')
class Six(Rank):
- def __init__(self, symbol='6'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '6')
class Seven(Rank):
- def __init__(self, symbol='7'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '7')
class Eight(Rank):
- def __init__(self, symbol='8'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '8')
class Nine(Rank):
- def __init__(self, symbol='9'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '9')
class Ten(Rank):
- def __init__(self, symbol='10'):
- pass
+ def __init__(self):
+ Rank.__init__(self, '10')
class Jack(Rank):
- def __init__(self, symbol='J'):
- pass
+ def __init__(self):
+ Rank.__init__(self, 'J')
class Queen(Rank):
- def __init__(self, symbol='Q'):
- pass
+ def __init__(self):
+ Rank.__init__(self, 'Q')
class King(Rank):
- def __init__(self, symbol='K'):
- pass
+ def __init__(self):
+ Rank.__init__(self, 'K')
-
RANKS_LIST = [King, Queen, Jack, Ten, Nine, Eight,
Seven, Six, Five, Four, Three, Two, Ace]
RANKS = {str(rank()): rank for rank in RANKS_LIST}
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
class Hearts(Suit):
- def __init__(self, symbol="red"):
- pass
+ def __init__(self):
+ Suit.__init__(self, "red")
class Clubs(Suit):
- def __init__(self, symbol="black"):
- pass
+ def __init__(self):
+ Suit.__init__(self, "black")
class Spades(Suit):
- def __init__(self, symbol="black"):
- pass
+ def __init__(self):
+ Suit.__init__(self, "black")
class Diamonds(Suit):
- def __init__(self, symbol="red"):
- pass
+ def __init__(self):
+ Suit.__init__(self, "red")
SUITS_LIST = [Diamonds, Clubs, Hearts, Spades]
SUITS = {str(suit()): suit for suit in SUITS_LIST}
class Card:
def __setattr__(self, *args):
raise TypeError('can\'t set attribute')
def __delattr__(self, *args):
raise TypeError('can\'t delete attribute')
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', 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)
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.__card_buffer = deque(collection)
else:
self.__card_buffer = deque()
def __len__(self):
return len(self.__card_buffer)
def __getitem__(self, index):
return self.__card_buffer[index]
def __str__(self):
return "[" + ', '.join(map(str, self)) + "]"
def draw(self, index):
drawn = self[index]
self.__card_buffer.remove(drawn)
return drawn
def draw_from_top(self):
return self.__card_buffer.pop()
def draw_from_bottom(self):
return self.__card_buffer.popleft()
def top_card(self):
return self.__card_buffer[-1]
def bottom_card(self):
return self.__card_buffer[0]
def add(self, card):
self.__card_buffer.append(card)
def index(self, card):
for i, element in enumerate(self.__card_buffer):
if card == element:
return i
raise ValueError("<Card {}> is not in the list".format(card))
def StandardDeck():
return CardCollection(Card(rank, suit) for suit in SUITS_LIST
for rank in RANKS_LIST)
def BeloteDeck():
def is_belote_card(card):
return card.rank not in [rank() for rank in RANKS_LIST[7:12]]
return CardCollection(card for card in StandardDeck()
if is_belote_card(card))
def SixtySixDeck():
def is_sixty_six_card(card):
return card.rank not in [rank() for rank in RANKS_LIST[5:12]]
return CardCollection(card for card in StandardDeck()
if is_sixty_six_card(card))
  • Не знам какво имаш предвид за init. Прбвах нещо(във втората версия е, ама не заработи).
    Аз искам да ми е кодиран/а всеки символ/боя при създаване на обекта без да се подава всеки път.
    Как да стане без init?
  • използвам object.__setattr__(self, 'rank', rank()), защото ако използвам self.rank = rank(),
    ще трябва да усложня setattr за да имплементирам immutability. Може да има друг метод и да не знам.
  • Card.str го промених. Дано да е по-четим сега

Божидар обнови решението на 24.03.2014 16:54 (преди над 10 години)

-from collections import deque
+from collections import deque, OrderedDict
+from types import new_class
+def ClassFactory(name, arg, base):
+ def __init__(self):
+ base.__init__(self, arg)
+ return type(name, (base,), {"__init__": __init__})
+
+
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
+RANKS_DATA = [("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')]
+RANKS = OrderedDict([(name, ClassFactory(name, symbol, Rank))
+ for name, symbol in RANKS_DATA])
+globals().update(RANKS)
-class Ace(Rank):
- def __init__(self):
- Rank.__init__(self, 'A')
-
-class Two(Rank):
- def __init__(self):
- Rank.__init__(self, '2')
-
-
-class Three(Rank):
- def __init__(self):
- Rank.__init__(self, '3')
-
-
-class Four(Rank):
- def __init__(self):
- Rank.__init__(self, '4')
-
-
-class Five(Rank):
- def __init__(self):
- Rank.__init__(self, '5')
-
-
-class Six(Rank):
- def __init__(self):
- Rank.__init__(self, '6')
-
-
-class Seven(Rank):
- def __init__(self):
- Rank.__init__(self, '7')
-
-
-class Eight(Rank):
- def __init__(self):
- Rank.__init__(self, '8')
-
-
-class Nine(Rank):
- def __init__(self):
- Rank.__init__(self, '9')
-
-
-class Ten(Rank):
- def __init__(self):
- Rank.__init__(self, '10')
-
-
-class Jack(Rank):
- def __init__(self):
- Rank.__init__(self, 'J')
-
-
-class Queen(Rank):
- def __init__(self):
- Rank.__init__(self, 'Q')
-
-
-class King(Rank):
- def __init__(self):
- Rank.__init__(self, 'K')
-
-RANKS_LIST = [King, Queen, Jack, Ten, Nine, Eight,
- Seven, Six, Five, Four, Three, Two, Ace]
-RANKS = {str(rank()): rank for rank in RANKS_LIST}
-
-
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
+SUITS_DATA = [("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
+ ("Spades", "black")]
+SUITS = OrderedDict([(name, ClassFactory(name, color, Suit))
+ for name, color in SUITS_DATA])
+globals().update(SUITS)
-class Hearts(Suit):
- def __init__(self):
- Suit.__init__(self, "red")
-
-class Clubs(Suit):
- def __init__(self):
- Suit.__init__(self, "black")
-
-
-class Spades(Suit):
- def __init__(self):
- Suit.__init__(self, "black")
-
-
-class Diamonds(Suit):
- def __init__(self):
- Suit.__init__(self, "red")
-
-SUITS_LIST = [Diamonds, Clubs, Hearts, Spades]
-SUITS = {str(suit()): suit for suit in SUITS_LIST}
-
-
class Card:
def __setattr__(self, *args):
raise TypeError('can\'t set attribute')
def __delattr__(self, *args):
raise TypeError('can\'t delete attribute')
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', 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)
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.__card_buffer = deque(collection)
else:
self.__card_buffer = deque()
def __len__(self):
return len(self.__card_buffer)
def __getitem__(self, index):
return self.__card_buffer[index]
def __str__(self):
return "[" + ', '.join(map(str, self)) + "]"
def draw(self, index):
drawn = self[index]
self.__card_buffer.remove(drawn)
return drawn
def draw_from_top(self):
return self.__card_buffer.pop()
def draw_from_bottom(self):
return self.__card_buffer.popleft()
def top_card(self):
return self.__card_buffer[-1]
def bottom_card(self):
return self.__card_buffer[0]
def add(self, card):
self.__card_buffer.append(card)
def index(self, card):
for i, element in enumerate(self.__card_buffer):
if card == element:
return i
raise ValueError("<Card {}> is not in the list".format(card))
def StandardDeck():
- return CardCollection(Card(rank, suit) for suit in SUITS_LIST
- for rank in RANKS_LIST)
+ return CardCollection(Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values())
def BeloteDeck():
def is_belote_card(card):
- return card.rank not in [rank() for rank in RANKS_LIST[7:12]]
+ return card.rank.symbol not in map(str, range(2, 7))
return CardCollection(card for card in StandardDeck()
if is_belote_card(card))
def SixtySixDeck():
def is_sixty_six_card(card):
- return card.rank not in [rank() for rank in RANKS_LIST[5:12]]
+ return card.rank.symbol not in map(str, range(2, 9))
return CardCollection(card for card in StandardDeck()
if is_sixty_six_card(card))

Божидар обнови решението на 24.03.2014 16:55 (преди над 10 години)

from collections import deque, OrderedDict
-from types import new_class
def ClassFactory(name, arg, base):
def __init__(self):
base.__init__(self, arg)
return type(name, (base,), {"__init__": __init__})
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
RANKS_DATA = [("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')]
RANKS = OrderedDict([(name, ClassFactory(name, symbol, Rank))
for name, symbol in RANKS_DATA])
globals().update(RANKS)
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
SUITS_DATA = [("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
("Spades", "black")]
SUITS = OrderedDict([(name, ClassFactory(name, color, Suit))
for name, color in SUITS_DATA])
globals().update(SUITS)
class Card:
def __setattr__(self, *args):
raise TypeError('can\'t set attribute')
def __delattr__(self, *args):
raise TypeError('can\'t delete attribute')
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', 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)
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.__card_buffer = deque(collection)
else:
self.__card_buffer = deque()
def __len__(self):
return len(self.__card_buffer)
def __getitem__(self, index):
return self.__card_buffer[index]
def __str__(self):
return "[" + ', '.join(map(str, self)) + "]"
def draw(self, index):
drawn = self[index]
self.__card_buffer.remove(drawn)
return drawn
def draw_from_top(self):
return self.__card_buffer.pop()
def draw_from_bottom(self):
return self.__card_buffer.popleft()
def top_card(self):
return self.__card_buffer[-1]
def bottom_card(self):
return self.__card_buffer[0]
def add(self, card):
self.__card_buffer.append(card)
def index(self, card):
for i, element in enumerate(self.__card_buffer):
if card == element:
return i
raise ValueError("<Card {}> is not in the list".format(card))
def StandardDeck():
return CardCollection(Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values())
def BeloteDeck():
def is_belote_card(card):
return card.rank.symbol not in map(str, range(2, 7))
return CardCollection(card for card in StandardDeck()
if is_belote_card(card))
def SixtySixDeck():
def is_sixty_six_card(card):
return card.rank.symbol not in map(str, range(2, 9))
return CardCollection(card for card in StandardDeck()
if is_sixty_six_card(card))

Божидар обнови решението на 24.03.2014 21:47 (преди над 10 години)

from collections import deque, OrderedDict
def ClassFactory(name, arg, base):
def __init__(self):
base.__init__(self, arg)
return type(name, (base,), {"__init__": __init__})
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
RANKS_DATA = [("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')]
RANKS = OrderedDict([(name, ClassFactory(name, symbol, Rank))
for name, symbol in RANKS_DATA])
-globals().update(RANKS)
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.__class__.__name__
SUITS_DATA = [("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
("Spades", "black")]
SUITS = OrderedDict([(name, ClassFactory(name, color, Suit))
for name, color in SUITS_DATA])
-globals().update(SUITS)
class Card:
- def __setattr__(self, *args):
- raise TypeError('can\'t set attribute')
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
- def __delattr__(self, *args):
+ @property
+ def rank(self):
+ return self.__rank
+
+ @rank.setter
+ def rank(self, rank):
raise TypeError('can\'t delete attribute')
- def __init__(self, rank, suit):
- object.__setattr__(self, 'rank', rank())
- object.__setattr__(self, 'suit', suit())
+ @property
+ def suit(self):
+ return self.__suit
+
+ @suit.setter
+ def suit(self, suit):
+ raise TypeError('can\'t set attribute')
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)
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.__card_buffer = deque(collection)
else:
self.__card_buffer = deque()
def __len__(self):
return len(self.__card_buffer)
def __getitem__(self, index):
return self.__card_buffer[index]
def __str__(self):
return "[" + ', '.join(map(str, self)) + "]"
def draw(self, index):
drawn = self[index]
self.__card_buffer.remove(drawn)
return drawn
def draw_from_top(self):
return self.__card_buffer.pop()
def draw_from_bottom(self):
return self.__card_buffer.popleft()
def top_card(self):
return self.__card_buffer[-1]
def bottom_card(self):
return self.__card_buffer[0]
def add(self, card):
self.__card_buffer.append(card)
def index(self, card):
for i, element in enumerate(self.__card_buffer):
if card == element:
return i
raise ValueError("<Card {}> is not in the list".format(card))
def StandardDeck():
return CardCollection(Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values())
def BeloteDeck():
def is_belote_card(card):
return card.rank.symbol not in map(str, range(2, 7))
return CardCollection(card for card in StandardDeck()
if is_belote_card(card))
def SixtySixDeck():
def is_sixty_six_card(card):
return card.rank.symbol not in map(str, range(2, 9))
return CardCollection(card for card in StandardDeck()
if is_sixty_six_card(card))