Петър обнови решението на 26.03.2014 01:16 (преди над 10 години)
+import collections
+from functools import cmp_to_key
+
+
+RANK_SYMBOLS_PRIORITIES = [('A', 'Ace', 1), ('K', 'King', 13),
+ ('Q', 'Queen', 12), ('J', 'Jack', 11),
+ ('10', 'Ten', 10), ('9', 'Nine', 9),
+ ('8', 'Eight', 8), ('7', 'Seven', 7),
+ ('6', 'Six', 6), ('5', 'Five', 5),
+ ('4', 'Four', 4), ('3', 'Three', 3),
+ ('2', 'Two', 2)]
+
+
+SUIT_COLORS_PRIORITIES = [('Diamonds', 'red', 39), ('Clubs', 'black', 26),
+ ('Hearts', 'red', 13), ('Spades', 'black', 0)]
+
+
+RANKS = {}
+
+
+SUITS = {}
+
+
+class Rank():
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+for rank in RANK_SYMBOLS_PRIORITIES:
+ RANKS[rank[1]] = type(rank[1], (Rank, ), {'symbol': rank[0],
+ 'priority': rank[2]})
+
+
+class Suit():
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+for suit in SUIT_COLORS_PRIORITIES:
+ SUITS[suit[0]] = type(suit[0], (Rank, ), {'color': suit[1],
+ 'priority': suit[2]})
+
+
+class Card():
+ def __init__(self, rank, suit):
+ self._rank = rank(rank.symbol)
+ self._suit = suit(suit.color)
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ @property
+ def priority(self):
+ return self._rank.priority + self._suit.priority
+
+ def __str__(self):
+ return "{} of {}".format(self.rank.__name__, self.suit.__name__)
+
+ def __eq__(self, other):
+ return self._rank == other.rank and self._suit == other.suit
+
+
+class CardCollection(collections.deque):
+ def __init__(self, collection):
+ for card in collection:
+ self.append(card)
+
+ def draw(self, index):
+ result = self[index]
+ del(self[index])
+ return result
+
+ def draw_from_top(self):
+ return self.pop()
+
+ def draw_from_bottom(self):
+ return self.popleft()
+
+ def top_card(self):
+ return self[len(self) - 1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def add(self, card):
+ self.append(card)
+
+ def index(self, card):
+ for i in range(0, len(self)):
+ if self[i] == card:
+ return i
+ raise ValueError
+
+ def sort_cards(self):
+ self = sorted(self, key = cmp_to_key(lambda x, y:
+ x.priority - y.priority))