Решение на Тесте карти от Моника Ефтимова

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

Към профила на Моника Ефтимова

Резултати

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

Код

class Rank:
def __init__(self):
self.symbol = ''
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
RANKS_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
('Seven', '7'), ('Six', '6'), ('Five', '5'),
('Four', '4'), ('Three', '3'), ('Two', '2'),
('Ace', 'A')]
RANKS = {cls[0]: type(cls[0], (Rank,), {'symbol': cls[1]})
for cls in RANKS_SYMBOLS}
class Suit:
def __init__(self):
self.color = ''
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
SUITS_COLORS = [('Diamonds', 'red'), ('Clubs', 'black'), ('Hearts', 'red'),
('Spades', 'black')]
SUITS = {cls[0]: type(cls[0], (Suit,), {'color': cls[1]})
for cls in SUITS_COLORS}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return '{0.rank} of {0.suit}'.format(self)
def __eq__(self, other):
return len({str(self.rank), str(self.suit),
str(other.rank), str(other.suit)}) == 2
class CardCollection:
pass

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

EEEEEEEE........
======================================================================
ERROR: 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-7v892m/test.py", line 174, in test_belote_deck
    cards = [str(card) for card in solution.BeloteDeck()]
AttributeError: 'module' object has no attribute 'BeloteDeck'

======================================================================
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-7v892m/test.py", line 120, in test_deck_add
    deck.add(card1)
AttributeError: 'CardCollection' object has no attribute 'add'

======================================================================
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-7v892m/test.py", line 138, in test_deck_draw
    deck = solution.CardCollection(self.deck)
TypeError: object.__new__() takes no parameters

======================================================================
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-7v892m/test.py", line 157, in test_deck_index
    deck.add(card1)
AttributeError: 'CardCollection' object has no attribute 'add'

======================================================================
ERROR: 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-7v892m/test.py", line 165, in test_deck_iteration
    deck = solution.CardCollection(self.deck)
TypeError: object.__new__() takes no parameters

======================================================================
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-7v892m/test.py", line 132, in test_deck_order
    deck.add(card1), deck.add(card2), deck.add(card3)
AttributeError: 'CardCollection' object has no attribute 'add'

======================================================================
ERROR: 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-7v892m/test.py", line 178, in test_sixtysix_deck
    cards = [str(card) for card in solution.SixtySixDeck()]
AttributeError: 'module' object has no attribute 'SixtySixDeck'

======================================================================
ERROR: 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-7v892m/test.py", line 170, in test_standard_deck
    cards = [str(card) for card in solution.StandardDeck()]
AttributeError: 'module' object has no attribute 'StandardDeck'

----------------------------------------------------------------------
Ran 16 tests in 0.022s

FAILED (errors=8)

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

Моника обнови решението на 25.03.2014 09:05 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class King(Rank):
+ def __init__(self):
+ self.symbol = 'K'
+
+
+class Queen(Rank):
+ def __init__(self):
+ self.symbol = 'Q'
+
+
+class Jack(Rank):
+ def __init__(self):
+ self.symbol = 'J'
+
+
+class Ten(Rank):
+ def __init__(self):
+ self.symbol = '10'
+
+
+class Nine(Rank):
+ def __init__(self):
+ self.symbol = '9'
+
+
+class Eight(Rank):
+ def __init__(self):
+ self.symbol = '8'
+
+
+class Seven(Rank):
+ def __init__(self):
+ self.symbol = '7'
+
+
+class Six(Rank):
+ def __init__(self):
+ self.symbol = '6'
+
+
+class Five(Rank):
+ def __init__(self):
+ self.symbol = '5'
+
+
+class Four(Rank):
+ def __init__(self):
+ self.symbol = '4'
+
+
+class Three(Rank):
+ def __init__(self):
+ self.symbol = '3'
+
+
+class Two(Rank):
+ def __init__(self):
+ self.symbol = '2'
+
+
+class Ace(Rank):
+ def __init__(self):
+ self.symbol = 'A'
+
+
+RANKS = {'King': King, 'Queen': Queen,
+ 'Jack': Jack, 'Ten': Ten,
+ 'Nine': Nine, 'Eight': Eight,
+ 'Seven': Seven, 'Six': Six,
+ 'Five': Five, 'Four': Four,
+ 'Three': Three, 'Two': Two,
+ 'Ace': Ace}
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+SUITS = {'Diamonds': Diamonds, 'Clubs': Clubs,
+ 'Hearts': Hearts, 'Spades': Spades}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return '{0.rank} of {0.suit}'.format(self)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection:
+ pass

Виждам, че решенето ти не е напълно готово, но въпреки това бих искал да ти дам няколко насоки:

  • Опитай да конструираш подкласовете на Suit и Rank динамично, и ще си спестиш доста код.
  • Когато имплементираш __eq__ не е напълно адекватно да използваш стринговите репрезентации за сравнение на обектите.
  • symbol и color могат също така да бъдат и клас-атрибути

Моника обнови решението на 26.03.2014 15:14 (преди над 10 години)

