Решение на Тесте карти от Йордан Петров

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

Към профила на Йордан Петров

Резултати

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

Код

from collections import OrderedDict
CARDS_RANKS_AND_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack' , 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'), ('Seven', '7'),
('Six', '6'), ('Five', '5'), ('Four' , '4'), ('Tree' , '3'),
('Two', '2'), ('Ace' , 'A')]
CARD_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
class Rank:
symbol = None
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
color = None
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return self.__class__.__name__
RANKS = OrderedDict([(name, type(name, (Rank,), {'symbol': rank}))
for name, rank in CARDS_RANKS_AND_SYMBOLS])
SUITS = OrderedDict([(color, type(color, (Rank,), {'color': color}))
for color in CARD_SUITS])
class Card():
def __init__(self, card_rank, card_suit):
object.__setattr__(self, 'rank', card_rank())
object.__setattr__(self, 'suit', card_suit())
def __setattr__(self, *attr):
raise AttributeError("can't set attribute")
def __str__(self):
return ' of '.join([str(self.rank), str(self.suit)])
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection(list):
draw = list.pop
def draw_from_top(self):
return self.pop()
def draw_from_bottom(self):
return self.pop(0)
def top_card(self):
return self[-1]
def bottom_card(self):
return self[0]
def add(self, card):
self.append(card)
def StandardDeck():
cards = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values()]
return CardCollection(cards)
def BeloteDeck():
rank_classes = RANKS.values()
ranks_in_deck = rank_classes[:7]
ranks_in_deck.append(rank_classes[-1])
cards = [Card(rank, suit) for suit in SUITS.values()
for rank in ranks_in_deck]
return CardCollection(cards)
def SixtySixDeck():
rank_classes = RANKS.values()
ranks_in_deck = rank_classes[:9]
ranks_in_deck.append(rank_classes[-1])
cards = [Card(rank, suit) for suit in SUITS.values()
for rank in ranks_in_deck]
return CardCollection(cards)

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

E.....EF....F...
======================================================================
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-1y024tn/test.py", line 174, in test_belote_deck
    cards = [str(card) for card in solution.BeloteDeck()]
  File "/tmp/d20140407-19315-1y024tn/solution.py", line 78, in BeloteDeck
    ranks_in_deck = rank_classes[:7]
TypeError: 'ValuesView' object is not subscriptable

======================================================================
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-1y024tn/test.py", line 178, in test_sixtysix_deck
    cards = [str(card) for card in solution.SixtySixDeck()]
  File "/tmp/d20140407-19315-1y024tn/solution.py", line 86, in SixtySixDeck
    ranks_in_deck = rank_classes[:9]
TypeError: 'ValuesView' object is not subscriptable

======================================================================
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-1y024tn/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 10:
Three of Diamonds
Tree of Diamonds

Diff is 1222 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-1y024tn/test.py", line 74, in test_card_equals
    self.assertNotEqual(aos1, solution.Card(solution.RANKS["Ace"], solution.SUITS["Hearts"]))
AssertionError: <solution.Card object at 0xb7855acc> == <solution.Card object at 0xb785580c>

----------------------------------------------------------------------
Ran 16 tests in 0.030s

FAILED (failures=2, errors=2)

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

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

+from collections import OrderedDict
+
+
+CARDS_RANKS_AND_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack' , 'J'),
+ ('Ten', '10'), ('Nine', '9'), ('Eight', '8'), ('Seven', '7'),
+ ('Six', '6'), ('Five', '5'), ('Four' , '4'), ('Tree' , '3'),
+ ('Two', '2'), ('Ace' , 'A')]
+CARD_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+class Rank:
+ symbol = None
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ color = None
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+RANKS = OrderedDict([(name, type(name, (Rank,), {'symbol': rank}))
+ for name, rank in CARDS_RANKS_AND_SYMBOLS])
+SUITS = OrderedDict([(color, type(color, (Rank,), {'color': color}))
+ for color in CARD_SUITS])
+
+class Card():
+
+ def __init__(self, card_rank, card_suit):
+ object.__setattr__(self, 'rank', card_rank())
+ object.__setattr__(self, 'suit', card_suit())
+
+ def __setattr__(self, *attr):
+ raise AttributeError("can't set attribute")
+
+ def __str__(self):
+ return ' of '.join([str(self.rank), str(self.suit)])
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection(list):
+ draw = list.pop
+
+ def draw_from_top(self):
+ return self.pop()
+
+ def draw_from_bottom(self):
+ return self.pop(0)
+
+ def top_card(self):
+ return self[-1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def add(self, card):
+ self.append(card)
+
+
+def StandardDeck():
+ cards = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values()]
+ return CardCollection(cards)
+
+def BeloteDeck():
+ rank_classes = RANKS.values()
+ ranks_in_deck = rank_classes[:7]
+ ranks_in_deck.append(rank_classes[-1])
+ cards = [Card(rank, suit) for suit in SUITS.values()
+ for rank in ranks_in_deck]
+ return CardCollection(cards)
+
+def SixtySixDeck():
+ rank_classes = RANKS.values()
+ ranks_in_deck = rank_classes[:9]
+ ranks_in_deck.append(rank_classes[-1])
+ cards = [Card(rank, suit) for suit in SUITS.values()
+ for rank in ranks_in_deck]
+ return CardCollection(cards)