Решение на Тесте карти от Георги Йорданов

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

Към профила на Георги Йорданов

Резултати

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

Код

class Rank:
symbol = None
def __eq__(self, other):
return self.__class__ == other.__class__
class Suit:
color = None
def __eq__(self, other):
return self.__class__ == other.__class__
STANDARD_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
]
RANK_NAMES_SYMBOLS = [('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')
]
BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace'
]
SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def get_name(self):
return self.name
def get_color(suit):
if suit == 'Diamonds' or suit == 'Hearts':
return 'red'
return 'black'
RANKS = {rank: type(rank, (Rank,),
{'__str__': get_name, 'name': rank, 'symbol': symbol})
for rank, symbol in RANK_NAMES_SYMBOLS}
SUITS = {suit: type(suit, (Suit,),
{'__str__': get_name, 'name': suit,
'color': get_color(suit)})
for suit in ORDERED_SUITS}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __setattr__(self, name, value):
if name not in self.__dict__:
object.__setattr__(self, name, value)
else:
raise TypeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
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(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):
for i in range(0, len(self.__cards)):
if card == self.__cards[i]:
return i
raise ValueError('<Card ' + str(card) + '>' + ' is not in list')
def __getitem__(self, index):
return self.__cards[index]
def __len__(self):
return len(self.__cards)
def create_deck(suits, ranks):
return [Card(RANKS[rank], SUITS[suit]) for suit in suits
for rank in ranks]
def StandardDeck():
return CardCollection(create_deck(ORDERED_SUITS, STANDARD_RANKS))
def BeloteDeck():
return CardCollection(create_deck(ORDERED_SUITS, BELOTE_RANKS))
def SixtySixDeck():
return CardCollection(create_deck(ORDERED_SUITS, SIXTY_SIX_RANKS))

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.021s

OK

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

