Решение на Тесте карти от Евгени Кунев

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

Към профила на Евгени Кунев

Резултати

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

Код

import collections
class CommonMixin:
def __str__(self):
return self.__class__.__name__
def __lt__(self, other):
return self.order < other.order
class Rank(CommonMixin):
def __eq__(self, other):
return self.rank == other.rank
class Two(Rank):
rank, order = '2', 2
class Three(Rank):
rank, order = '3', 3
class Four(Rank):
rank, order = '4', 4
class Five(Rank):
rank, order = '5', 5
class Six(Rank):
rank, order = '6', 6
class Seven(Rank):
rank, order = '7', 7
class Eight(Rank):
rank, order = '8', 8
class Nine(Rank):
rank, order = '9', 9
class Ten(Rank):
rank, order = '10', 10
class Jack(Rank):
rank, order = 'J', 11
class Queen(Rank):
rank, order = 'Q', 12
class King(Rank):
rank, order = 'K', 13
class Ace(Rank):
rank, order = 'A', 1
class Suit(CommonMixin):
def __eq__(self, other):
return type(self) == type(other)
class Diamonds(Suit):
color, order = 'red', 4
class Clubs(Suit):
color, order = 'black', 3
class Hearts(Suit):
color, order = 'red', 2
class Spades(Suit):
color, order = 'black', 1
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __repr__(self):
return "<Card {}>".format(self)
def __lt__(self, other):
return (self.suit, self.rank) < (other.suit, other.rank)
class CardCollection:
def __init__(self, collection=[]):
self.collection = collections.deque(collection)
def draw(self, index):
card = self.collection[index]
self.collection.remove(card)
return card
def draw_from_bottom(self):
return self.collection.popleft()
def draw_from_top(self):
return self.collection.pop()
def index(self, card):
return list(self.collection).index(card)
def add(self, card):
self.collection.append(card)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
RANKS = {
'Two': Two,
'Three': Three,
'Four': Four,
'Five': Five,
'Six': Six,
'Seven': Seven,
'Eight': Eight,
'Nine': Nine,
'Ten': Ten,
'Jack': Jack,
'Queen': Queen,
'King': King,
'Ace': Ace,
}
SUITS = {
'Diamonds': Diamonds,
'Spades': Spades,
'Hearts': Hearts,
'Clubs': Clubs,
}
def StandardDeck():
return list(sorted((
Card(rank, suit)
for rank in RANKS.values()
for suit in SUITS.values()
), reverse=True))
def BeloteDeck():
return [card for card in StandardDeck()
if card.rank.order > Six.order or isinstance(card.rank, Ace)]
def SixtySixDeck():
return [card for card in StandardDeck()
if card.rank.order > Eight.order or isinstance(card.rank, Ace)]

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.022s

OK

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

Евгени обнови решението на 20.03.2014 21:41 (преди над 10 години)

