Решение на Тесте карти от Тихомир Кожухарски

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

Към профила на Тихомир Кожухарски

Резултати

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

Код

# Used list, so we can get the sorting order from here
RANKS_INFO = [
{'symbol': 'K', 'name': 'King'},
{'symbol': 'Q', 'name': 'Queen'},
{'symbol': 'J', 'name': 'Jack'},
{'symbol': '10', 'name': 'Ten'},
{'symbol': '9', 'name': 'Nine'},
{'symbol': '8', 'name': 'Eight'},
{'symbol': '7', 'name': 'Seven'},
{'symbol': '6', 'name': 'Six'},
{'symbol': '5', 'name': 'Five'},
{'symbol': '4', 'name': 'Four'},
{'symbol': '3', 'name': 'Three'},
{'symbol': '2', 'name': 'Two'},
{'symbol': 'A', 'name': 'Ace'},
]
def is_rank_equal(self, other):
return self.symbol == other.symbol
def stringify_object(self):
return self.__name
def stringify_object(self):
return self.__name
stringify_rank = stringify_object
class Rank:
def __init__(self):
self.symbol = ""
def __str__(self):
return ""
RANKS = dict()
for rank in RANKS_INFO:
RANKS[rank['name']] = type(
rank['name'], (Rank,),
{
'symbol': rank['symbol'],
'__name': rank['name'],
'__eq__': is_rank_equal,
'__str__': stringify_rank
}
)
#setattr(RANKS[name], '__str__', staticmethod(stringify_rank))
#'__str__': stringify_rank
#RANKS[name].__str__ = stringify_rank
# Used list, so we can get the sorting order from here
SUIT_VALUES = [
{'name': 'Diamonds', 'color': 'red'},
{'name': 'Hearts', 'color': 'red'},
{'name': 'Spades', 'color': 'black'},
{'name': 'Clubs', 'color': 'black'}
]
class Suit(object):
def __init__(self):
self.color = ''
def is_suit_equal(self, other):
return self.__name == other.__name
stringify_suit = stringify_object
SUITS = dict()
for suit in SUIT_VALUES:
SUITS[suit['name']] = type(
suit['name'], (Suit,),
{
'color': suit['color'],
'__name': suit['name'],
'__eq__': is_suit_equal,
'__str__': stringify_suit
}
)
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __str__(self):
return '{0} of {1}'.format(
str(self.rank), str(self.suit)
)
class CardIterator:
def __init__(self, cards):
self.__cards = cards
self.__current_index = 0
def __iter__(self):
return self
def __next__(self):
if (self.__current_index >= len(self.__cards)):
raise StopIteration()
index = self.__current_index
self.__current_index += 1
return self.__cards[index]
class CardCollection:
def __init__(self, collection=list()):
self.__cards = list(collection)
def __iter__(self):
return CardIterator(self.__cards)
def __getitem__(self, num):
return self.__cards[num]
def __len__(self):
return len(self.__cards)
def draw(self, index):
return self.__cards.pop(index)
def draw_from_top(self):
return self.__cards.pop()
def draw_from_bottom(self):
return self.__cards.pop(0)
def top_card(self):
return self.__cards[len(self.__cards) - 1]
def bottom_card(self):
return self.__cards[0]
def add(self, card):
self.__cards.append(card)
def index(self, card):
return self.__cards.index(card)
def GenerateDeck(exclusions):
cards = CardCollection()
for suit in SUIT_VALUES:
for rank in RANKS_INFO:
if rank['symbol'] in exclusions:
continue
cards.add(Card(RANKS[rank['name']], SUITS[suit['name']]))
return cards
def StandardDeck():
return GenerateDeck([])
def StandardDeck():
return GenerateDeck(['2', '3', '4', '5', '6'])
def SixtySixDeck():
return GenerateDeck(['2', '3', '4', '5', '6', '7', '8'])

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

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

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

First differing element 6:
King of Clubs
King of Hearts

Diff is 687 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-185s595/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 7:
Six of Diamonds
Ace of Diamonds

First list contains 20 additional elements.
First extra element 32:
Seven of Hearts

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

----------------------------------------------------------------------
Ran 16 tests in 0.032s

FAILED (failures=2, errors=1)

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

Тихомир обнови решението на 25.03.2014 23:37 (преди около 10 години)

