Решение на Тесте карти от Любомир Коев

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

Към профила на Любомир Коев

Резултати

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

Код

from collections import deque
# key -> (symbol, order value)
RANK_INFO = {'King': ('K', 1), 'Queen': ('Q', 2), 'Jack': ('J', 3),
'Ten': ('10', 4), 'Nine': ('9', 5), 'Eight': ('8', 6),
'Seven': ('7', 7), 'Six': ('6', 8), 'Five': ('5', 9),
'Four': ('4', 10), 'Three': ('3', 11), 'Two': ('2', 12),
'Ace': ('A', 13)}
# key -> (color, order_value)
SUIT_INFO = {'Diamonds': ('red', 1), 'Clubs': ('black', 2),
'Hearts': ('red', 3), 'Spades': ('black', 4)}
class Rank():
def __init__(self, symbol):
self.symbol = symbol
class Suit():
def __init__(self, color):
self.color = color
def get_rank_prototype(name):
"""dict for derivatives of Rank"""
return {
'__init__': lambda self: Rank.__init__(self, RANK_INFO[name][0]),
'__eq__': lambda self, other: type(self) == type(other),
'__str__': lambda self: name,
'__lt__': lambda self, other:
RANK_INFO[str(self)][1] < RANK_INFO[str(other)][1],
'__le__': lambda self, other: self < other or self == other
}
RANKS = {name: type(name, (Rank, ), get_rank_prototype(name))
for name in RANK_INFO.keys()}
def get_suit_prototype(name):
"""dict for derivatives of Suit"""
return {
'__init__': lambda self: Rank.__init__(self, SUIT_INFO[name][0]),
'__eq__': lambda self, other: type(self) == type(other),
'__str__': lambda self: name,
'__lt__': lambda self, other:
SUIT_INFO[str(self)][1] < SUIT_INFO[str(other)][1]
}
SUITS = {name: type(name, (Suit, ), get_suit_prototype(name))
for name in SUIT_INFO.keys()}
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', suit())
def __setattr__(self):
raise AttributeError
def __delattr__(self):
raise AttributeError
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __lt__(self, other):
if self.suit == other.suit:
return self.rank < other.rank
return self.suit < other.suit
class CardCollection:
def __init__(self, collection=[]):
self.__cards = list(collection)
def draw(self, index):
return self.__cards.pop(index)
def draw_from_top(self):
return self.__cards.pop()
def draw_from_bottom(self):
return self.__cards.pop(0)
def top_card(self):
return self.__cards[-1]
def bottom_card(self):
return self.__cards[0]
def add(self, card):
return self.__cards.append(card)
def index(self, card):
return self.__cards.index(card)
def __len__(self):
return len(self.__cards)
def __getitem__(self, index):
return self.__cards[index]
def card_rank_range(rank_low=RANKS['King'](), rank_high=RANKS['Ace']()):
return sorted([Card(rank, suit)
for rank in RANKS.values()
for suit in SUITS.values()
if rank_low <= rank() <= rank_high])
def StandardDeck():
return card_rank_range()
def BeloteDeck():
return sorted(
card_rank_range(RANKS['King'](), RANKS['Seven']()) +
card_rank_range(RANKS['Ace'](), RANKS['Ace']())
)
def SixtySixDeck():
return sorted(
card_rank_range(RANKS['King'](), RANKS['Nine']()) +
card_rank_range(RANKS['Ace'](), RANKS['Ace']())
)

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.031s

OK

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

Любомир обнови решението на 24.03.2014 04:28 (преди около 10 години)

+from collections import deque
+
+# key -> (symbol, order value)
+RANK_INFO = {'King': ('K', 1), 'Queen': ('Q', 2), 'Jack': ('J', 3),
+ 'Ten': ('10', 4), 'Nine': ('9', 5), 'Eight': ('8', 6),
+ 'Seven': ('7', 7), 'Six': ('6', 8), 'Five': ('5', 9),
+ 'Four': ('4', 10), 'Three': ('3', 11), 'Two': ('2', 12),
+ 'Ace': ('A', 13)}
+
+# key -> (color, order_value)
+SUIT_INFO = {'Diamonds': ('red', 1), 'Clubs': ('black', 2),
+ 'Hearts': ('red', 3), 'Spades': ('black', 4)}
+
+
+class Rank():
+
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+
+class Suit():
+
+ def __init__(self, color):
+ self.color = color
+
+
+def get_rank_prototype(name):
+ """dict for derivatives of Rank"""
+ return {
+ '__init__': lambda self: Rank.__init__(self, RANK_INFO[name][0]),
+ '__eq__': lambda self, other: type(self) == type(other),
+ '__str__': lambda self: name,
+ '__lt__': lambda self, other:
+ RANK_INFO[str(self)][1] < RANK_INFO[str(other)][1],
+ '__gt__': lambda self, other: not self < other and not self == other
+ }
+
+RANKS = {name: type(name, (Rank, ), get_rank_prototype(name))
+ for name in RANK_INFO.keys()}
+
+
+def get_suit_prototype(name):
+ """dict for derivatives of Suit"""
+ return {
+ '__init__': lambda self: Rank.__init__(self, SUIT_INFO[name][0]),
+ '__eq__': lambda self, other: type(self) == type(other),
+ '__str__': lambda self: name,
+ '__lt__': lambda self, other:
+ SUIT_INFO[str(self)][1] < SUIT_INFO[str(other)][1]
+ }
+
+SUITS = {name: type(name, (Suit, ), get_suit_prototype(name))
+ for name in SUIT_INFO.keys()}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __setattr__(self):
+ raise AttributeError
+
+ def __delattr__(self):
+ raise AttributeError
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
+
+ def __lt__(self, other):
+ if self.suit == other.suit:
+ return self.rank < other.rank
+ return self.suit < other.suit
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.__cards = deque(collection)
+
+ def draw(self):
+ return self.draw_from_top()
+
+ def draw_from_top(self):
+ return self.__cards.pop()
+
+ def draw_from_bottom(self):
+ return self.__cards.popleft()
+
+ def top_card(self):
+ return self.__cards[-1]
+
+ def bottom_card(self):
+ return self.__cards[0]
+
+ def add(self, card):
+ return self.__cards.append(card)
+
+ def index(self, card):
+ for index in range(0, len(self.__cards)):
+ if self.__cards[index] == card:
+ return index
+ raise ValueError
+
+ def __len__(self):
+ return len(self.__cards)
+
+ def __getitem__(self, index):
+ return self.__cards[index]
+
+
+def StandardDeck():
+ return sorted([Card(rank, suit)
+ for rank in RANKS.values()
+ for suit in SUITS.values()])
+
+
+def BeloteDeck():
+ return sorted([card for card in StandardDeck()
+ if card.rank < RANKS['Six']()
+ or card.rank == RANKS['Ace']()])
+
+
+def SixtySixDeck():
+ return sorted([card for card in StandardDeck()
+ if card.rank < RANKS['Eight']()
+ or card.rank == RANKS['Ace']()])

