Решение на Тесте карти от Александър Златков

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

Към профила на Александър Златков

Резултати

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

Код

class BaseCardData:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return str(self) == str(other)
class Rank(BaseCardData):
def __init__(self, symbol):
self.symbol = symbol
class Suit(BaseCardData):
def __init__(self, color):
self.color = color
def create_class(class_name, base_class, property_name, property_value):
return type(
class_name,
(base_class,),
{property_name: property_value, '__init__': BaseCardData.__init__}
)
SUITS_ORDERED = [
('Diamonds', 'red'), ('Clubs', 'black'),
('Hearts', 'red'), ('Spades', 'black')
]
RANKS_ORDERED = [
('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'), ('Ten', '10'), ('Nine', '9'),
('Eight', '8'), ('Seven', '7'), ('Six', '6'), ('Five', '5'), ('Four', '4'),
('Three', '3'), ('Two', '2'), ('Ace', 'A')
]
RANKS = {
key: create_class(key, Rank, 'symbol', value)
for key, value in RANKS_ORDERED
}
SUITS = {
key: create_class(key, Suit, 'color', value)
for key, value in SUITS_ORDERED
}
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 __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self._cards = []
for card in collection:
self.add(card)
def draw(self, index):
return self._cards.pop(index)
def draw_from_top(self):
return self._cards.pop(len(self._cards) - 1)
def draw_from_bottom(self):
return self._cards.pop(0)
def top_card(self):
return self._cards[len(self._cards) - 1]
def bottom_card(self):
return self._cards[0]
def add(self, card):
self._cards.append(card)
def index(self, card):
cards_count = len(self._cards)
for i in range(0, cards_count):
if self._cards[i] == card:
return i
raise ValueError('Cound not find the specified card.')
def __getitem__(self, index):
return self._cards[index]
def __len__(self):
return len(self._cards)
def generate_deck(ranks_to_filter=[]):
cards = CardCollection([])
for suit in [element[0] for element in SUITS_ORDERED]:
for rank in [element[0] for element in RANKS_ORDERED]:
if rank not in ranks_to_filter:
cards.add(Card(RANKS[rank], SUITS[suit]))
return cards
def StandardDeck():
return generate_deck()
def BeloteDeck():
return generate_deck(['Two', 'Three', 'Four', 'Five', 'Six'])
def SixtySixDeck():
return generate_deck([
'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'
])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.020s

OK

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

Александър обнови решението на 25.03.2014 00:56 (преди над 10 години)

+class ClassNameRepresenter:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+
+class Rank(ClassNameRepresenter):
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Suit(ClassNameRepresenter):
+ def __init__(self, color):
+ self.color = color
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
+ 'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
+ 'Two': Two, 'Seven': Seven, 'Nine': Nine}
+
+
+SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
+ 'Spades': Spades, 'Clubs': Clubs}
+
+
+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 __str__(self):
+ return "{0} of {1}".format(str(self.rank), str(self.suit))
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.__cards = []
+ for card in collection:
+ self.add(card)
+
+ def draw(self, index):
+ return self.__cards.pop(index)
+
+ def draw_from_top(self):
+ return self.__cards.pop(len(self.__cards) - 1)
+
+ def draw_from_bottom(self):
+ return self.__cards.pop(0)
+
+ def top_card(self):
+ return self.__cards[len(self.__cards) - 1]
+
+ def bottom_card(self):
+ return self.__cards[0]
+
+ def add(self, card):
+ self.__cards.append(card)
+
+ def index(self, card):
+ cards_count = len(self.__cards)
+ for i in range(0, cards_count):
+ if self.__cards[i] == card:
+ return i
+ raise ValueError('Cound not find the specified card.')
+
+ def __getitem__(self, index):
+ return self.__cards[index]
+
+ def __len__(self):
+ return len(self.__cards)
+
+
+SUITS_ORDERED = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+RANKS_ORDERED = [
+ 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
+]
+
+
+def generate_deck(ranks_to_filter=set()):
+ cards = CardCollection([])
+ for suit in SUITS_ORDERED:
+ for rank in RANKS_ORDERED:
+ if rank not in ranks_to_filter:
+ cards.add(Card(RANKS[rank], SUITS[suit]))
+ return cards
+
+
+def StandardDeck():
+ return generate_deck()
+
+
+def BeloteDeck():
+ return generate_deck({'Two', 'Three', 'Four', 'Five', 'Six'})
+
+
+def SixtySixDeck():
+ return generate_deck({
+ 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'
+ })

