Решение на Тесте карти от Димитър Мутафчиев

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

Към профила на Димитър Мутафчиев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 13 успешни тест(а)
  • 3 неуспешни тест(а)

Код

class Rank:
def __init__(self, symbol):
self.__symbol = symbol
def __str__(self):
return self.__symbol
def __eq__(self, other):
return self.__symbol == other.__symbol
RANKS_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
RANKS = {}
for i in RANKS_NAMES:
RANKS[i] = type(i, (Rank,),
dict(__init__=lambda self, i=i: Rank.__init__(self, i)))
#####################################################################
class Suit:
def __init__(self, color):
self.__color = color
def __str__(self):
return self.__color
def __eq__(self, other):
return self.__color == other.__color
SUITS_NAMES = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
SUITS = {}
for i in SUITS_NAMES:
SUITS[i] = type(i, (Suit,),
dict(__init__=lambda self, i=i: Suit.__init__(self, i)))
#####################################################################
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
def __str__(self):
return str(self.__rank) + " of " + str(self.__suit)
def __eq__(self, other):
return self.__rank == other.__rank and self.__suit == other.__suit
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
#####################################################################
class CardCollection:
def __init__(self, collection=[]):
self.__collection = list(collection)
def __getitem__(self, i):
return self.__collection[i]
def __len__(self):
return len(self.__collection)
def draw(self, index):
return self.__collection.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.__collection[-1]
def bottom_card(self):
return self.__collection[0]
def add(self, card):
self.__collection.append(card)
def index(self, card):
return self.__collection.index(card)
#####################################################################
def StandardDeck():
result_deck = []
for rank in RANKS_NAMES:
for suit in SUITS_NAMES:
result_deck.append(Card(RANKS[rank], SUITS[suit]))
return result_deck
BELOTE_RANKS = ["King", "Queen", "Jack", "Ten",
"Nine", "Eight", "Seven", "Ace"]
def BeloteDeck():
return list(filter(lambda x: str(x.rank) in BELOTE_RANKS, StandardDeck()))
SIXTY_SIX_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Ace"]
def SixtySixDeck():
return list(filter(lambda x: str(x.rank) in SIXTY_SIX_RANKS,
StandardDeck()))

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

F.....FF........
======================================================================
FAIL: test_belote_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-kciz6o/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'King of ...

First differing element 1:
Queen of Diamonds
King of Hearts

Diff is 1205 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_sixtysix_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-kciz6o/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'King of ...

First differing element 1:
Queen of Diamonds
King of Hearts

Diff is 897 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_standard_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-kciz6o/test.py", line 171, in test_standard_deck
    self.assertEqual(STANDARD_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'King of ...

First differing element 1:
Queen of Diamonds
King of Hearts

Diff is 1933 characters long. Set self.maxDiff to None to see it.

----------------------------------------------------------------------
Ran 16 tests in 0.047s

FAILED (failures=3)

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

Димитър обнови решението на 24.03.2014 22:02 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __str__(self):
+ return self.__symbol
+
+ def __eq__(self, other):
+ return self.__symbol == other.__symbol
+
+RANKS_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+RANKS = {}
+for i in RANKS_NAMES:
+ RANKS[i] = type(i, (Rank,),
+ dict(__init__=lambda self, i=i: Rank.__init__(self, i)))
+
+
+#####################################################################
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+ def __str__(self):
+ return self.__color
+
+ def __eq__(self, other):
+ return self.__color == other.__color
+
+SUITS_NAMES = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+
+SUITS = {}
+for i in SUITS_NAMES:
+ SUITS[i] = type(i, (Suit,),
+ dict(__init__=lambda self, i=i: Suit.__init__(self, i)))
+
+
+#####################################################################
+class Card:
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
+
+ def __str__(self):
+ return str(self.__rank) + " of " + str(self.__suit)
+
+ def __eq__(self, other):
+ return self.__rank == other.__rank and self.__suit == other.__suit
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+#####################################################################
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.__collection = list(collection)
+
+ def __getitem__(self, i):
+ return self.__collection[i]
+
+ def __len__(self):
+ return len(self.__collection)
+
+ def draw(self, index):
+ return self.__collection.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.__collection[-1]
+
+ def bottom_card(self):
+ return self.__collection[0]
+
+ def add(self, card):
+ self.__collection.append(card)
+
+ def index(self, card):
+ return self.__collection.index(card)
+
+
+#####################################################################
+def StandardDeck():
+ result_deck = []
+ for rank in RANKS_NAMES:
+ for suit in SUITS_NAMES:
+ result_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return result_deck
+
+BELOTE_RANKS = ["King", "Queen", "Jack", "Ten",
+ "Nine", "Eight", "Seven", "Ace"]
+
+
+def BeloteDeck():
+ return list(filter(lambda x: str(x.rank) in BELOTE_RANKS, StandardDeck()))
+
+SIXTY_SIX_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Ace"]
+
+
+def SixtySixDeck():
+ return list(filter(lambda x: str(x.rank) in SIXTY_SIX_RANKS,
+ StandardDeck()))
  • ###...### не е нужно да се ползва като разделител между класовете
  • Не ми се струва логично __color и __rank да бъдат private атрибути
  • i не е достатъчно добро име, когато става дума за имена, попринцип идва от "index"