+import collections
+
+
+class CommonMixin:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __lt__(self, other):
+ return self.order < other.order
+
+
+class Rank(CommonMixin):
+ def __eq__(self, other):
+ return self.rank == other.rank
+
+
+class Two(Rank):
+ def __init__(self):
+ self.rank = '2'
+ self.order = 2
+
+
+class Three(Rank):
+ def __init__(self):
+ self.rank = '3'
+ self.order = 3
+
+
+class Four(Rank):
+ def __init__(self):
+ self.rank = '4'
+ self.order = 4
+
+
+class Five(Rank):
+ def __init__(self):
+ self.rank = '5'
+ self.order = 5
+
+
+class Six(Rank):
+ def __init__(self):
+ self.rank = '6'
+ self.order = 6
+
+
+class Seven(Rank):
+ def __init__(self):
+ self.rank = '7'
+ self.order = 7
+
+
+class Eight(Rank):
+ def __init__(self):
+ self.rank = '8'
+ self.order = 8
+
+
+class Nine(Rank):
+ def __init__(self):
+ self.rank = '9'
+ self.order = 9
+
+
+class Ten(Rank):
+ def __init__(self):
+ self.rank = '10'
+ self.order = 10
+
+
+class Jack(Rank):
+ def __init__(self):
+ self.rank = 'J'
+ self.order = 11
+
+
+class Queen(Rank):
+ def __init__(self):
+ self.rank = 'Q'
+ self.order = 12
+
+
+class King(Rank):
+ def __init__(self):
+ self.rank = 'K'
+ self.order = 13
+
+
+class Ace(Rank):
+ def __init__(self):
+ self.rank = 'A'
+ self.order = 1
+
+
+class Suit(CommonMixin):
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'red'
+ self.order = 4
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'black'
+ self.order = 3
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+ self.order = 2
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+ self.order = 1
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __eq__(self, other):
+ return (self.rank, self.suit) == (other.rank, other.suit)
+
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
+
+ def __repr__(self):
+ return "<Card {}>".format(self)
+
+ def __lt__(self, other):
+ return (self.suit, self.rank) < (other.suit, other.rank)
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collections.deque(collection)
+
+ def draw(self, index):
+ card = self.collection[index]
+ self.collection.remove(card)
+ return card
+
+ def draw_from_bottom(self):
+ return self.collection.popleft()
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def index(self, card):
+ return list(self.collection).index(card)
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __len__(self):
+ return len(self.collection)
+
+
+RANKS = {
+ 'Two': Two,
+ 'Three': Three,
+ 'Four': Four,
+ 'Five': Five,
+ 'Six': Six,
+ 'Seven': Seven,
+ 'Eight': Eight,
+ 'Nine': Nine,
+ 'Ten': Ten,
+ 'Jack': Jack,
+ 'Queen': Queen,
+ 'King': King,
+ 'Ace': Ace,
+}
+
+
+SUITS = {
+ 'Diamonds': Diamonds,
+ 'Spades': Spades,
+ 'Hearts': Hearts,
+ 'Clubs': Clubs,
+}
+
+
+def StandardDeck():
+ return list(sorted((
+ Card(rank, suit)
+ for rank in RANKS.values()
+ for suit in SUITS.values()
+ ), reverse=True))
+
+
+def BeloteDeck():
+ return list(sorted((
+ Card(rank, suit)
+ for rank in RANKS.values()
+ for suit in SUITS.values()
+ if rank not in [Two, Three, Four, Five, Six]
+ ), reverse=True))
+
+
+def SixtySixDeck():
+ return list(sorted((
+ Card(rank, suit)
+ for rank in RANKS.values()
+ for suit in SUITS.values()
+ if rank not in [Two, Three, Four, Five, Six, Seven, Eight]
+ ), reverse=True))

Евгени обнови решението на 21.03.2014 16:31 (преди над 10 години)

import collections
class CommonMixin:
def __str__(self):
return self.__class__.__name__
def __lt__(self, other):
return self.order < other.order
class Rank(CommonMixin):
def __eq__(self, other):
return self.rank == other.rank
class Two(Rank):
- def __init__(self):
- self.rank = '2'
- self.order = 2
+ rank, order = '2', 2
class Three(Rank):
- def __init__(self):
- self.rank = '3'
- self.order = 3
+ rank, order = '3', 3
class Four(Rank):
- def __init__(self):
- self.rank = '4'
- self.order = 4
+ rank, order = '4', 4
class Five(Rank):
- def __init__(self):
- self.rank = '5'
- self.order = 5
+ rank, order = '5', 5
class Six(Rank):
- def __init__(self):
- self.rank = '6'
- self.order = 6
+ rank, order = '6', 6
class Seven(Rank):
- def __init__(self):
- self.rank = '7'
- self.order = 7
+ rank, order = '7', 7
class Eight(Rank):
- def __init__(self):
- self.rank = '8'
- self.order = 8
+ rank, order = '8', 8
class Nine(Rank):
- def __init__(self):
- self.rank = '9'
- self.order = 9
+ rank, order = '9', 9
class Ten(Rank):
- def __init__(self):
- self.rank = '10'
- self.order = 10
+ rank, order = '10', 10
class Jack(Rank):
- def __init__(self):
- self.rank = 'J'
- self.order = 11
+ rank, order = 'J', 11
class Queen(Rank):
- def __init__(self):
- self.rank = 'Q'
- self.order = 12
+ rank, order = 'Q', 12
class King(Rank):
- def __init__(self):
- self.rank = 'K'
- self.order = 13
+ rank, order = 'K', 13
class Ace(Rank):
- def __init__(self):
- self.rank = 'A'
- self.order = 1
+ rank, order = 'A', 1
class Suit(CommonMixin):
def __eq__(self, other):
return type(self) == type(other)
class Diamonds(Suit):
- def __init__(self):
- self.color = 'red'
- self.order = 4
+ color, order = 'red', 4
class Clubs(Suit):
- def __init__(self):
- self.color = 'black'
- self.order = 3
+ color, order = 'black', 3
class Hearts(Suit):
- def __init__(self):
- self.color = 'red'
- self.order = 2
+ color, order = 'red', 2
class Spades(Suit):
- def __init__(self):
- self.color = 'black'
- self.order = 1
+ color, order = 'black', 1
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __repr__(self):
return "<Card {}>".format(self)
def __lt__(self, other):
return (self.suit, self.rank) < (other.suit, other.rank)
class CardCollection:
def __init__(self, collection=[]):
self.collection = collections.deque(collection)
def draw(self, index):
card = self.collection[index]
self.collection.remove(card)
return card
def draw_from_bottom(self):
return self.collection.popleft()
def draw_from_top(self):
return self.collection.pop()
def index(self, card):
return list(self.collection).index(card)
def add(self, card):
self.collection.append(card)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
RANKS = {
'Two': Two,
'Three': Three,
'Four': Four,
'Five': Five,
'Six': Six,
'Seven': Seven,
'Eight': Eight,
'Nine': Nine,
'Ten': Ten,
'Jack': Jack,
'Queen': Queen,
'King': King,
'Ace': Ace,
}
SUITS = {
'Diamonds': Diamonds,
'Spades': Spades,
'Hearts': Hearts,
'Clubs': Clubs,
}
def StandardDeck():
return list(sorted((
Card(rank, suit)
for rank in RANKS.values()
for suit in SUITS.values()
), reverse=True))
def BeloteDeck():
- return list(sorted((
- Card(rank, suit)
- for rank in RANKS.values()
- for suit in SUITS.values()
- if rank not in [Two, Three, Four, Five, Six]
- ), reverse=True))
+ return [card for card in StandardDeck()
+ if card.rank.order > Six.order or isinstance(card.rank, Ace)]
def SixtySixDeck():
- return list(sorted((
- Card(rank, suit)
+ return [card for card in StandardDeck()
- for rank in RANKS.values()
+ if card.rank.order > Eight.order or isinstance(card.rank, Ace)]
- for suit in SUITS.values()
- if rank not in [Two, Three, Four, Five, Six, Seven, Eight]
- ), reverse=True))