Георги обнови решението на 23.03.2014 22:02 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Ace'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Two'
+
+
+class Tree(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Tree'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Four'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Five'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Six'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Seven'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Eight'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Nine'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Ten'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Jack'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Queen'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'King'
+
+
+class Diamonds(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Diamonds'
+
+
+class Clubs(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Clubs'
+
+
+class Hearts(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Hearts'
+
+
+class Spades(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Spades'
+
+
+RANKS = {'Ace': Ace, 'Two': Two, 'Tree': Tree, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
+ 'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King
+ }
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Diamonds': Diamonds, 'Spades': Spades
+ }
+
+
+STANDARD_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Tree', 'Two', 'Ace'
+ ]
+
+
+BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace'
+ ]
+
+
+SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+
+
+ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __setattr__(self, name, value):
+ if name not in self.__dict__:
+ object.__setattr__(self, name, value)
+ else:
+ raise TypeError("can't set attribute")
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+
+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(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):
+ for i in range(0, len(self.__cards)):
+ if card == self.__cards[i]:
+ return i
+
+ raise ValueError('<Card ' + str(card) + '>' + ' is not in list')
+
+ def __getitem__(self, index):
+ return self.__cards[index]
+
+ def __len__(self):
+ return len(self.__cards)
+
+
+def create_deck(suits, ranks):
+ return [Card(RANKS[rank], SUITS[suit]) for suit in suits
+ for rank in ranks]
+
+
+def StandardDeck():
+ return CardCollection(create_deck(ORDERED_SUITS, STANDARD_RANKS))
+
+
+def BeloteDeck():
+ return CardCollection(create_deck(ORDERED_SUITS, BELOTE_RANKS))
+
+
+def SixtySixDeck():
+ return CardCollection(create_deck(ORDERED_SUITS, SIXTY_SIX_RANKS))

Tree означава дърво. Не е необходимо да дефинираш __eq__ във всички подкласове - можеш да гло сложиш само в базовия клас.

class King(Rank):
    def __init__(self):
        Rank.__init__(self, 'K')

Това можеш да го замениш само с:

class King(Rank):
    symbol = 'K'

Tree означава дърво. Не е необходимо да дефинираш __eq__ във всички подкласове - можеш да гло сложиш само в базовия клас.

class King(Rank):
    def __init__(self):
        Rank.__init__(self, 'K')

Това можеш да го замениш само с:

class King(Rank):
    symbol = 'K'

Георги обнови решението на 26.03.2014 14:07 (преди над 10 години)

class Rank:
- def __init__(self, symbol):
- self.__symbol = symbol
+ symbol = None
-
-class Suit:
- def __init__(self, color):
- self.__color = color
-
-
-class Ace(Rank):
- def __init__(self):
- Rank.__init__(self, 'A')
-
def __eq__(self, other):
return self.__class__ == other.__class__
- def __str__(self):
- return 'Ace'
+class Suit:
+ color = None
-class Two(Rank):
- def __init__(self):
- Rank.__init__(self, '2')
-
def __eq__(self, other):
return self.__class__ == other.__class__
- def __str__(self):
- return 'Two'
+STANDARD_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'
+ ]
-class Tree(Rank):
- def __init__(self):
- Rank.__init__(self, '3')
- def __eq__(self, other):
- return self.__class__ == other.__class__
+RANK_NAMES_SYMBOLS = [('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')
+ ]
- def __str__(self):
- return 'Tree'
+BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace'
+ ]
-class Four(Rank):
- def __init__(self):
- Rank.__init__(self, '4')
- def __eq__(self, other):
- return self.__class__ == other.__class__
+SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
- def __str__(self):
- return 'Four'
+ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-class Five(Rank):
- def __init__(self):
- Rank.__init__(self, '5')
- def __eq__(self, other):
- return self.__class__ == other.__class__
+def get_name(self):
+ return self.name
- def __str__(self):
- return 'Five'
+def get_color(suit):
+ if suit == 'Diamonds' or suit == 'Hearts':
+ return 'red'
-class Six(Rank):
- def __init__(self):
- Rank.__init__(self, '6')
+ return 'black'
- def __eq__(self, other):
- return self.__class__ == other.__class__
- def __str__(self):
- return 'Six'
+RANKS = {rank: type(rank, (Rank,),
+ {'__str__': get_name, 'name': rank, 'symbol': symbol})
+ for rank, symbol in RANK_NAMES_SYMBOLS}
-class Seven(Rank):
- def __init__(self):
- Rank.__init__(self, '7')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Seven'
-
-
-class Eight(Rank):
- def __init__(self):
- Rank.__init__(self, '8')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Eight'
-
-
-class Nine(Rank):
- def __init__(self):
- Rank.__init__(self, '9')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Nine'
-
-
-class Ten(Rank):
- def __init__(self):
- Rank.__init__(self, '10')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Ten'
-
-
-class Jack(Rank):
- def __init__(self):
- Rank.__init__(self, 'J')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Jack'
-
-
-class Queen(Rank):
- def __init__(self):
- Rank.__init__(self, 'Q')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Queen'
-
-
-class King(Rank):
- def __init__(self):
- Rank.__init__(self, 'K')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'King'
-
-
-class Diamonds(Rank):
- def __init__(self):
- Rank.__init__(self, 'red')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Diamonds'
-
-
-class Clubs(Rank):
- def __init__(self):
- Rank.__init__(self, 'black')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Clubs'
-
-
-class Hearts(Rank):
- def __init__(self):
- Rank.__init__(self, 'red')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Hearts'
-
-
-class Spades(Rank):
- def __init__(self):
- Rank.__init__(self, 'black')
-
- def __eq__(self, other):
- return self.__class__ == other.__class__
-
- def __str__(self):
- return 'Spades'
-
-
-RANKS = {'Ace': Ace, 'Two': Two, 'Tree': Tree, 'Four': Four, 'Five': Five,
- 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
- 'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King
- }
-
-
-SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
- 'Diamonds': Diamonds, 'Spades': Spades
- }
-
-
-STANDARD_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Tree', 'Two', 'Ace'
- ]
-
-
-BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
- 'Nine', 'Eight', 'Seven', 'Ace'
- ]
-
-
-SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
-
-
-ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUITS = {suit: type(suit, (Suit,),
+ {'__str__': get_name, 'name': suit,
+ 'color': get_color(suit)})
+ for suit in ORDERED_SUITS}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __setattr__(self, name, value):
if name not in self.__dict__:
object.__setattr__(self, name, value)
else:
raise TypeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
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(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):
for i in range(0, len(self.__cards)):
if card == self.__cards[i]:
return i
raise ValueError('<Card ' + str(card) + '>' + ' is not in list')
def __getitem__(self, index):
return self.__cards[index]
def __len__(self):
return len(self.__cards)
def create_deck(suits, ranks):
return [Card(RANKS[rank], SUITS[suit]) for suit in suits
for rank in ranks]
def StandardDeck():
return CardCollection(create_deck(ORDERED_SUITS, STANDARD_RANKS))
def BeloteDeck():
return CardCollection(create_deck(ORDERED_SUITS, BELOTE_RANKS))
def SixtySixDeck():
return CardCollection(create_deck(ORDERED_SUITS, SIXTY_SIX_RANKS))