Решение на Тесте карти от Ангел Цанев

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

Към профила на Ангел Цанев

Резултати

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

Код

CARDS = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
'Three': '3', 'Two' : '2', 'Six' : '6', 'Seven': '7', 'Ten' : '10',
'King' : 'K', 'Five' : '5', 'Ace' : 'A'}
COLORS = {'Hearts': 'red' , 'Clubs' : 'black',
'Spades': 'black', 'Diamonds': 'red'}
class Rank:
def __init__(self):
self.symbol = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __ne__(self, other):
return not self.__eq__(other)
class Suit:
def __init__(self):
self.color = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __ne__(self, other):
return not self.__eq__(other)
RANKS = {rank: type(rank, (Rank,), {'symbol': CARDS[rank]}) for rank in CARDS}
SUITS = {suit: type(suit, (Suit,), {'color': COLORS[suit]}) for suit in COLORS}
class Card:
def __init__(self, _rank, _suit):
self.card = (_rank(), _suit())
def __repr__(self):
return "<Card {0}>".format(str(self))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
@property
def rank(self):
return self.card[0]
@property
def suit(self):
return self.card[1]
class CardCollection:
def __init__(self, collection=None):
if collection is None:
self.deck = []
else:
self.deck = list(collection)
def __getitem__(self, key):
return self.deck[key]
def __iter__(self):
for card in self.deck:
yield card
def __len__(self):
return len(self.deck)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return self.deck.index(card)
RANKS_ORDER = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
'Seven' , 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
SUITS_ORDER = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def StandardDeck():
return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in RANKS_ORDER]
def BeloteDeck():
belote_order = RANKS_ORDER[:7] + [RANKS_ORDER[-1]]
return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in belote_order]
def SixtySixDeck():
santase_order = RANKS_ORDER[:5] + [RANKS_ORDER[-1]]
return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in santase_order]

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

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

OK

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

Ангел обнови решението на 24.03.2014 20:55 (преди около 10 години)