Александър обнови решението на 25.03.2014 01:13 (преди над 10 години)

class ClassNameRepresenter:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return str(self) == str(other)
class Rank(ClassNameRepresenter):
def __init__(self, symbol):
self.symbol = symbol
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
class Suit(ClassNameRepresenter):
def __init__(self, color):
self.color = color
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
'Two': Two, 'Seven': Seven, 'Nine': Nine}
-
SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
'Spades': Spades, 'Clubs': Clubs}
+SUITS_ORDERED = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+RANKS_ORDERED = [
+ 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
+]
+
+
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 __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
- self.__cards = []
+ self._cards = []
for card in collection:
self.add(card)
def draw(self, index):
- return self.__cards.pop(index)
+ return self._cards.pop(index)
def draw_from_top(self):
- return self.__cards.pop(len(self.__cards) - 1)
+ return self._cards.pop(len(self._cards) - 1)
def draw_from_bottom(self):
- return self.__cards.pop(0)
+ return self._cards.pop(0)
def top_card(self):
- return self.__cards[len(self.__cards) - 1]
+ return self._cards[len(self._cards) - 1]
def bottom_card(self):
- return self.__cards[0]
+ return self._cards[0]
def add(self, card):
- self.__cards.append(card)
+ self._cards.append(card)
def index(self, card):
- cards_count = len(self.__cards)
+ cards_count = len(self._cards)
for i in range(0, cards_count):
- if self.__cards[i] == card:
+ if self._cards[i] == card:
return i
raise ValueError('Cound not find the specified card.')
def __getitem__(self, index):
- return self.__cards[index]
+ return self._cards[index]
def __len__(self):
- return len(self.__cards)
+ return len(self._cards)
-SUITS_ORDERED = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-RANKS_ORDERED = [
- 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
- 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
-]
-
-
-def generate_deck(ranks_to_filter=set()):
+def generate_deck(ranks_to_filter=[]):
cards = CardCollection([])
for suit in SUITS_ORDERED:
for rank in RANKS_ORDERED:
if rank not in ranks_to_filter:
cards.add(Card(RANKS[rank], SUITS[suit]))
return cards
def StandardDeck():
return generate_deck()
def BeloteDeck():
- return generate_deck({'Two', 'Three', 'Four', 'Five', 'Six'})
+ return generate_deck(['Two', 'Three', 'Four', 'Five', 'Six'])
def SixtySixDeck():
- return generate_deck({
+ return generate_deck([
'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'
- })
+ ])

Александър обнови решението на 25.03.2014 01:17 (преди над 10 години)

-class ClassNameRepresenter:
+class BaseCardData:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return str(self) == str(other)
-class Rank(ClassNameRepresenter):
+class Rank(BaseCardData):
def __init__(self, symbol):
self.symbol = symbol
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
-class Suit(ClassNameRepresenter):
+class Suit(BaseCardData):
def __init__(self, color):
self.color = color
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
'Two': Two, 'Seven': Seven, 'Nine': Nine}
SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
'Spades': Spades, 'Clubs': Clubs}
SUITS_ORDERED = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
RANKS_ORDERED = [
'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
]
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 __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self._cards = []
for card in collection:
self.add(card)
def draw(self, index):
return self._cards.pop(index)
def draw_from_top(self):
return self._cards.pop(len(self._cards) - 1)
def draw_from_bottom(self):
return self._cards.pop(0)
def top_card(self):
return self._cards[len(self._cards) - 1]
def bottom_card(self):
return self._cards[0]
def add(self, card):
self._cards.append(card)
def index(self, card):
cards_count = len(self._cards)
for i in range(0, cards_count):
if self._cards[i] == card:
return i
raise ValueError('Cound not find the specified card.')
def __getitem__(self, index):
return self._cards[index]
def __len__(self):
return len(self._cards)
def generate_deck(ranks_to_filter=[]):
cards = CardCollection([])
for suit in SUITS_ORDERED:
for rank in RANKS_ORDERED:
if rank not in ranks_to_filter:
cards.add(Card(RANKS[rank], SUITS[suit]))
return cards
def StandardDeck():
return generate_deck()
def BeloteDeck():
return generate_deck(['Two', 'Three', 'Four', 'Five', 'Six'])
def SixtySixDeck():
return generate_deck([
'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'
])

Rank.__init__(self, 'K') - това не е добър начин да зададеш стойност на self. Можеш просто да направиш: self.symbol = 'K' и резултата ще е същия. Мисли си за прости решения, когато правиш такива неща. Подкласовете на Rank и Suit могат да се дефинират динамично. За целта използвай type.

