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

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

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

Резултати

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

Код

class Rank:
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return self.__class__.__name__
class Suit:
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return self.__class__.__name__
RANKS = {'Two': type('Two', (Rank,), {'symbol': '2'}),
'Three': type('Three', (Rank,), {'symbol': '3'}),
'Four': type('Four', (Rank,), {'symbol': '4'}),
'Five': type('Five', (Rank,), {'symbol': '5'}),
'Six': type('Six', (Rank,), {'symbol': '6'}),
'Seven': type('Seven', (Rank,), {'symbol': '7'}),
'Eight': type('Eight', (Rank,), {'symbol': '8'}),
'Nine': type('Nine', (Rank,), {'symbol': '9'}),
'Ten': type('Ten', (Rank,), {'symbol': '10'}),
'Jack': type('Jack', (Rank,), {'symbol': 'J'}),
'Queen': type('Queen', (Rank,), {'symbol': 'Q'}),
'King': type('King', (Rank,), {'symbol': 'K'}),
'Ace': type('Ace', (Rank,), {'symbol': 'A'})}
SUITS = {'Hearts': type('Hearts', (Suit, ), {'color': 'red'}),
'Diamonds': type('Diamonds', (Suit, ), {'color': 'red'}),
'Spades': type('Spades', (Suit, ), {'color': 'black'}),
'Clubs': type('Clubs', (Suit, ), {'color': 'black'})}
class Card(object):
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return self.rank.__name__ + " of " + self.suit.__name__
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, index):
return self.collection[index]
def __setitem__(self, index, value):
self.collection[index] = value
def __len__(self):
return len(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(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):
first_found = next(obj for obj in self.collection if obj == card)
return self.collection.index(first_found)
def StandardDeck():
standard_deck = CardCollection()
for card_ranks in RANKS.keys():
for card_suits in SUITS.keys():
standard_deck.add(str(Card(RANKS[card_ranks], SUITS[card_suits])))
return standard_deck.collection
def BeloteDeck():
belot_deck = CardCollection()
for card_ranks in RANKS.keys():
if RANKS[card_ranks].symbol not in ['2', '3', '4', '5', '6']:
for card_suits in SUITS.keys():
belot_deck.add(str(Card(RANKS[card_ranks], SUITS[card_suits])))
return belot_deck.collection
def SixtySixDeck():
six_deck = CardCollection()
for card_ranks in RANKS.keys():
if RANKS[card_ranks].symbol not in ['2', '3', '4', '5', '6', '7', '8']:
for card_suits in SUITS.keys():
six_deck.add(str(Card(RANKS[card_ranks], SUITS[card_suits])))
return six_deck.collection

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

FE.E.EFFF.FF.FF.
======================================================================
ERROR: test_deck_add (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-1ptqrfr/test.py", line 121, in test_deck_add
    self.assertEqual(deck[0], card1)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 642, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 632, in _baseAssertEqual
    if not first == second:
  File "/tmp/d20140407-19315-1ptqrfr/solution.py", line 43, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'str' object has no attribute 'rank'

======================================================================
ERROR: test_deck_index (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-1ptqrfr/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
  File "/tmp/d20140407-19315-1ptqrfr/solution.py", line 81, in index
    first_found = next(obj for obj in self.collection if obj == card)
  File "/tmp/d20140407-19315-1ptqrfr/solution.py", line 81, in <genexpr>
    first_found = next(obj for obj in self.collection if obj == card)
  File "/tmp/d20140407-19315-1ptqrfr/solution.py", line 43, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'str' object has no attribute 'rank'

======================================================================
ERROR: test_deck_order (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-1ptqrfr/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 642, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 632, in _baseAssertEqual
    if not first == second:
  File "/tmp/d20140407-19315-1ptqrfr/solution.py", line 43, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'str' object has no attribute 'rank'

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

First differing element 0:
King of Diamonds
Nine of Spades

Diff is 1374 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-1ptqrfr/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['Nine of Spades', 'Nine of He...

First differing element 0:
King of Diamonds
Nine of Spades

Second list contains 38 additional elements.
First extra element 24:
Eight of Spades

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

First differing element 0:
King of Diamonds
Nine of Spades

Second list contains 62 additional elements.
First extra element 52:
Jack of Diamonds

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

======================================================================
FAIL: test_all_card_instances (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-1ptqrfr/test.py", line 67, in test_all_card_instances
    self.assertIsInstance(card.rank, rank)
AssertionError: <class 'solution.Nine'> is not an instance of <class 'solution.Nine'>

======================================================================
FAIL: test_all_suits_ranks_equal (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-1ptqrfr/test.py", line 92, in test_all_suits_ranks_equal
    self.assertEqual(card.rank, rank())
AssertionError: <class 'solution.Nine'> != <solution.Nine object at 0xb6e43f2c>

======================================================================
FAIL: test_all_to_string (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-1ptqrfr/test.py", line 104, in test_all_to_string
    + " of " + str(card.suit))
AssertionError: 'Nine of Spades' != "<class 'solution.Nine'> of <class 'solution.Spades'>"
- Nine of Spades
+ <class 'solution.Nine'> of <class 'solution.Spades'>


======================================================================
FAIL: test_card_instance (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-1ptqrfr/test.py", line 60, in test_card_instance
    self.assertIsInstance(aos.rank, solution.RANKS["Ace"])
AssertionError: <class 'solution.Ace'> is not an instance of <class 'solution.Ace'>

======================================================================
FAIL: test_suit_rank_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-1ptqrfr/test.py", line 85, in test_suit_rank_equals
    self.assertEqual(aos.rank, solution.RANKS["Ace"]())
AssertionError: <class 'solution.Ace'> != <solution.Ace object at 0xb6e4896c>

----------------------------------------------------------------------
Ran 16 tests in 0.051s

FAILED (failures=8, errors=3)

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

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

+class Rank:
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+RANKS = {'Two': type('Two', (Rank,), {'symbol': '2'}),
+ 'Three': type('Three', (Rank,), {'symbol': '3'}),
+ 'Four': type('Four', (Rank,), {'symbol': '4'}),
+ 'Five': type('Five', (Rank,), {'symbol': '5'}),
+ 'Six': type('Six', (Rank,), {'symbol': '6'}),
+ 'Seven': type('Seven', (Rank,), {'symbol': '7'}),
+ 'Eight': type('Eight', (Rank,), {'symbol': '8'}),
+ 'Nine': type('Nine', (Rank,), {'symbol': '9'}),
+ 'Ten': type('Ten', (Rank,), {'symbol': '10'}),
+ 'Jack': type('Jack', (Rank,), {'symbol': 'J'}),
+ 'Queen': type('Queen', (Rank,), {'symbol': 'Q'}),
+ 'King': type('King', (Rank,), {'symbol': 'K'}),
+ 'Ace': type('Ace', (Rank,), {'symbol': 'A'})}
+
+SUITS = {'Hearts': type('Hearts', (Suit, ), {'color': 'red'}),
+ 'Diamonds': type('Diamonds', (Suit, ), {'color': 'red'}),
+ 'Spades': type('Spades', (Suit, ), {'color': 'black'}),
+ 'Clubs': type('Clubs', (Suit, ), {'color': 'black'})}
+
+
+class Card(object):
+
+ def __init__(self, rank, suit):
+ self.rank = rank
+ self.suit = suit
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return self.rank.__name__ + " of " + self.suit.__name__
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __setitem__(self, index, value):
+ self.collection[index] = value
+
+ def __len__(self):
+ return len(self.collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.pop(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):
+ first_found = next(obj for obj in self.collection if obj == card)
+ return self.collection.index(first_found)
+
+
+def StandardDeck():
+ standard_deck = CardCollection()
+ for card_ranks in RANKS.keys():
+ for card_suits in SUITS.keys():
+ standard_deck.add(str(Card(RANKS[card_ranks], SUITS[card_suits])))
+ return standard_deck.collection
+
+
+def BeloteDeck():
+ belot_deck = CardCollection()
+ for card_ranks in RANKS.keys():
+ if RANKS[card_ranks].symbol not in ['2', '3', '4', '5', '6']:
+ for card_suits in SUITS.keys():
+ belot_deck.add(str(Card(RANKS[card_ranks], SUITS[card_suits])))
+ return belot_deck.collection
+
+
+def SixtySixDeck():
+ six_deck = CardCollection()
+ for card_ranks in RANKS.keys():
+ if RANKS[card_ranks].symbol not in ['2', '3', '4', '5', '6', '7', '8']:
+ for card_suits in SUITS.keys():
+ six_deck.add(str(Card(RANKS[card_ranks], SUITS[card_suits])))
+ return six_deck.collection