Решение на Тесте карти от Емануел Стоянов

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

Към профила на Емануел Стоянов

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.name
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
return self.name
class Two(Rank):
def __init__(self):
super().__init__('2')
name = 'Two'
class Three(Rank):
def __init__(self):
super().__init__('3')
name = 'Three'
class Four(Rank):
def __init__(self):
super().__init__('4')
name = 'Four'
class Five(Rank):
def __init__(self):
super().__init__('5')
name = 'Five'
class Six(Rank):
def __init__(self):
super().__init__('6')
name = 'Six'
class Seven(Rank):
def __init__(self):
super().__init__('7')
name = 'Seven'
class Eight(Rank):
def __init__(self):
super().__init__('8')
name = 'Eight'
class Nine(Rank):
def __init__(self):
super().__init__('9')
name = 'Nine'
class Ten(Rank):
def __init__(self):
super().__init__('T')
name = 'Ten'
class Jack(Rank):
def __init__(self):
super().__init__('J')
name = 'Jack'
class Queen(Rank):
def __init__(self):
super().__init__('Q')
name = 'Queen'
class King(Rank):
def __init__(self):
super().__init__('K')
name = 'King'
class Ace(Rank):
def __init__(self):
super().__init__('A')
name = 'Ace'
RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
RANKS_ORDERED = OrderedDict([('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 Hearts(Suit):
def __init__(self):
super().__init__('red')
name = 'Hearts'
class Diamonds(Suit):
def __init__(self):
super().__init__('red')
name = 'Diamonds'
class Spades(Suit):
def __init__(self):
super().__init__('black')
name = 'Spades'
class Clubs(Suit):
def __init__(self):
super().__init__('black')
name = 'Clubs'
SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
'Diamonds': Diamonds, 'Spades': Spades}
SUITS_ORDERED = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs),
('Hearts', Hearts), ('Spades', Spades)])
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._rank == other._rank and self._suit == other._suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return "<Card {}>".format(str(self))
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, i):
return self.collection[i]
def draw(self, index):
return self.collection.pop(index)
def draw_from_bottom(self):
return self.draw(0)
def draw_from_top(self):
return self.draw(len(self.collection) - 1)
def bottom_card(self):
return self.collection[0]
def top_card(self):
return self.collection[len(self.collection) - 1]
def add(self, card):
self.collection.append(card)
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else:
raise ValueError('<Card {}> is not in list'.format(str(card)))
def __iter__(self):
return iter(self.collection)
def __len__(self):
return len(self.collection)
def StandardDeck():
deck = []
for suit in SUITS_ORDERED:
for rank in RANKS_ORDERED:
deck.append(Card(RANKS[rank], SUITS[suit]))
return deck

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

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

======================================================================
FAIL: 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-aeay96/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
AssertionError: 0 != 2

======================================================================
FAIL: 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-aeay96/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <Card Nine of Diamonds> != <Card Ten of Clubs>

----------------------------------------------------------------------
Ran 16 tests in 0.024s

FAILED (failures=2, errors=2)

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

Емануел обнови решението на 26.03.2014 16:48 (преди над 10 години)

+from collections import OrderedDict
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.name
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.name
+
+
+class Two(Rank):
+ def __init__(self):
+ super().__init__('2')
+
+ name = 'Two'
+
+
+class Three(Rank):
+ def __init__(self):
+ super().__init__('3')
+
+ name = 'Three'
+
+
+class Four(Rank):
+ def __init__(self):
+ super().__init__('4')
+
+ name = 'Four'
+
+
+class Five(Rank):
+ def __init__(self):
+ super().__init__('5')
+
+ name = 'Five'
+
+
+class Six(Rank):
+ def __init__(self):
+ super().__init__('6')
+
+ name = 'Six'
+
+
+class Seven(Rank):
+ def __init__(self):
+ super().__init__('7')
+
+ name = 'Seven'
+
+
+class Eight(Rank):
+ def __init__(self):
+ super().__init__('8')
+
+ name = 'Eight'
+
+
+class Nine(Rank):
+ def __init__(self):
+ super().__init__('9')
+
+ name = 'Nine'
+
+
+class Ten(Rank):
+ def __init__(self):
+ super().__init__('T')
+
+ name = 'Ten'
+
+
+class Jack(Rank):
+ def __init__(self):
+ super().__init__('J')
+
+ name = 'Jack'
+
+
+class Queen(Rank):
+ def __init__(self):
+ super().__init__('Q')
+
+ name = 'Queen'
+
+
+class King(Rank):
+ def __init__(self):
+ super().__init__('K')
+
+ name = 'King'
+
+
+class Ace(Rank):
+ def __init__(self):
+ super().__init__('A')
+
+ name = 'Ace'
+
+
+RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
+ 'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
+
+RANKS_ORDERED = OrderedDict([('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 Hearts(Suit):
+ def __init__(self):
+ super().__init__('red')
+
+ name = 'Hearts'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ super().__init__('red')
+
+ name = 'Diamonds'
+
+
+class Spades(Suit):
+ def __init__(self):
+ super().__init__('black')
+
+ name = 'Spades'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ super().__init__('black')
+
+ name = 'Clubs'
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Diamonds': Diamonds, 'Spades': Spades}
+
+SUITS_ORDERED = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs),
+ ('Hearts', Hearts), ('Spades', Spades)])
+
+
+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._rank == other._rank and self._suit == other._suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return "<Card {}>".format(str(self))
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def draw_from_top(self):
+ return self.draw(len(self.collection) - 1)
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def top_card(self):
+ return self.collection[len(self.collection) - 1]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ if card in self.collection:
+ return self.collection.index(card)
+ else:
+ raise ValueError('<Card {}> is not in list'.format(str(card)))
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def StandardDeck():
+ deck = []
+ for suit in SUITS_ORDERED:
+ for rank in RANKS_ORDERED:
+ deck.append(Card(RANKS[rank], SUITS[suit]))
+ return deck