Александър обнови решението на 26.03.2014 01:51 (преди над 10 години)

class BaseCardData:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return str(self) == str(other)
class Rank(BaseCardData):
def __init__(self, symbol):
self.symbol = symbol
-class King(Rank):
- def __init__(self):
- Rank.__init__(self, 'K')
-
-
-class Six(Rank):
- def __init__(self):
- Rank.__init__(self, '6')
-
-
-class Jack(Rank):
- def __init__(self):
- Rank.__init__(self, 'J')
-
-
-class Five(Rank):
- def __init__(self):
- Rank.__init__(self, '5')
-
-
-class Queen(Rank):
- def __init__(self):
- Rank.__init__(self, 'Q')
-
-
-class Ten(Rank):
- def __init__(self):
- Rank.__init__(self, '10')
-
-
-class Ace(Rank):
- def __init__(self):
- Rank.__init__(self, 'A')
-
-
-class Three(Rank):
- def __init__(self):
- Rank.__init__(self, '3')
-
-
-class Eight(Rank):
- def __init__(self):
- Rank.__init__(self, '8')
-
-
-class Four(Rank):
- def __init__(self):
- Rank.__init__(self, '4')
-
-
-class Two(Rank):
- def __init__(self):
- Rank.__init__(self, '2')
-
-
-class Seven(Rank):
- def __init__(self):
- Rank.__init__(self, '7')
-
-
-class Nine(Rank):
- def __init__(self):
- Rank.__init__(self, '9')
-
-
class Suit(BaseCardData):
def __init__(self, color):
self.color = color
-class Diamonds(Suit):
- def __init__(self):
- Suit.__init__(self, 'red')
+def create_class(class_name, base_class, property_name, property_value):
+ return type(
+ class_name,
+ (base_class,),
+ {property_name: property_value, '__init__': BaseCardData.__init__}
+ )
+SUITS_ORDERED = [
+ ('Diamonds', 'red'), ('Clubs', 'black'),
+ ('Hearts', 'red'), ('Spades', 'black')
+]
-class Hearts(Suit):
- def __init__(self):
- Suit.__init__(self, 'red')
-
-
-class Spades(Suit):
- def __init__(self):
- Suit.__init__(self, 'black')
-
-
-class Clubs(Suit):
- def __init__(self):
- Suit.__init__(self, 'black')
-
-
-RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
- 'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
- 'Two': Two, 'Seven': Seven, 'Nine': Nine}
-
-SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
- 'Spades': Spades, 'Clubs': Clubs}
-
-SUITS_ORDERED = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-
RANKS_ORDERED = [
- 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
- 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
+ ('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'), ('Ten', '10'), ('Nine', '9'),
+ ('Eight', '8'), ('Seven', '7'), ('Six', '6'), ('Five', '5'), ('Four', '4'),
+ ('Three', '3'), ('Two', '2'), ('Ace', 'A')
]
+RANKS = {
+ key: create_class(key, Rank, 'symbol', value)
+ for key, value in RANKS_ORDERED
+}
+SUITS = {
+ key: create_class(key, Suit, 'color', value)
+ for key, value in SUITS_ORDERED
+}
+
+
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 __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self._cards = []
for card in collection:
self.add(card)
def draw(self, index):
return self._cards.pop(index)
def draw_from_top(self):
return self._cards.pop(len(self._cards) - 1)
def draw_from_bottom(self):
return self._cards.pop(0)
def top_card(self):
return self._cards[len(self._cards) - 1]
def bottom_card(self):
return self._cards[0]
def add(self, card):
self._cards.append(card)
def index(self, card):
cards_count = len(self._cards)
for i in range(0, cards_count):
if self._cards[i] == card:
return i
raise ValueError('Cound not find the specified card.')
def __getitem__(self, index):
return self._cards[index]
def __len__(self):
return len(self._cards)
def generate_deck(ranks_to_filter=[]):
cards = CardCollection([])
- for suit in SUITS_ORDERED:
- for rank in RANKS_ORDERED:
+ for suit in [element[0] for element in SUITS_ORDERED]:
+ for rank in [element[0] for element in RANKS_ORDERED]:
if rank not in ranks_to_filter:
cards.add(Card(RANKS[rank], SUITS[suit]))
return cards
def StandardDeck():
return generate_deck()
def BeloteDeck():
return generate_deck(['Two', 'Three', 'Four', 'Five', 'Six'])
def SixtySixDeck():
return generate_deck([
'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'
])