+ranks = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
+ 'Three': '3', 'Two' : '2', 'Six' : '6', 'Seven': '7', 'Ten' : '10',
+ 'King' : 'K', 'Five' : '5', 'Ace' : 'A'}
+
+suits = {'Hearts': 'red', 'Clubs': 'black', 'Spades': 'black', 'Diamonds': 'red'}
+
+
+class Rank:
+ def __init__(self):
+ self.symbol = None
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.symbol == other.symbol
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+
+
+class Suit:
+ def __init__(self):
+ self.color = None
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.color == other.color
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+
+RANKS = {rank: type(rank, (Rank,), dict(symbol=ranks[rank])) for rank in ranks}
+SUITS = {suit: type(suit, (Suit,), dict(color=suits[suit])) for suit in suits}
+
+
+class Card:
+ def __init__(self, _rank, _suit):
+ self.card = (_rank(), _suit())
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ @property
+ def rank(self):
+ return self.card[0]
+
+ @property
+ def suit(self):
+ return self.card[1]
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ if collection is None:
+ self.deck = []
+ else:
+ self.deck = list(collection)
+
+ def __getitem__(self, key):
+ return self.deck[key]
+
+ def __iter__(self):
+ for card in self.deck:
+ yield card
+
+ def __len__(self):
+ return len(self.deck)
+
+ def draw(self, index):
+ return self.deck.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.deck[-1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ return self.deck.index(card)
+
+ranks_hierarchy = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
+ 'Seven', 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
+
+suits_hierarchy = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+def StandardDeck():
+ result = []
+ for suit in suits_hierarchy:
+ for rank in ranks_hierarchy:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def BeloteDeck():
+ result = []
+ for suit in suits_hierarchy:
+ for rank in ranks_hierarchy[:7] + [ranks_hierarchy[-1]]:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def SixtySixDeck():
+ result = []
+ for suit in suits_hierarchy:
+ for rank in ranks_hierarchy[:5] + [ranks_hierarchy[-1]]:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result

Ангел обнови решението на 24.03.2014 21:07 (преди около 10 години)

ranks = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
'Three': '3', 'Two' : '2', 'Six' : '6', 'Seven': '7', 'Ten' : '10',
'King' : 'K', 'Five' : '5', 'Ace' : 'A'}
suits = {'Hearts': 'red', 'Clubs': 'black', 'Spades': 'black', 'Diamonds': 'red'}
class Rank:
def __init__(self):
self.symbol = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return isinstance(other, self.__class__) and self.symbol == other.symbol
def __ne__(self, other):
return not self.__eq__(other)
-
class Suit:
def __init__(self):
self.color = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return isinstance(other, self.__class__) and self.color == other.color
def __ne__(self, other):
return not self.__eq__(other)
RANKS = {rank: type(rank, (Rank,), dict(symbol=ranks[rank])) for rank in ranks}
SUITS = {suit: type(suit, (Suit,), dict(color=suits[suit])) for suit in suits}
class Card:
def __init__(self, _rank, _suit):
self.card = (_rank(), _suit())
def __repr__(self):
return '<Card ' + str(self) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
@property
def rank(self):
return self.card[0]
@property
def suit(self):
return self.card[1]
class CardCollection:
def __init__(self, collection=None):
if collection is None:
self.deck = []
else:
self.deck = list(collection)
def __getitem__(self, key):
return self.deck[key]
def __iter__(self):
for card in self.deck:
yield card
def __len__(self):
return len(self.deck)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return self.deck.index(card)
-ranks_hierarchy = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
+
+ranks_order = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
'Seven', 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
-suits_hierarchy = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+suits_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def StandardDeck():
- result = []
- for suit in suits_hierarchy:
- for rank in ranks_hierarchy:
- result.append(Card(RANKS[rank], SUITS[suit]))
- return result
+ return [Card(RANKS[y], SUITS[x]) for x in suits_order for y in ranks_order]
def BeloteDeck():
- result = []
- for suit in suits_hierarchy:
- for rank in ranks_hierarchy[:7] + [ranks_hierarchy[-1]]:
- result.append(Card(RANKS[rank], SUITS[suit]))
- return result
+ belote_order = ranks_order[:7] + [ranks_order[-1]]
+ return [Card(RANKS[y], SUITS[x]) for x in suits_order for y in belote_order]
def SixtySixDeck():
- result = []
- for suit in suits_hierarchy:
+ santase_order = ranks_order[:5] + [ranks_order[-1]]
- for rank in ranks_hierarchy[:5] + [ranks_hierarchy[-1]]:
+ return [Card(RANKS[y], SUITS[x]) for x in suits_order for y in santase_order]
- result.append(Card(RANKS[rank], SUITS[suit]))
- return result
  • подравнил си си ranks добре, защо не и rank_order?
  • dict(symbol=ranks[rank]) използвай литерален синтаксис: {key1: value1, key2: value2 ...}
  • когато обхождаш речник и ти трябват и ключовете, и стойностите използвай dict.items()
  • При сравняването е ненужно(по-скоро непрепоръчително) да проверяваш дали другия обект е от същия клас. Duck typing философията на Python гласи, че нещо, което прилича на пате и се държи като пате е пате...
  • за __str__ методите е препоръчително да ползваш str.format вместо изрично конвертиране до низ и конкатениране с +
  • глобалните променливи не са хубаво нещо. Или ги отбележи като константи, или заобиколи употребата им

Ангел обнови решението на 26.03.2014 11:49 (преди около 10 години)

