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

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

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

Резултати

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

Код

RANK_SYMBOLS = {'K': 'King', '6': 'Six', 'J': 'Jack', '5': 'Five',
'Q': 'Queen', '10': 'Ten', 'A': 'Ace', '3': 'Three',
'8': 'Eight', '4': 'Four',
'2': 'Two', '7': 'Seven', '9': 'Nine'}
SUITS_SYMBOLS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
RANKS_SYMBOLS_2 = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __str__(self):
out = str(RANK_SYMBOLS[self.symbol])
return out
def __eq__(self, other):
return self.symbol == other.symbol
class Suit:
def __init__(self, color):
self.color = color
def __str__(self):
return str(self.color)
def __eq__(self, other):
return self.color == other.color
def make_cls(name, args):
def my_init(self):
Rank.__init__(self, args)
return type(name, (Rank,), {'__init__': my_init})
def make_cls_2(name):
def my_init(self):
Suit.__init__(self, name)
return type(name, (Suit,), {'__init__': my_init})
RANKS = {RANK_SYMBOLS[sym]: make_cls(RANK_SYMBOLS[sym], sym)
for sym in RANK_SYMBOLS.keys()}
SUITS = {suits: make_cls_2(suits) for suits in SUITS_SYMBOLS}
class Card():
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __delattr__(self, *args):
raise AttributeError("can't delete attribute")
def __init__(self, rank, suit):
super(Card, self).__setattr__('rank', rank())
super(Card, self).__setattr__('suit', suit())
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
agr = str(self.suit)
return str(self.rank) + " of " + agr.replace("\'", "")
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
self.collection_index = 0
def add(self, card):
self.collection.append(card)
def __len__(self):
return len(self.collection)
def __getitem__(self, key):
return self.collection[key]
def draw(self, index):
c = self.__getitem__(self.index)
self.collection.remove(c)
return c
def draw_from_top(self):
c = self.__getitem__(len(self.collection)-1)
self.collection.remove(c)
return c
def draw_from_bottom(self):
c = self.__getitem__(0)
self.collection.remove(c)
return c
def top_card(self):
return self.__getitem__(len(self.collection)-1)
def bottom_card(self):
return self.__getitem__(0)
def __iter__(self):
return self
def __next__(self):
if self.collection_index < len(self.collection):
c = self.collection[self.collection_index]
self.collection_index += 1
return c
else:
self.collection_index = 0
raise StopIteration
def __str__(self):
res = []
for car in self.collection:
res.append(str(car))
#return '\n'.join(res)
return str(res)
def index(self, card):
#if card in self.collection:
return self.collection.index(card)
#else:
# raise ValueError
def StandardDeck():
pass
def BelotteDeck():
pass
def SixtySixDeck():
pass

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

E.EF.FEE........
======================================================================
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-pbgrbp/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_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-pbgrbp/test.py", line 150, in test_deck_draw
    self.assertEqual(card, deck.draw(i))
  File "/tmp/d20140407-19315-pbgrbp/solution.py", line 84, in draw
    c = self.__getitem__(self.index)
  File "/tmp/d20140407-19315-pbgrbp/solution.py", line 81, in __getitem__
    return self.collection[key]
TypeError: list indices must be integers, not method

======================================================================
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-pbgrbp/test.py", line 178, in test_sixtysix_deck
    cards = [str(card) for card in solution.SixtySixDeck()]
TypeError: 'NoneType' object is not iterable

======================================================================
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-pbgrbp/test.py", line 170, in test_standard_deck
    cards = [str(card) for card in solution.StandardDeck()]
TypeError: 'NoneType' object is not iterable

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

----------------------------------------------------------------------
Ran 16 tests in 0.029s

FAILED (failures=2, errors=4)

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

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

+RANK_SYMBOLS = {'K': 'King', '6': 'Six', 'J': 'Jack', '5': 'Five',
+ 'Q': 'Queen', '10': 'Ten', 'A': 'Ace', '3': 'Three',
+ '8': 'Eight', '4': 'Four',
+ '2': 'Two', '7': 'Seven', '9': 'Nine'}
+SUITS_SYMBOLS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+RANKS_SYMBOLS_2 = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __str__(self):
+ out = str(RANK_SYMBOLS[self.symbol])
+ return out
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __str__(self):
+ return str(self.color)
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+
+def make_cls(name, args):
+ def my_init(self):
+ Rank.__init__(self, args)
+ return type(name, (Rank,), {'__init__': my_init})
+
+
+def make_cls_2(name):
+ def my_init(self):
+ Suit.__init__(self, name)
+ return type(name, (Suit,), {'__init__': my_init})
+
+RANKS = {RANK_SYMBOLS[sym]: make_cls(RANK_SYMBOLS[sym], sym)
+ for sym in RANK_SYMBOLS.keys()}
+
+SUITS = {suits: make_cls_2(suits) for suits in SUITS_SYMBOLS}
+
+
+class Card():
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+ def __delattr__(self, *args):
+ raise AttributeError("can't delete attribute")
+
+ def __init__(self, rank, suit):
+ super(Card, self).__setattr__('rank', rank())
+ super(Card, self).__setattr__('suit', suit())
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ agr = str(self.suit)
+ return str(self.rank) + " of " + agr.replace("\'", "")
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.collection = collection
+ self.collection_index = 0
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self, key):
+ return self.collection[key]
+
+ def draw(self, index):
+ c = self.__getitem__(self.index)
+ self.collection.remove(c)
+ return c
+
+ def draw_from_top(self):
+ c = self.__getitem__(len(self.collection)-1)
+ self.collection.remove(c)
+ return c
+
+ def draw_from_bottom(self):
+ c = self.__getitem__(0)
+ self.collection.remove(c)
+ return c
+
+ def top_card(self):
+ return self.__getitem__(len(self.collection)-1)
+
+ def bottom_card(self):
+ return self.__getitem__(0)
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if self.collection_index < len(self.collection):
+ c = self.collection[self.collection_index]
+ self.collection_index += 1
+ return c
+ else:
+ self.collection_index = 0
+ raise StopIteration
+
+ def __str__(self):
+ res = []
+ for car in self.collection:
+ res.append(str(car))
+ #return '\n'.join(res)
+ return str(res)
+
+ def index(self, card):
+ #if card in self.collection:
+ return self.collection.index(card)
+ #else:
+ # raise ValueError
+
+
+def StandardDeck():
+ pass
+
+
+def BelotteDeck():
+ pass
+
+
+def SixtySixDeck():
+ pass