Решение на Тесте карти от Антон Стоянов

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

Към профила на Антон Стоянов

Резултати

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

Код

#!/usr/bin/python
#-*- coding: cp1251 -*-
#====================================RANK COLLECTION============================
class Rank:
def __init__(self):
self.symbol = ''
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return self.__class__.__name__
class Two(Rank):
pass
class Three(Rank):
pass
class Four(Rank):
pass
class Five(Rank):
pass
class Six(Rank):
pass
class Seven(Rank):
pass
class Eight(Rank):
pass
class Nine(Rank):
pass
class Ten(Rank):
pass
class Jack(Rank):
pass
class Queen(Rank):
pass
class Ace(Rank):
pass
class King(Rank):
pass
RANKS = {Ace.__name__:Ace,
Two.__name__:Two,
Three.__name__:Three,
Four.__name__:Four,
Five.__name__:Five,
Six.__name__:Six,
Seven.__name__:Seven,
Eight.__name__:Eight,
Nine.__name__:Nine,
Ten.__name__:Ten,
Jack.__name__:Jack,
Queen.__name__:Queen,
King.__name__:King}
#===============================SUIT COLLECTION=================================
class Suit:
def __init__(self):
self.color = ''
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return self.__class__.__name__
class Clubs(Suit):
pass
class Diamonds(Suit):
pass
class Hearts(Suit):
pass
class Spades(Suit):
pass
SUITS = {Clubs.__name__:Clubs,
Diamonds.__name__:Diamonds,
Hearts.__name__:Hearts,
Spades.__name__:Spades}
#===============================CARDS REPRESENTATION============================
class Card:
def __init__(self, rank, suit):
self._rank = rank
self._suit = suit
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return self._rank.__name__+' of '+self._suit.__name__
#================================CARDS COLLECTION===============================
class CardCollection:
def __init__(self, collection):
self._deck = list(collection)
def __getitem__(self, i):
return 'Card '+ str((self._deck)[i])
def __str__(self):
if self._deck:
rep = ""
for card in self._deck:
rep += '<Card '+str(card) + '>'
else:
rep = "<empty>"
return rep
@property
def deck(self):
return self._deck
#Метода draw връща картата на определен индекс и я премахва от колекцията
def draw(self, index):
self.__custome_delete(index)
def draw_from_top(self):
return self.__custome_delete(len(self._deck)-1)
def draw_from_bottom(self):
return self.__custome_delete(0)
def top_card(self):
return self._deck[(len(self._deck)-1)]
def bottom_card(self):
return self._deck[0]
#Още карти могат да се добавят с метода add, Метода add добавя карти отгоре
def add(self, card):
self._deck.append(card)
#Метода index търси за определена карта в тестето и връща индекса и.
def index(self, card):
try:
return self._deck.index(card)
except ValueError:
return ("ValueError: {0} is not in list".format(card))
def __custome_delete(self, element_index):
temp = self._deck[element_index]
del self._deck[element_index]
return temp
#================================Standard Deck===============================
def StandardDeck():
Collection = []
for suit_key in SUITS.items():
for ranks_key in RANKS.items():
temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
Collection.append(temp)
return CardCollection(Collection)
def BeloteDeck():
Collection = []
for suit_key in SUITS.items():
for ranks_key in RANKS.items():
if(ranks_key[0] not in ['Two', 'Three', 'Four','Five','Six']):
temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
Collection.append(temp)
return CardCollection(Collection)
def SixtySixDeck():
Collection = []
for suit_key in SUITS.items():
for ranks_key in RANKS.items():
if(ranks_key[0] not in ['Two', 'Three', 'Four','Five',
'Six','Seven','Eight']):
temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
Collection.append(temp)
return CardCollection(Collection)

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

FEEEFEFFF.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-mnddyu/test.py", line 117, in test_deck_add
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

======================================================================
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-mnddyu/test.py", line 142, in test_deck_draw
    self.assertEqual(len(deck), 51)
TypeError: object of type 'CardCollection' has no len()

======================================================================
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-mnddyu/test.py", line 154, in test_deck_index
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

======================================================================
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-mnddyu/test.py", line 129, in test_deck_order
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

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

First differing element 0:
King of Diamonds
Card Nine of Hearts

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