-ranks = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
+CARDS = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
'Three': '3', 'Two' : '2', 'Six' : '6', 'Seven': '7', 'Ten' : '10',
'King' : 'K', 'Five' : '5', 'Ace' : 'A'}
-suits = {'Hearts': 'red', 'Clubs': 'black', 'Spades': 'black', 'Diamonds': 'red'}
+COLORS = {'Hearts': 'red' , 'Clubs' : 'black',
+ 'Spades': 'black', 'Diamonds': 'red'}
class Rank:
def __init__(self):
self.symbol = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
- return isinstance(other, self.__class__) and self.symbol == other.symbol
+ return self.__class__.__name__ == other.__class__.__name__
def __ne__(self, other):
return not self.__eq__(other)
class Suit:
def __init__(self):
self.color = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
- return isinstance(other, self.__class__) and self.color == other.color
+ return self.__class__.__name__ == other.__class__.__name__
def __ne__(self, other):
return not self.__eq__(other)
-RANKS = {rank: type(rank, (Rank,), dict(symbol=ranks[rank])) for rank in ranks}
-SUITS = {suit: type(suit, (Suit,), dict(color=suits[suit])) for suit in suits}
+RANKS = {rank: type(rank, (Rank,), {'symbol': CARDS[rank]}) for rank in CARDS}
+SUITS = {suit: type(suit, (Suit,), {'color': COLORS[suit]}) for suit in COLORS}
class Card:
def __init__(self, _rank, _suit):
self.card = (_rank(), _suit())
def __repr__(self):
- return '<Card ' + str(self) + '>'
+ #return '<Card ' + str(self) + '>'
+ return "<Card {name}>".format(name=str(self))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
- return str(self.rank) + ' of ' + str(self.suit)
+ #return str(self.rank) + ' of ' + str(self.suit)
+ return "{rank} of {suit}".format(rank=str(self.rank), suit=str(self.suit))
@property
def rank(self):
return self.card[0]
@property
def suit(self):
return self.card[1]
class CardCollection:
def __init__(self, collection=None):
if collection is None:
self.deck = []
else:
self.deck = list(collection)
def __getitem__(self, key):
return self.deck[key]
def __iter__(self):
for card in self.deck:
yield card
def __len__(self):
return len(self.deck)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return self.deck.index(card)
-ranks_order = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
- 'Seven', 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
+RANKS_ORDER = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
+ 'Seven' , 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
-suits_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUITS_ORDER = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def StandardDeck():
- return [Card(RANKS[y], SUITS[x]) for x in suits_order for y in ranks_order]
+ return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in RANKS_ORDER]
def BeloteDeck():
- belote_order = ranks_order[:7] + [ranks_order[-1]]
- return [Card(RANKS[y], SUITS[x]) for x in suits_order for y in belote_order]
+ belote_order = RANKS_ORDER[:7] + [RANKS_ORDER[-1]]
+ return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in belote_order]
def SixtySixDeck():
- santase_order = ranks_order[:5] + [ranks_order[-1]]
- return [Card(RANKS[y], SUITS[x]) for x in suits_order for y in santase_order]
+ santase_order = RANKS_ORDER[:5] + [RANKS_ORDER[-1]]
+ return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in santase_order]

Ангел обнови решението на 26.03.2014 13:06 (преди около 10 години)

CARDS = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
'Three': '3', 'Two' : '2', 'Six' : '6', 'Seven': '7', 'Ten' : '10',
'King' : 'K', 'Five' : '5', 'Ace' : 'A'}
COLORS = {'Hearts': 'red' , 'Clubs' : 'black',
'Spades': 'black', 'Diamonds': 'red'}
class Rank:
def __init__(self):
self.symbol = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __ne__(self, other):
return not self.__eq__(other)
class Suit:
def __init__(self):
self.color = None
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __ne__(self, other):
return not self.__eq__(other)
RANKS = {rank: type(rank, (Rank,), {'symbol': CARDS[rank]}) for rank in CARDS}
SUITS = {suit: type(suit, (Suit,), {'color': COLORS[suit]}) for suit in COLORS}
class Card:
def __init__(self, _rank, _suit):
self.card = (_rank(), _suit())
def __repr__(self):
- #return '<Card ' + str(self) + '>'
- return "<Card {name}>".format(name=str(self))
+ return "<Card {0}>".format(str(self))
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
- #return str(self.rank) + ' of ' + str(self.suit)
- return "{rank} of {suit}".format(rank=str(self.rank), suit=str(self.suit))
+ return "{0} of {1}".format(str(self.rank), str(self.suit))
@property
def rank(self):
return self.card[0]
@property
def suit(self):
return self.card[1]
class CardCollection:
def __init__(self, collection=None):
if collection is None:
self.deck = []
else:
self.deck = list(collection)
def __getitem__(self, key):
return self.deck[key]
def __iter__(self):
for card in self.deck:
yield card
def __len__(self):
return len(self.deck)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return self.deck.index(card)
RANKS_ORDER = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
'Seven' , 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
SUITS_ORDER = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def StandardDeck():
return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in RANKS_ORDER]
def BeloteDeck():
belote_order = RANKS_ORDER[:7] + [RANKS_ORDER[-1]]
return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in belote_order]
def SixtySixDeck():
santase_order = RANKS_ORDER[:5] + [RANKS_ORDER[-1]]
return [Card(RANKS[y], SUITS[x]) for x in SUITS_ORDER for y in santase_order]