Решение на Тесте карти от Георги Янев

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

Към профила на Георги Янев

Резултати

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

Код

import re
RANK_CARDS = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
'Nine', 'Ten', 'Jack', 'Queen', 'King']
SUIT_CARDS = ['Hearts', 'Clubs', 'Spades', 'Diamonds']
class Rank(object):
symbol = None
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return str(self.symbol)
class Suit(object):
color = None
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return str(self.color)
RANKS = {card: type(card, (Rank,), dict(symbol=card)) for card in RANK_CARDS}
SUITS = {suit: type(suit, (Suit,), dict(color=suit)) for suit in SUIT_CARDS}
class Card():
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __setattr__(self, rank, suit):
if not 'rank' in self.__dict__ or not 'suit' in self.__dict__:
return object.__setattr__(self, rank, suit)
else:
raise AttributeError("Can't set attribute")
def __delattr__(self, *args):
raise AttributeError("Can't delete attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
def __iter__(self):
return iter(self.collection)
def __getitem__(self, key):
return self.collection[key]
def __len__(self):
return len(self.collection)
def draw(self, index):
self.index = index
return self.collection.pop(self.index)
def draw_from_top(self):
return self.collection.pop(len(self.collection) - 1)
def draw_from_bottom(self):
return self.collection.pop()
def top_card(self):
return self.collection[len(self.collection) - 1]
def bottom_card(self):
return list(self.collection)[0]
def add(self, card):
self.card = card
self.collection.append(self.card)
def index(self, card):
self.card = card
if self.card in self.collection:
return self.collection.index(self.card)
else:
raise ValueError(self.card, 'is not in list')
def StandardDeck():
return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in SUITS for
rank in RANKS])
def BeloteDeck():
exclude_cards_re = 'Two|Three|Four|Five|Six'
return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in SUITS for
rank in RANKS if not re.search(exclude_cards_re,
rank)])
def SixtySixDeck():
exclude_cards_re = 'Two|Three|Four|Five|Six|Seven|Eight'
return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in SUITS for
rank in RANKS if not re.search(exclude_cards_re,
rank)])

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

F.FF.FFF........
======================================================================
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-1jqbewz/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['Nine of Clubs', 'Jack of Clu...

First differing element 0:
King of Diamonds
Nine of Clubs

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

======================================================================
FAIL: 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-1jqbewz/test.py", line 145, in test_deck_draw
    self.assertEqual(card, deck.draw_from_bottom())
AssertionError: <solution.Card object at 0xb77f94ec> != <solution.Card object at 0xb77f8eec>

======================================================================
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-1jqbewz/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-1jqbewz/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <solution.Card object at 0xb77f95ac> != <solution.Card object at 0xb780890c>

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

First differing element 0:
King of Diamonds
Nine of Clubs

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

First differing element 0:
King of Diamonds
Nine of Clubs

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

----------------------------------------------------------------------
Ran 16 tests in 0.085s

FAILED (failures=6)

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

Георги обнови решението на 26.03.2014 13:46 (преди около 10 години)

+import re
+
+RANK_CARDS = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
+ 'Nine', 'Ten', 'Jack', 'Queen', 'King']
+SUIT_CARDS = ['Hearts', 'Clubs', 'Spades', 'Diamonds']
+
+
+class Rank(object):
+ symbol = None
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return str(self.symbol)
+
+
+class Suit(object):
+ color = None
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return str(self.color)
+
+RANKS = {card: type(card, (Rank,), dict(symbol=card)) for card in RANK_CARDS}
+SUITS = {suit: type(suit, (Suit,), dict(color=suit)) for suit in SUIT_CARDS}
+
+
+class Card():
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __setattr__(self, rank, suit):
+ if not 'rank' in self.__dict__ or not 'suit' in self.__dict__:
+ return object.__setattr__(self, rank, suit)
+ else:
+ raise AttributeError("Can't set attribute")
+
+ def __delattr__(self, *args):
+ raise AttributeError("Can't delete attribute")
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __getitem__(self, key):
+ return self.collection[key]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def draw(self, index):
+ self.index = index
+ return self.collection.pop(self.index)
+
+ def draw_from_top(self):
+ return self.collection.pop(len(self.collection) - 1)
+
+ def draw_from_bottom(self):
+ return self.collection.pop()
+
+ def top_card(self):
+ return self.collection[len(self.collection) - 1]
+
+ def bottom_card(self):
+ return list(self.collection)[0]
+
+ def add(self, card):
+ self.card = card
+ self.collection.append(self.card)
+
+ def index(self, card):
+ self.card = card
+ if self.card in self.collection:
+ return self.collection.index(self.card)
+ else:
+ raise ValueError(self.card, 'is not in list')
+
+
+def StandardDeck():
+ return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in SUITS for
+ rank in RANKS])
+
+
+def BeloteDeck():
+ exclude_cards_re = 'Two|Three|Four|Five|Six'
+ return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in SUITS for
+ rank in RANKS if not re.search(exclude_cards_re,
+ rank)])
+
+
+def SixtySixDeck():
+ exclude_cards_re = 'Two|Three|Four|Five|Six|Seven|Eight'
+ return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in SUITS for
+ rank in RANKS if not re.search(exclude_cards_re,
+ rank)])