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

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

Към профила на Марио Даскалов

Резултати

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

Код

from itertools import product
def _class_type_equality(self, other):
return isinstance(other, type(self))
def _stringify_class_name(self):
return type(self).__name__
def _dict_class(parent, klass_names, symbol_name, symbols):
dict_class = {}
for klass_name, symbol in zip(klass_names, symbols):
fields = {'__eq__': _class_type_equality,
'__str__': _stringify_class_name,
symbol_name: symbol}
dict_class[klass_name] = type(klass_name, (parent,), fields)
return dict_class
class Rank:
pass
_RANK_VALUES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
_RANK_SYMBOLS = ['K', 'Q', 'J', '10', '9', '8',
'7', '6', '5', '4', '3', '2', 'A']
RANKS = _dict_class(Rank, _RANK_VALUES, 'symbol', _RANK_SYMBOLS)
class Suit:
pass
_SUIT_VALUES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
_SUIT_SYMBOLS = ['red', 'black', 'red', 'black']
SUITS = _dict_class(Suit, _SUIT_VALUES, 'color', _SUIT_SYMBOLS)
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
if isinstance(other, type(self)):
return self.rank == other.rank and self.suit == other.suit
else:
return False
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
class CardCollection(list):
add = list.append
draw_from_top = list.pop
draw = list.pop
index = list.index
def top_card(self):
return self[-1]
def bottom_card(self):
return self[0]
def draw_from_bottom(self):
return self.pop(0)
def _pair_to_card(pair):
return Card(RANKS[pair[1]], SUITS[pair[0]])
def _deck_from_rank_strings(ranks):
collection = [_pair_to_card(pair) for pair in product(_SUIT_VALUES, ranks)]
return CardCollection(collection)
def StandardDeck():
return _deck_from_rank_strings(_RANK_VALUES)
def BeloteDeck():
return _deck_from_rank_strings(_RANK_VALUES[:7] + [_RANK_VALUES[-1]])
def SixtySixDeck():
return _deck_from_rank_strings(_RANK_VALUES[:5] + [_RANK_VALUES[-1]])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.017s

OK

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

Марио обнови решението на 26.03.2014 00:53 (преди над 10 години)

+from itertools import product
+
+
+def class_type_equality(self, other):
+ return isinstance(other, type(self))
+
+
+def stringify_class_name(self):
+ return type(self).__name__
+
+
+TYPE_BASED_METHODS = {'__eq__': class_type_equality,
+ '__str__': stringify_class_name}
+
+
+class Rank:
+ pass
+
+RANK_VALUES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+RANKS = {rank: type(rank, (Rank,), TYPE_BASED_METHODS) for rank in RANK_VALUES}
+
+
+class Suit:
+ pass
+
+SUIT_VALUES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUITS = {suit: type(suit, (Suit,), TYPE_BASED_METHODS) for suit in SUIT_VALUES}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ if isinstance(other, type(self)):
+ return self.rank == other.rank and self.suit == other.suit
+ else:
+ return False
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+
+class CardCollection(list):
+ add = list.append
+ draw_from_top = list.pop
+ draw = list.pop
+ index = list.index
+
+ def top_card(self):
+ return self[-1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def draw_from_bottom(self):
+ return self.pop(0)
+
+
+def pair_to_card(pair):
+ return Card(RANKS[pair[1]], SUITS[pair[0]])
+
+
+def deck_from_rank_strings(ranks):
+ collection = [pair_to_card(pair) for pair in product(SUIT_VALUES, ranks)]
+ return CardCollection(collection)
+
+
+def StandardDeck():
+ return deck_from_rank_strings(RANK_VALUES)
+
+
+def BelotteDeck():
+ return deck_from_rank_strings(RANK_VALUES[:7] + [RANK_VALUES[-1]])
+
+
+def SixtySixDeck():
+ return deck_from_rank_strings(RANK_VALUES[:5] + [RANK_VALUES[-1]])

Марио обнови решението на 26.03.2014 02:05 (преди над 10 години)

from itertools import product
-def class_type_equality(self, other):
+def _class_type_equality(self, other):
return isinstance(other, type(self))
-def stringify_class_name(self):
+def _stringify_class_name(self):
return type(self).__name__
-TYPE_BASED_METHODS = {'__eq__': class_type_equality,
- '__str__': stringify_class_name}
+def _dict_class(parent, klass_names, symbol_name, symbols):
+ dict_class = {}
+ for klass_name, symbol in zip(klass_names, symbols):
+ fields = {'__eq__': _class_type_equality,
+ '__str__': _stringify_class_name,
+ symbol_name: symbol}
+ dict_class[klass_name] = type(klass_name, (parent,), fields)
+ return dict_class
class Rank:
pass
-RANK_VALUES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
-RANKS = {rank: type(rank, (Rank,), TYPE_BASED_METHODS) for rank in RANK_VALUES}
+_RANK_VALUES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+_RANK_SYMBOLS = ['K', 'Q', 'J', '10', '9', '8',
+ '7', '6', '5', '4', '3', '2', 'A']
+RANKS = _dict_class(Rank, _RANK_VALUES, 'symbol', _RANK_SYMBOLS)
class Suit:
pass
-SUIT_VALUES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-SUITS = {suit: type(suit, (Suit,), TYPE_BASED_METHODS) for suit in SUIT_VALUES}
+_SUIT_VALUES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+_SUIT_SYMBOLS = ['red', 'black', 'red', 'black']
+SUITS = _dict_class(Suit, _SUIT_VALUES, 'color', _SUIT_SYMBOLS)
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
if isinstance(other, type(self)):
return self.rank == other.rank and self.suit == other.suit
else:
return False
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
class CardCollection(list):
add = list.append
draw_from_top = list.pop
draw = list.pop
index = list.index
def top_card(self):
return self[-1]
def bottom_card(self):
return self[0]
def draw_from_bottom(self):
return self.pop(0)
-def pair_to_card(pair):
+def _pair_to_card(pair):
return Card(RANKS[pair[1]], SUITS[pair[0]])
-def deck_from_rank_strings(ranks):
- collection = [pair_to_card(pair) for pair in product(SUIT_VALUES, ranks)]
+def _deck_from_rank_strings(ranks):
+ collection = [_pair_to_card(pair) for pair in product(_SUIT_VALUES, ranks)]
return CardCollection(collection)
def StandardDeck():
- return deck_from_rank_strings(RANK_VALUES)
+ return _deck_from_rank_strings(_RANK_VALUES)
-def BelotteDeck():
- return deck_from_rank_strings(RANK_VALUES[:7] + [RANK_VALUES[-1]])
+def BeloteDeck():
+ return _deck_from_rank_strings(_RANK_VALUES[:7] + [_RANK_VALUES[-1]])
def SixtySixDeck():
- return deck_from_rank_strings(RANK_VALUES[:5] + [RANK_VALUES[-1]])
+ return _deck_from_rank_strings(_RANK_VALUES[:5] + [_RANK_VALUES[-1]])