Решение на Тесте карти от Мария Кърчева

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

Към профила на Мария Кърчева

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 14 успешни тест(а)
  • 2 неуспешни тест(а)

Код

class Rank:
@classmethod
def __eq__(cls, other):
return cls == other
@classmethod
def __str__(cls):
return cls.__name__
class Suit:
@classmethod
def __eq__(cls, other):
return cls == other
@classmethod
def __str__(cls):
return cls.__name__
SYMBOLS = {
'Two': '2',
'Three': '3',
'Four': '4',
'Five': '5',
'Six': '6',
'Seven': '7',
'Eight': '8',
'Nine': '9',
'Ten': '10',
'Jack': 'J',
'Queen': 'Q',
'King': 'K',
'Ace': 'A',
}
RANK_KEYS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
SUIT_KEYS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
COLORS = {'Diamonds': 'red', 'Clubs': 'black',
'Hearts': 'red', 'Spades' : 'black'}
RANKS = {name: type(name, (Rank, object), {'symbol': symbol}) for name, symbol in SYMBOLS.items()}
SUITS = {name: type(name, (Suit, object), {'color': color}) for name, color in COLORS.items()}
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __iter__(self):
return iter(self.collection)
def __len__(self):
return len(self.collection)
def __getitem__(self,index):
return self.collection[index]
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
return self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def StandardDeck():
return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_KEYS
for rank_name in RANK_KEYS])
def BeloteDeck():
rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_KEYS
for rank_name in rank_names])
def SixtySixDeck():
rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_KEYS
for rank_name in rank_names])
class Card(tuple):
def __new__(cls, rank, suit):
return tuple.__new__(cls, (rank(), suit()))
@property
def rank(self):
return self[0]
@property
def suit(self):
return self[1]
def __str__(self):
return type(self[0]).__name__ + ' of ' + type(self[1]).__name__
def __setattr__(self, *ignored):
raise AttributeError
def __delattr__(self, *ignored):
raise AttributeError

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

...F.F..........
======================================================================
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-a7c404/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-a7c404/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: (<solution.Five object at 0xb785d52c>, <solution.Spades object at 0xb785d46c>) != (<solution.Three object at 0xb780388c>, <solution.Diamonds object at 0xb78038ac>)

----------------------------------------------------------------------
Ran 16 tests in 0.021s

FAILED (failures=2)

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

Мария обнови решението на 26.03.2014 16:29 (преди около 10 години)

+import collections
+class Rank:
+ @classmethod
+ def __eq__(cls, other):
+ return cls == other
+
+ @classmethod
+ def __str__(cls):
+ return cls.__name__
+
+
+class Suit:
+ @classmethod
+ def __eq__(cls, other):
+ return cls == other
+
+ @classmethod
+ def __str__(cls):
+ return cls.__name__
+
+
+SYMBOLS = {
+ 'Two': '2',
+ 'Three': '3',
+ 'Four': '4',
+ 'Five': '5',
+ 'Six': '6',
+ 'Seven': '7',
+ 'Eight': '8',
+ 'Nine': '9',
+ 'Ten': '10',
+ 'Jack': 'J',
+ 'Queen': 'Q',
+ 'King': 'K',
+ 'Ace': 'A',
+}
+
+RANK_KEYS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+SUIT_KEYS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+COLORS = {'Diamonds': 'red', 'Clubs': 'black',
+ 'Hearts': 'red', 'Spades' : 'black'}
+
+RANKS = {name: type(name, (Rank, object), {'symbol': symbol}) for name, symbol in SYMBOLS.items()}
+
+SUITS = {name: type(name, (Suit, object), {'color': color}) for name, color in COLORS.items()}
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self,index):
+ return self.collection[index]
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.pop(0)
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ return self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+
+
+def StandardDeck():
+ return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_KEYS
+ for rank_name in RANK_KEYS])
+
+
+def BeloteDeck():
+ rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_KEYS
+ for rank_name in rank_names])
+
+
+def SixtySixDeck():
+ rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_KEYS
+ for rank_name in rank_names])
+# class Card:
+# def __init__(self, rank, suit):
+# self.__rank, self.__suit = rank, suit
+# @property
+# def rank(self):
+# return self.__rank
+# @property
+# def suit(self):
+# return self.__suit
+
+# Card = collections.namedtuple('Card',['rank', 'suit'])
+
+class Card(tuple):
+ def __new__(cls, rank, suit):
+ return tuple.__new__(cls, (rank(), suit()))
+
+ @property
+ def rank(self):
+ return self[0]
+
+ @property
+ def suit(self):
+ return self[1]
+
+ def __str__(self):
+ return type(self[0]).__name__ + ' of ' + type(self[1]).__name__
+
+ def __setattr__(self, *ignored):
+ raise AttributeError
+
+ def __delattr__(self, *ignored):
+ raise AttributeError

Мария обнови решението на 26.03.2014 16:31 (преди около 10 години)

-import collections
class Rank:
@classmethod
def __eq__(cls, other):
return cls == other
@classmethod
def __str__(cls):
return cls.__name__
class Suit:
@classmethod
def __eq__(cls, other):
return cls == other
@classmethod
def __str__(cls):
return cls.__name__
SYMBOLS = {
'Two': '2',
'Three': '3',
'Four': '4',
'Five': '5',
'Six': '6',
'Seven': '7',
'Eight': '8',
'Nine': '9',
'Ten': '10',
'Jack': 'J',
'Queen': 'Q',
'King': 'K',
'Ace': 'A',
}
RANK_KEYS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
SUIT_KEYS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
COLORS = {'Diamonds': 'red', 'Clubs': 'black',
'Hearts': 'red', 'Spades' : 'black'}
RANKS = {name: type(name, (Rank, object), {'symbol': symbol}) for name, symbol in SYMBOLS.items()}
SUITS = {name: type(name, (Suit, object), {'color': color}) for name, color in COLORS.items()}
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __iter__(self):
return iter(self.collection)
def __len__(self):
return len(self.collection)
def __getitem__(self,index):
return self.collection[index]
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
return self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def StandardDeck():
return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_KEYS
for rank_name in RANK_KEYS])
def BeloteDeck():
rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_KEYS
for rank_name in rank_names])
def SixtySixDeck():
rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
for suit_name in SUIT_KEYS
for rank_name in rank_names])
-# class Card:
-# def __init__(self, rank, suit):
-# self.__rank, self.__suit = rank, suit
-# @property
-# def rank(self):
-# return self.__rank
-# @property
-# def suit(self):
-# return self.__suit
-# Card = collections.namedtuple('Card',['rank', 'suit'])
class Card(tuple):
def __new__(cls, rank, suit):
return tuple.__new__(cls, (rank(), suit()))
@property
def rank(self):
return self[0]
@property
def suit(self):
return self[1]
def __str__(self):
return type(self[0]).__name__ + ' of ' + type(self[1]).__name__
def __setattr__(self, *ignored):
raise AttributeError
def __delattr__(self, *ignored):
raise AttributeError