Евгени обнови решението на 21.03.2014 16:32 (преди над 10 години)

import collections
class CommonMixin:
def __str__(self):
return self.__class__.__name__
def __lt__(self, other):
return self.order < other.order
class Rank(CommonMixin):
def __eq__(self, other):
return self.rank == other.rank
class Two(Rank):
rank, order = '2', 2
class Three(Rank):
rank, order = '3', 3
class Four(Rank):
rank, order = '4', 4
class Five(Rank):
rank, order = '5', 5
class Six(Rank):
rank, order = '6', 6
class Seven(Rank):
rank, order = '7', 7
class Eight(Rank):
rank, order = '8', 8
class Nine(Rank):
rank, order = '9', 9
class Ten(Rank):
rank, order = '10', 10
class Jack(Rank):
rank, order = 'J', 11
class Queen(Rank):
rank, order = 'Q', 12
class King(Rank):
rank, order = 'K', 13
class Ace(Rank):
rank, order = 'A', 1
class Suit(CommonMixin):
def __eq__(self, other):
return type(self) == type(other)
class Diamonds(Suit):
color, order = 'red', 4
class Clubs(Suit):
color, order = 'black', 3
class Hearts(Suit):
color, order = 'red', 2
class Spades(Suit):
- color, order = 'black', 1
+ color, order = 'black', 1
class Card:
def __init__(self, rank, suit):
self._rank = rank()
self._suit = suit()
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __repr__(self):
return "<Card {}>".format(self)
def __lt__(self, other):
return (self.suit, self.rank) < (other.suit, other.rank)
class CardCollection:
def __init__(self, collection=[]):
self.collection = collections.deque(collection)
def draw(self, index):
card = self.collection[index]
self.collection.remove(card)
return card
def draw_from_bottom(self):
return self.collection.popleft()
def draw_from_top(self):
return self.collection.pop()
def index(self, card):
return list(self.collection).index(card)
def add(self, card):
self.collection.append(card)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
RANKS = {
'Two': Two,
'Three': Three,
'Four': Four,
'Five': Five,
'Six': Six,
'Seven': Seven,
'Eight': Eight,
'Nine': Nine,
'Ten': Ten,
'Jack': Jack,
'Queen': Queen,
'King': King,
'Ace': Ace,
}
SUITS = {
'Diamonds': Diamonds,
'Spades': Spades,
'Hearts': Hearts,
'Clubs': Clubs,
}
def StandardDeck():
return list(sorted((
Card(rank, suit)
for rank in RANKS.values()
for suit in SUITS.values()
), reverse=True))
def BeloteDeck():
return [card for card in StandardDeck()
if card.rank.order > Six.order or isinstance(card.rank, Ace)]
def SixtySixDeck():
return [card for card in StandardDeck()
if card.rank.order > Eight.order or isinstance(card.rank, Ace)]