+# Used list, so we can get the sorting order from here
+RANKS_INFO = [
+ {'symbol': 'K', 'name': 'King'},
+ {'symbol': 'Q', 'name': 'Queen'},
+ {'symbol': 'J', 'name': 'Jack'},
+ {'symbol': '10', 'name': 'Ten'},
+ {'symbol': '9', 'name': 'Nine'},
+ {'symbol': '8', 'name': 'Eight'},
+ {'symbol': '7', 'name': 'Seven'},
+ {'symbol': '6', 'name': 'Six'},
+ {'symbol': '5', 'name': 'Five'},
+ {'symbol': '4', 'name': 'Four'},
+ {'symbol': '3', 'name': 'Three'},
+ {'symbol': '2', 'name': 'Two'},
+ {'symbol': 'A', 'name': 'Ace'},
+]
+
+
+def is_rank_equal(self, other):
+ return self.symbol == other.symbol
+
+
+def stringify_object(self):
+ return self.__name
+
+
+def stringify_object(self):
+ return self.__name
+
+
+stringify_rank = stringify_object
+
+
+class Rank:
+ def __init__(self):
+ self.symbol = ""
+
+ def __str__(self):
+ return ""
+
+
+RANKS = dict()
+for rank in RANKS_INFO:
+ RANKS[rank['name']] = type(
+ rank['name'], (Rank,),
+ {
+ 'symbol': rank['symbol'],
+ '__name': rank['name'],
+ '__eq__': is_rank_equal,
+ '__str__': stringify_rank
+ }
+ )
+ #setattr(RANKS[name], '__str__', staticmethod(stringify_rank))
+ #'__str__': stringify_rank
+ #RANKS[name].__str__ = stringify_rank
+
+
+# Used list, so we can get the sorting order from here
+SUIT_VALUES = [
+ {'name': 'Diamonds', 'color': 'red'},
+ {'name': 'Hearts', 'color': 'red'},
+ {'name': 'Spades', 'color': 'black'},
+ {'name': 'Clubs', 'color': 'black'}
+]
+
+
+class Suit(object):
+ def __init__(self):
+ self.color = ''
+
+
+def is_suit_equal(self, other):
+ return self.__name == other.__name
+
+stringify_suit = stringify_object
+
+SUITS = dict()
+for suit in SUIT_VALUES:
+ SUITS[suit['name']] = type(
+ suit['name'], (Suit,),
+ {
+ 'color': suit['color'],
+ '__name': suit['name'],
+ '__eq__': is_suit_equal,
+ '__str__': stringify_suit
+ }
+ )
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return (self.rank, self.suit) == (other.rank, other.suit)
+
+ def __str__(self):
+ return '{0} of {1}'.format(
+ str(self.rank), str(self.suit)
+ )
+
+
+class CardIterator:
+ def __init__(self, cards):
+ self.__cards = cards
+ self.__current_index = 0
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if (self.__current_index >= len(self.__cards)):
+ raise StopIteration()
+
+ index = self.__current_index
+ self.__current_index += 1
+
+ return self.__cards[index]
+
+
+class CardCollection:
+ def __init__(self, collection=list()):
+ self.__cards = list(collection)
+
+ def __iter__(self):
+ return CardIterator(self.__cards)
+
+ def __getitem__(self, num):
+ return self.__cards[num]
+
+ def __len__(self):
+ return len(self.__cards)
+
+ def draw(self, index):
+ return self.__cards.pop(index)
+
+ def draw_from_top(self):
+ return self.__cards.pop()
+
+ def draw_from_bottom(self):
+ return self.__cards.pop(0)
+
+ def top_card(self):
+ return self.__cards[len(self.__cards) - 1]
+
+ def bottom_card(self):
+ return self.__cards[0]
+
+ def add(self, card):
+ self.__cards.append(card)
+
+ def index(self, card):
+ return self.__cards.index(card)
+
+
+def GenerateDeck(exclusions):
+ cards = CardCollection()
+
+ for suit in SUIT_VALUES:
+ for rank in RANKS_INFO:
+ if rank['symbol'] in exclusions:
+ continue
+ cards.add(Card(RANKS[rank['name']], SUITS[suit['name']]))
+
+ return cards
+
+
+def StandardDeck():
+ return GenerateDeck([])
+
+
+def StandardDeck():
+ return GenerateDeck(['2', '3', '4', '5', '6'])
+
+
+def SixtySixDeck():
+ return GenerateDeck(['2', '3', '4', '5', '6', '7', '8'])