======================================================================
FAIL: test_deck_iteration (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-mnddyu/test.py", line 167, in test_deck_iteration
    self.assertIsInstance(card, solution.Card)
AssertionError: 'Card Nine of Hearts' is not an instance of <class 'solution.Card'>

======================================================================
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-mnddyu/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['Card Nine of Hearts', 'Card ...

First differing element 0:
King of Diamonds
Card Nine of Hearts

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

First differing element 0:
King of Diamonds
Card Nine of Hearts

Diff is 2758 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-mnddyu/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-mnddyu/test.py", line 92, in test_all_suits_ranks_equal
    self.assertEqual(card.rank, rank())
AssertionError: <class 'solution.Nine'> != <solution.Nine object at 0xb7840bac>

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


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

----------------------------------------------------------------------
Ran 16 tests in 0.294s

FAILED (failures=9, errors=4)

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

Антон обнови решението на 26.03.2014 16:25 (преди над 10 години)

+#!/usr/bin/python
+#-*- coding: cp1251 -*-
+
+#====================================RANK COLLECTION============================
+class Rank:
+ def __init__(self):
+ self.symbol = ''
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+ def __str__(self):
+ return self.__class__.__name__
+
+class Two(Rank):
+ pass
+
+class Three(Rank):
+ pass
+
+class Four(Rank):
+ pass
+
+class Five(Rank):
+ pass
+
+class Six(Rank):
+ pass
+
+class Seven(Rank):
+ pass
+
+class Eight(Rank):
+ pass
+
+class Nine(Rank):
+ pass
+
+class Ten(Rank):
+ pass
+
+class Jack(Rank):
+ pass
+
+class Queen(Rank):
+ pass
+
+class Ace(Rank):
+ pass
+
+class King(Rank):
+ pass
+
+RANKS = {Ace.__name__:Ace,
+ Two.__name__:Two,
+ Three.__name__:Three,
+ Four.__name__:Four,
+ Five.__name__:Five,
+ Six.__name__:Six,
+ Seven.__name__:Seven,
+ Eight.__name__:Eight,
+ Nine.__name__:Nine,
+ Ten.__name__:Ten,
+ Jack.__name__:Jack,
+ Queen.__name__:Queen,
+ King.__name__:King}
+
+#===============================SUIT COLLECTION=================================
+class Suit:
+ def __init__(self):
+ self.color = ''
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+ def __str__(self):
+ return self.__class__.__name__
+
+class Clubs(Suit):
+ pass
+
+class Diamonds(Suit):
+ pass
+
+class Hearts(Suit):
+ pass
+
+class Spades(Suit):
+ pass
+
+SUITS = {Clubs.__name__:Clubs,
+ Diamonds.__name__:Diamonds,
+ Hearts.__name__:Hearts,
+ Spades.__name__:Spades}
+
+#===============================CARDS REPRESENTATION============================
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank
+ self._suit = suit
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+
+ def __str__(self):
+ return self._rank.__name__+' of '+self._suit.__name__
+
+#================================CARDS COLLECTION===============================
+class CardCollection:
+ def __init__(self, collection):
+ self._deck = list(collection)
+
+ def __getitem__(self, i):
+ return 'Card '+ str((self._deck)[i])
+
+ def __str__(self):
+ if self._deck:
+ rep = ""
+ for card in self._deck:
+ rep += '<Card '+str(card) + '>'
+ else:
+ rep = "<empty>"
+ return rep
+
+ @property
+ def deck(self):
+ return self._deck
+
+ #Метода draw връща картата на определен индекс и я премахва от колекцията
+ def draw(self, index):
+ self.__custome_delete(index)
+
+ def draw_from_top(self):
+ return self.__custome_delete(len(self._deck)-1)
+
+ def draw_from_bottom(self):
+ return self.__custome_delete(0)
+
+ def top_card(self):
+ return self._deck[(len(self._deck)-1)]
+
+ def bottom_card(self):
+ return self._deck[0]
+
+ #Още карти могат да се добавят с метода add, Метода add добавя карти отгоре
+ def add(self, card):
+ self._deck.append(card)
+
+ #Метода index търси за определена карта в тестето и връща индекса и.
+ def index(self, card):
+ try:
+ return self._deck.index(card)
+ except ValueError:
+ return ("ValueError: {0} is not in list".format(card))
+
+ def __custome_delete(self, element_index):
+ temp = self._deck[element_index]
+ del self._deck[element_index]
+ return temp
+
+#================================Standard Deck===============================
+
+def StandardDeck():
+ Collection = []
+ for suit_key in SUITS.items():
+ for ranks_key in RANKS.items():
+ temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
+ Collection.append(temp)
+ return CardCollection(Collection)
+
+def BeloteDeck():
+ Collection = []
+ for suit_key in SUITS.items():
+ for ranks_key in RANKS.items():
+ if(ranks_key[0] not in ['Two', 'Three', 'Four','Five','Six']):
+ temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
+ Collection.append(temp)
+ return CardCollection(Collection)
+
+def SixtySixDeck():
+ Collection = []
+ for suit_key in SUITS.items():
+ for ranks_key in RANKS.items():
+ if(ranks_key[0] not in ['Two', 'Three', 'Four','Five',
+ 'Six','Seven','Eight']):
+ temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
+ Collection.append(temp)
+ return CardCollection(Collection)
+
+
+
+

Всичко би трябвало да работи само не можах да реализирам OrderedDict или нещо подобно. И не можах да ги сортирам по правилният начин. Може би ако вие го довършите (да подредите dict) и тогава да тествате. Поздрави.