Любомир обнови решението на 25.03.2014 20:40 (преди около 10 години)

from collections import deque
# key -> (symbol, order value)
RANK_INFO = {'King': ('K', 1), 'Queen': ('Q', 2), 'Jack': ('J', 3),
'Ten': ('10', 4), 'Nine': ('9', 5), 'Eight': ('8', 6),
'Seven': ('7', 7), 'Six': ('6', 8), 'Five': ('5', 9),
'Four': ('4', 10), 'Three': ('3', 11), 'Two': ('2', 12),
'Ace': ('A', 13)}
# key -> (color, order_value)
SUIT_INFO = {'Diamonds': ('red', 1), 'Clubs': ('black', 2),
'Hearts': ('red', 3), 'Spades': ('black', 4)}
class Rank():
def __init__(self, symbol):
self.symbol = symbol
class Suit():
def __init__(self, color):
self.color = color
def get_rank_prototype(name):
"""dict for derivatives of Rank"""
return {
'__init__': lambda self: Rank.__init__(self, RANK_INFO[name][0]),
'__eq__': lambda self, other: type(self) == type(other),
'__str__': lambda self: name,
'__lt__': lambda self, other:
RANK_INFO[str(self)][1] < RANK_INFO[str(other)][1],
- '__gt__': lambda self, other: not self < other and not self == other
+ '__le__': lambda self, other: self < other or self == other
}
RANKS = {name: type(name, (Rank, ), get_rank_prototype(name))
for name in RANK_INFO.keys()}
def get_suit_prototype(name):
"""dict for derivatives of Suit"""
return {
'__init__': lambda self: Rank.__init__(self, SUIT_INFO[name][0]),
'__eq__': lambda self, other: type(self) == type(other),
'__str__': lambda self: name,
'__lt__': lambda self, other:
SUIT_INFO[str(self)][1] < SUIT_INFO[str(other)][1]
}
SUITS = {name: type(name, (Suit, ), get_suit_prototype(name))
for name in SUIT_INFO.keys()}
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', suit())
def __setattr__(self):
raise AttributeError
def __delattr__(self):
raise AttributeError
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __lt__(self, other):
if self.suit == other.suit:
return self.rank < other.rank
return self.suit < other.suit
class CardCollection:
def __init__(self, collection=[]):
- self.__cards = deque(collection)
+ self.__cards = list(collection)
- def draw(self):
- return self.draw_from_top()
+ def draw(self, index):
+ return self.__cards.pop(index)
def draw_from_top(self):
return self.__cards.pop()
def draw_from_bottom(self):
- return self.__cards.popleft()
+ return self.__cards.pop(0)
def top_card(self):
return self.__cards[-1]
def bottom_card(self):
return self.__cards[0]
def add(self, card):
return self.__cards.append(card)
def index(self, card):
- for index in range(0, len(self.__cards)):
- if self.__cards[index] == card:
- return index
- raise ValueError
+ return self.__cards.index(card)
def __len__(self):
return len(self.__cards)
def __getitem__(self, index):
return self.__cards[index]
-def StandardDeck():
+def card_rank_range(rank_low=RANKS['King'](), rank_high=RANKS['Ace']()):
return sorted([Card(rank, suit)
for rank in RANKS.values()
- for suit in SUITS.values()])
+ for suit in SUITS.values()
+ if rank_low <= rank() <= rank_high])
+def StandardDeck():
+ return card_rank_range()
+
+
def BeloteDeck():
- return sorted([card for card in StandardDeck()
- if card.rank < RANKS['Six']()
- or card.rank == RANKS['Ace']()])
+ return sorted(
+ card_rank_range(RANKS['King'](), RANKS['Seven']()) +
+ card_rank_range(RANKS['Ace'](), RANKS['Ace']())
+ )
def SixtySixDeck():
- return sorted([card for card in StandardDeck()
- if card.rank < RANKS['Eight']()
+ return sorted(
- or card.rank == RANKS['Ace']()])
+ card_rank_range(RANKS['King'](), RANKS['Nine']()) +
+ card_rank_range(RANKS['Ace'](), RANKS['Ace']())
+ )