class Rank:
- def __init__(self, symbol):
- self.symbol = symbol
- def __str__(self):
- return self.__class__.__name__
-
- def __eq__(self, other):
- return self.symbol == other.symbol
-
-
-class King(Rank):
def __init__(self):
- self.symbol = 'K'
+ self.symbol = ''
-
-class Queen(Rank):
- def __init__(self):
- self.symbol = 'Q'
-
-
-class Jack(Rank):
- def __init__(self):
- self.symbol = 'J'
-
-
-class Ten(Rank):
- def __init__(self):
- self.symbol = '10'
-
-
-class Nine(Rank):
- def __init__(self):
- self.symbol = '9'
-
-
-class Eight(Rank):
- def __init__(self):
- self.symbol = '8'
-
-
-class Seven(Rank):
- def __init__(self):
- self.symbol = '7'
-
-
-class Six(Rank):
- def __init__(self):
- self.symbol = '6'
-
-
-class Five(Rank):
- def __init__(self):
- self.symbol = '5'
-
-
-class Four(Rank):
- def __init__(self):
- self.symbol = '4'
-
-
-class Three(Rank):
- def __init__(self):
- self.symbol = '3'
-
-
-class Two(Rank):
- def __init__(self):
- self.symbol = '2'
-
-
-class Ace(Rank):
- def __init__(self):
- self.symbol = 'A'
-
-
-RANKS = {'King': King, 'Queen': Queen,
- 'Jack': Jack, 'Ten': Ten,
- 'Nine': Nine, 'Eight': Eight,
- 'Seven': Seven, 'Six': Six,
- 'Five': Five, 'Four': Four,
- 'Three': Three, 'Two': Two,
- 'Ace': Ace}
-
-
-class Suit:
- def __init__(self, color):
- self.color = color
-
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
- return str(self) == str(other)
+ return self.__class__ == other.__class__
-class Diamonds(Suit):
- def __init__(self):
- self.color = 'red'
+RANKS_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
+ ('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
+ ('Seven', '7'), ('Six', '6'), ('Five', '5'),
+ ('Four', '4'), ('Three', '3'), ('Two', '2'),
+ ('Ace', 'A')]
+RANKS = {cls[0]: type(cls[0], (Rank,), {'symbol': cls[1]})
+ for cls in RANKS_SYMBOLS}
-class Clubs(Suit):
- def __init__(self):
- self.color = 'black'
-
-
-class Hearts(Suit):
- def __init__(self):
- self.color = 'red'
-
-
-class Spades(Suit):
- def __init__(self):
- self.color = 'black'
-
-
-SUITS = {'Diamonds': Diamonds, 'Clubs': Clubs,
- 'Hearts': Hearts, 'Spades': Spades}
-
-
-class Card:
- def __init__(self, rank, suit):
- self.rank = rank()
- self.suit = suit()
-
- def __str__(self):
- return '{0.rank} of {0.suit}'.format(self)
-
- def __eq__(self, other):
- return self.rank == other.rank and self.suit == other.suit
-
-
-class CardCollection:
- pass

Моника обнови решението на 26.03.2014 15:15 (преди над 10 години)

class Rank:
def __init__(self):
self.symbol = ''
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
RANKS_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
('Seven', '7'), ('Six', '6'), ('Five', '5'),
('Four', '4'), ('Three', '3'), ('Two', '2'),
('Ace', 'A')]
RANKS = {cls[0]: type(cls[0], (Rank,), {'symbol': cls[1]})
for cls in RANKS_SYMBOLS}
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return '{0.rank} of {0.suit}'.format(self)
+
+ def __eq__(self, other):
+ return len({str(self.rank), str(self.suit),
+ str(other.rank), str(other.suit)}) == 2
+
+
+class CardCollection:
+ pass

Моника обнови решението на 26.03.2014 15:16 (преди над 10 години)

class Rank:
def __init__(self):
self.symbol = ''
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
RANKS_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
('Seven', '7'), ('Six', '6'), ('Five', '5'),
('Four', '4'), ('Three', '3'), ('Two', '2'),
('Ace', 'A')]
RANKS = {cls[0]: type(cls[0], (Rank,), {'symbol': cls[1]})
for cls in RANKS_SYMBOLS}
+class Suit:
+
+ def __init__(self):
+ self.color = ''
+
+
+
+
+SUITS_COLORS = [('Diamonds', 'red'), ('Clubs', 'black'), ('Hearts', 'red'),
+ ('Spades', 'black')]
+
+SUITS = {cls[0]: type(cls[0], (Suit,), {'color': cls[1]})
+ for cls in SUITS_COLORS}
+
+
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return '{0.rank} of {0.suit}'.format(self)
def __eq__(self, other):
return len({str(self.rank), str(self.suit),
str(other.rank), str(other.suit)}) == 2
class CardCollection:
pass

Моника обнови решението на 26.03.2014 15:17 (преди над 10 години)

class Rank:
def __init__(self):
self.symbol = ''
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__ == other.__class__
RANKS_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
('Seven', '7'), ('Six', '6'), ('Five', '5'),
('Four', '4'), ('Three', '3'), ('Two', '2'),
('Ace', 'A')]
RANKS = {cls[0]: type(cls[0], (Rank,), {'symbol': cls[1]})
for cls in RANKS_SYMBOLS}
class Suit:
def __init__(self):
self.color = ''
-
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
SUITS_COLORS = [('Diamonds', 'red'), ('Clubs', 'black'), ('Hearts', 'red'),
('Spades', 'black')]
SUITS = {cls[0]: type(cls[0], (Suit,), {'color': cls[1]})
for cls in SUITS_COLORS}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return '{0.rank} of {0.suit}'.format(self)
def __eq__(self, other):
return len({str(self.rank), str(self.suit),
str(other.rank), str(other.suit)}) == 2
class CardCollection:
pass