Любомир обнови решението на 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']()])
- Прочети си внимателно условието, draw премахна картата на подаден индекс
- Последните ти три функции доста си приличат, дали не можеш да намериш начин да преизползваш кода си?