Решение на Тесте карти от Даяна Маринова

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

Към профила на Даяна Маринова

Резултати

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

Код

class Rank():
"""docstring for Rank"""
def __init__(self, symbol=''):
self.symbol = symbol
def __str__(self):
return str(self.__class__.__name__)
def __eq__(self, other):
return self.symbol == other.symbol
RANKS = {
'King': type('King', (Rank, ), {'symbol': 'K'}),
'Six': type('Six', (Rank, ), {'symbol': '6'}),
'Jack': type('Jack', (Rank, ), {'symbol': 'J'}),
'Five': type('Five', (Rank, ), {'symbol': '5'}),
'Queen': type('Queen', (Rank, ), {'symbol': 'Q'}),
'Ten': type('Ten', (Rank, ), {'symbol': '10'}),
'Ace': type('Ace', (Rank, ), {'symbol': 'A'}),
'Three': type('Three', (Rank, ), {'symbol': '3'}),
'Eight': type('Eight', (Rank, ), {'symbol': '8'}),
'Four': type('Four', (Rank, ), {'symbol': '4'}),
'Two': type('Two', (Rank, ), {'symbol': '2'}),
'Seven': type('Seven', (Rank, ), {'symbol': '7'}),
'Nine': type('Nine', (Rank, ), {'symbol': '9'})
}
class Suit():
"""docstring for Suit"""
def __init__(self, color=''):
self.color = color
def __str__(self):
return str(self.__class__.__name__)
def __eq__(self, other):
return self.color == other.color
SUITS = {
'Diamonds': type('Diamonds', (Suit, ), {'color': 'red'}),
'Hearts': type('Hearts', (Suit, ), {'color': 'red'}),
'Spades': type('Spades', (Suit, ), {'color': 'black'}),
'Clubs': type('Clubs', (Suit, ), {'color': 'black'})
}
class Card():
"""docstring for Card"""
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection():
"""docstring for CardCollection"""
def __init__(self, collection=[]):
self.collection = collection
def __len__(self):
return len(self.collection)
def __getitem__(self, index):
return self.collection[index]
def __str__(self):
return str(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def drawn_from_botton(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[len(self.collection) - 1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
if card in self.collection:
return self.collection.index(card)
def StandardDeck():
standard_deck = CardCollection()
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
for suit in range(0, len(suits)):
for rank in range(0, len(ranks)):
standard_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
return standard_deck
def BeloteDeck():
belote_deck = CardCollection()
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
for suit in range(0, len(suits)):
for rank in range(0, len(ranks)):
belote_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
return belote_deck
def SixtySixDeck():
sixty_six_deck = CardCollection()
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
for suit in range(0, len(suits)):
for rank in range(0, len(ranks)):
sixty_six_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
return sixty_six_deck

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

FEEE.EFF....F...
======================================================================
ERROR: test_deck_add (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_deck_draw (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-k3tlpv/test.py", line 145, in test_deck_draw
    self.assertEqual(card, deck.draw_from_bottom())
AttributeError: 'CardCollection' object has no attribute 'draw_from_bottom'

======================================================================
ERROR: test_deck_index (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_deck_order (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
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-k3tlpv/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 8:
King of Clubs
King of Hearts

Diff is 907 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-k3tlpv/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', 'Queen of...

First differing element 5:
Ace of Diamonds
Eight of Diamonds

Second list contains 40 additional elements.
First extra element 24:
King of Clubs

Diff is 1549 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-k3tlpv/test.py", line 171, in test_standard_deck
    self.assertEqual(STANDARD_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 7:
Six of Diamonds
Ace of Diamonds

Second list contains 64 additional elements.
First extra element 52:
Nine of Spades

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

======================================================================
FAIL: test_card_equals (test.CardTest)
----------------------------------------------------------------------
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-k3tlpv/test.py", line 74, in test_card_equals
    self.assertNotEqual(aos1, solution.Card(solution.RANKS["Ace"], solution.SUITS["Hearts"]))
AssertionError: <Card Ace of Spades> == <Card Ace of Hearts>

----------------------------------------------------------------------
Ran 16 tests in 6.927s

FAILED (failures=4, errors=4)

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

Даяна обнови решението на 25.03.2014 01:35 (преди над 10 години)

+'''
+Извинявам се че ти пиша тук, но test_deck_add ми прави безкраен цикъл и не мога да разбера защо. Като си го тествам си ми добава карти, уж, без проблем.
+'''
+
+
+class Rank():
+ """docstring for Rank"""
+ def __init__(self, symbol=''):
+ self.symbol = symbol
+
+ def __str__(self):
+ return str(self.__class__.__name__)
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+RANKS = {
+ 'King': type('King', (Rank, ), {'symbol': 'K'}),
+ 'Six': type('Six', (Rank, ), {'symbol': '6'}),
+ 'Jack': type('Jack', (Rank, ), {'symbol': 'J'}),
+ 'Five': type('Five', (Rank, ), {'symbol': '5'}),
+ 'Queen': type('Queen', (Rank, ), {'symbol': 'Q'}),
+ 'Ten': type('King', (Rank, ), {'symbol': 'k'}),
+ 'Ace': type('Ace', (Rank, ), {'symbol': 'A'}),
+ 'Three': type('Three', (Rank, ), {'symbol': '3'}),
+ 'Eight': type('Eight', (Rank, ), {'symbol': '8'}),
+ 'Four': type('Four', (Rank, ), {'symbol': '4'}),
+ 'Two': type('Two', (Rank, ), {'symbol': '2'}),
+ 'Seven': type('Seven', (Rank, ), {'symbol': '7'}),
+ 'Nine': type('Nine', (Rank, ), {'symbol': '9'})
+}
+
+
+class Suit():
+ """docstring for Suit"""
+ def __init__(self, color=''):
+ self.color = color
+
+ def __str__(self):
+ return str(self.__class__.__name__)
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+
+SUITS = {
+ 'Diamonds': type('Diamonds', (Suit, ), {'color': 'red'}),
+ 'Hearts': type('Hearts', (Suit, ), {'color': 'red'}),
+ 'Spades': type('Spades', (Suit, ), {'color': 'black'}),
+ 'Clubs': type('Clubs', (Suit, ), {'color': 'black'})
+}
+
+
+class Card():
+ """docstring for Card"""
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection():
+ """docstring for CardCollection"""
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __str__(self):
+ return str(self.collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def drawn_from_botton(self):
+ return self.collection.pop(0)
+
+ def top_card(self):
+ return self.collection[len(self.collection) - 1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ if card in self.collection:
+ return self.collection.index(card)
+
+
+def StandardDeck():
+ standard_deck = CardCollection()
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+ suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+ for suit in range(0, len(suits)):
+ for rank in range(0, len(ranks)):
+ standard_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
+ return standard_deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+ for suit in range(0, len(suits)):
+ for rank in range(0, len(ranks)):
+ belote_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+ for suit in range(0, len(suits)):
+ for rank in range(0, len(ranks)):
+ sixty_six_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
+ return sixty_six_deck
RANKS = {
    'King': type('King', (Rank, ), {'symbol': 'K'}),
    'Six': type('Six', (Rank, ), {'symbol': '6'}),
    ...

Това може да стане доста по-кратко, ако използваш dictionary comprehension. Иначе, си се ориентирала правилно да дефинираш класовете с type.

Решението ти може да бъде и по-кратко. Например, имаш повтарящ се код. Предимно, имената на картите. Помисли, как да ги изнесеш в константи.

Даяна обнови решението на 26.03.2014 14:04 (преди над 10 години)

-'''
-Извинявам се че ти пиша тук, но test_deck_add ми прави безкраен цикъл и не мога да разбера защо. Като си го тествам си ми добава карти, уж, без проблем.
-'''
-
-
class Rank():
"""docstring for Rank"""
def __init__(self, symbol=''):
self.symbol = symbol
def __str__(self):
return str(self.__class__.__name__)
def __eq__(self, other):
return self.symbol == other.symbol
RANKS = {
'King': type('King', (Rank, ), {'symbol': 'K'}),
'Six': type('Six', (Rank, ), {'symbol': '6'}),
'Jack': type('Jack', (Rank, ), {'symbol': 'J'}),
'Five': type('Five', (Rank, ), {'symbol': '5'}),
'Queen': type('Queen', (Rank, ), {'symbol': 'Q'}),
- 'Ten': type('King', (Rank, ), {'symbol': 'k'}),
+ 'Ten': type('Ten', (Rank, ), {'symbol': '10'}),
'Ace': type('Ace', (Rank, ), {'symbol': 'A'}),
'Three': type('Three', (Rank, ), {'symbol': '3'}),
'Eight': type('Eight', (Rank, ), {'symbol': '8'}),
'Four': type('Four', (Rank, ), {'symbol': '4'}),
'Two': type('Two', (Rank, ), {'symbol': '2'}),
'Seven': type('Seven', (Rank, ), {'symbol': '7'}),
'Nine': type('Nine', (Rank, ), {'symbol': '9'})
}
class Suit():
"""docstring for Suit"""
def __init__(self, color=''):
self.color = color
def __str__(self):
return str(self.__class__.__name__)
def __eq__(self, other):
return self.color == other.color
SUITS = {
'Diamonds': type('Diamonds', (Suit, ), {'color': 'red'}),
'Hearts': type('Hearts', (Suit, ), {'color': 'red'}),
'Spades': type('Spades', (Suit, ), {'color': 'black'}),
'Clubs': type('Clubs', (Suit, ), {'color': 'black'})
}
class Card():
"""docstring for Card"""
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection():
"""docstring for CardCollection"""
def __init__(self, collection=[]):
self.collection = collection
def __len__(self):
return len(self.collection)
def __getitem__(self, index):
return self.collection[index]
def __str__(self):
return str(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def drawn_from_botton(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[len(self.collection) - 1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
if card in self.collection:
return self.collection.index(card)
def StandardDeck():
standard_deck = CardCollection()
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
for suit in range(0, len(suits)):
for rank in range(0, len(ranks)):
standard_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
return standard_deck
def BeloteDeck():
belote_deck = CardCollection()
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
for suit in range(0, len(suits)):
for rank in range(0, len(ranks)):
belote_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
return belote_deck
def SixtySixDeck():
sixty_six_deck = CardCollection()
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
for suit in range(0, len(suits)):
for rank in range(0, len(ranks)):
sixty_six_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
return sixty_six_deck