Решение на Тесте карти от Мария Донева

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

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

Резултати

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

Код

class Rank:
def __init__(self, symbol, order=0):
self.symbol = symbol
self.order = order
@classmethod
def __str__(cls):
return str(cls.__name__)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.symbol == other.symbol
else:
return False
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A', 1)
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2', 2)
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3', 3)
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4', 4)
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5', 5)
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6', 6)
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7', 7)
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8', 8)
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9', 9)
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10', 10)
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J', 11)
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q', 12)
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K', 13)
RANKS = {subclass().__class__.__name__: subclass for subclass
in Rank.__subclasses__()}
class Suit:
def __init__(self, color, order=0):
self.color = color
self.order = order
@classmethod
def __str__(cls):
return str(cls.__name__)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.color == other.color
else:
return False
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red', 3)
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black', 2)
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red', 1)
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black', 4)
SUITS = {subclass().__class__.__name__: subclass for subclass
in Suit.__subclasses__()}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return str(self.rank.__class__.__name__ + ' of '
+ self.suit.__class__.__name__)
def __repr__(self):
return ("<Card {0} of {1}>".format(self.rank.__class__.__name__,
self.suit.__class__.__name__))
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self.rank == other.rank and
self.suit == other.suit)
else:
return False
class CardCollection:
def __init__(self, collection=None):
if collection is None:
self.collection = []
else:
self.collection = collection
def __len__(self):
return len(self.collection)
def __getitem__(self, i):
return (self.collection)[i]
def add(self, card):
(self.collection).append(card)
def draw(self, index):
card = (self.collection)[index]
(self.collection).remove(card)
return card
def draw_from_bottom(self):
card = (self.collection)[0]
(self.collection).remove(card)
return card
def draw_from_top(self):
last_position = len(self.collection) - 1
card = (self.collection)[last_position]
(self.collection).remove(card)
return card
def bottom_card(self):
return (self.collection)[0]
def top_card(self):
last_position = len(self.collection) - 1
return (self.collection)[last_position]
def index(self, card):
index = -1
card_rank = card.rank.__class__
card_suit = card.suit.__class__
for item in range(len(self.collection)):
current_card_rank = (self.collection)[item].rank.__class__
current_card_suit = (self.collection)[item].suit.__class__
index += 1
if (current_card_rank == card_rank and
current_card_suit == card_suit):
return index
raise ValueError("<Card {0}> is not in list".
format(card))
def get_sorted_ranks():
ranks = [subclass for subclass in Rank.__subclasses__()]
ranks.sort(key=lambda x: -(x().order))
ranks = [rank().__class__.__name__ for rank in ranks]
return ranks
def get_sorted_suits():
suits = [subclass for subclass in Suit.__subclasses__()]
suits.sort(key=lambda x: x().order)
suits = [suit().__class__.__name__ for suit in suits]
return suits
def StandardDeck():
standard_deck = CardCollection()
ranks = get_sorted_ranks()
suits = get_sorted_suits()
for suit in suits:
for rank in ranks:
card = Card(RANKS[rank], SUITS[suit])
standard_deck.add(card)
return list(standard_deck)
def BeloteDeck():
belote_deck = CardCollection()
ranks = get_sorted_ranks()
suits = get_sorted_suits()
end = 7
for suit in suits:
for i in range(end):
card = Card(RANKS[ranks[i]], SUITS[suit])
belote_deck.add(card)
return list(belote_deck)
def SixtySixDeck():
sixty_six_deck = CardCollection()
ranks = get_sorted_ranks()
suits = get_sorted_suits()
end = 5
for suit in suits:
for i in range(end):
card = Card(RANKS[ranks[i]], SUITS[suit])
sixty_six_deck.add(card)
return list(sixty_six_deck)

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

F.....F.........
======================================================================
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-13fyq3z/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 7:
Ace of Diamonds
King of Clubs

First list contains 4 additional elements.
First extra element 28:
Nine of Spades

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

======================================================================
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-13fyq3z/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 5:
Ace of Diamonds
King of Clubs

First list contains 4 additional elements.
First extra element 20:
Jack of Spades

  ['King of Diamonds',
   'Queen of Diamonds',
   'Jack of Diamonds',
   'Ten of Diamonds',
   'Nine of Diamonds',
-  'Ace of Diamonds',
   'King of Clubs',
   'Queen of Clubs',
   'Jack of Clubs',
   'Ten of Clubs',
   'Nine of Clubs',
-  'Ace of Clubs',
   'King of Hearts',
   'Queen of Hearts',
   'Jack of Hearts',
   'Ten of Hearts',
   'Nine of Hearts',
-  'Ace of Hearts',
   'King of Spades',
   'Queen of Spades',
   'Jack of Spades',
   'Ten of Spades',
-  'Nine of Spades',
?                  ^

+  'Nine of Spades']
?                  ^

-  'Ace of Spades']

----------------------------------------------------------------------
Ran 16 tests in 0.031s

FAILED (failures=2)

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

Мария обнови решението на 23.03.2014 21:24 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol, order=0):
+ self.symbol = symbol
+ self.order = order
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return self.symbol == other.symbol
+ else:
+ return False
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A', 1)
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2', 2)
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3', 3)
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4', 4)
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5', 5)
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6', 6)
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7', 7)
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8', 8)
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9', 9)
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10', 10)
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J', 11)
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q', 12)
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K', 13)
+
+
+RANKS = {subclass().__class__.__name__: subclass for subclass
+ in Rank.__subclasses__()}
+
+
+class Suit:
+ def __init__(self, color, order=0):
+ self.color = color
+ self.order = order
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return self.color == other.color
+ else:
+ return False
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red', 3)
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black', 2)
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red', 1)
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black', 4)
+
+
+SUITS = {subclass().__class__.__name__: subclass for subclass
+ in Suit.__subclasses__()}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return str(self.rank.__class__.__name__ + ' of '
+ + self.suit.__class__.__name__)
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return (self.rank == other.rank and
+ self.suit == other.suit)
+ else:
+ return False
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self, i):
+ return (self.collection)[i]
+
+ def add(self, card):
+ (self.collection).append(card)
+
+ def draw(self, index):
+ card = (self.collection)[index]
+ (self.collection).remove(card)
+ return str(card)
+
+ def draw_from_bottom(self):
+ card = (self.collection)[0]
+ (self.collection).remove(card)
+ return str(card)
+
+ def draw_from_top(self):
+ last_position = len(self.collection) - 1
+ card = (self.collection)[last_position]
+ (self.collection).remove(card)
+ return str(card)
+
+ def bottom_card(self):
+ return str((self.collection)[0])
+
+ def top_card(self):
+ last_position = len(self.collection) - 1
+ return str((self.collection)[last_position])
+
+ def index(self, card):
+ index = -1
+ card_rank = card.rank.__class__
+ card_suit = card.suit.__class__
+ for item in range(len(self.collection)):
+ current_card_rank = (self.collection)[item].rank.__class__
+ current_card_suit = (self.collection)[item].suit.__class__
+ index += 1
+ if (current_card_rank == card_rank and
+ current_card_suit == card_suit):
+ return index
+ raise ValueError("<Card {0}> is not in list".
+ format(str(card)))
+
+
+def get_sorted_ranks():
+ ranks = [subclass for subclass in Rank.__subclasses__()]
+ ranks.sort(key=lambda x: -(x().order))
+ ranks = [rank().__class__.__name__ for rank in ranks]
+ return ranks
+
+
+def get_sorted_suits():
+ suits = [subclass for subclass in Suit.__subclasses__()]
+ suits.sort(key=lambda x: x().order)
+ suits = [suit().__class__.__name__ for suit in suits]
+ return suits
+
+
+def StandardDeck():
+ standard_deck = []
+ ranks = get_sorted_ranks()
+ suits = get_sorted_suits()
+ for suit in suits:
+ for rank in ranks:
+ standard_deck.append('Card {0} of {1}'.format(rank, suit))
+ return standard_deck
+
+
+def BeloteDeck():
+ belote_deck = []
+ ranks = get_sorted_ranks()
+ suits = get_sorted_suits()
+ end = 8
+ for suit in suits:
+ for i in range(end):
+ belote_deck.append('Card {0} of {1}'.format(ranks[i], suit))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = []
+ ranks = get_sorted_ranks()
+ suits = get_sorted_suits()
+ end = 6
+ for suit in suits:
+ for i in range(end):
+ sixty_six_deck.append('Card {0} of {1}'.format(ranks[i], suit))
+ return sixty_six_deck

Прочети пак условието. От StandardDeck и приятелите му се изисква да връщаш инстанция на CardCollection, който съдържа съответните инстанции на класа Card.

В момента връщаш масив от низове. Причината, поради която изглежда така в примера ми е, че съм предефинирал метода __repr__.

Мария обнови решението на 24.03.2014 22:48 (преди над 10 години)

class Rank:
def __init__(self, symbol, order=0):
self.symbol = symbol
self.order = order
@classmethod
def __str__(cls):
return str(cls.__name__)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.symbol == other.symbol
else:
return False
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A', 1)
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2', 2)
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3', 3)
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4', 4)
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5', 5)
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6', 6)
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7', 7)
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8', 8)
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9', 9)
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10', 10)
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J', 11)
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q', 12)
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K', 13)
RANKS = {subclass().__class__.__name__: subclass for subclass
in Rank.__subclasses__()}
class Suit:
def __init__(self, color, order=0):
self.color = color
self.order = order
@classmethod
def __str__(cls):
return str(cls.__name__)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.color == other.color
else:
return False
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red', 3)
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black', 2)
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red', 1)
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black', 4)
SUITS = {subclass().__class__.__name__: subclass for subclass
in Suit.__subclasses__()}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return str(self.rank.__class__.__name__ + ' of '
+ self.suit.__class__.__name__)
+ def __repr__(self):
+ return ("<Card {0} of {1}>".format(self.rank.__class__.__name__,
+ self.suit.__class__.__name__))
+
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self.rank == other.rank and
self.suit == other.suit)
else:
return False
class CardCollection:
- def __init__(self, collection=[]):
- self.collection = collection
+ def __init__(self, collection=None):
+ if collection is None:
+ self.collection = []
+ else:
+ self.collection = collection
def __len__(self):
return len(self.collection)
def __getitem__(self, i):
return (self.collection)[i]
def add(self, card):
(self.collection).append(card)
def draw(self, index):
card = (self.collection)[index]
(self.collection).remove(card)
- return str(card)
+ return card
def draw_from_bottom(self):
card = (self.collection)[0]
(self.collection).remove(card)
- return str(card)
+ return card
def draw_from_top(self):
last_position = len(self.collection) - 1
card = (self.collection)[last_position]
(self.collection).remove(card)
- return str(card)
+ return card
def bottom_card(self):
- return str((self.collection)[0])
+ return (self.collection)[0]
def top_card(self):
last_position = len(self.collection) - 1
- return str((self.collection)[last_position])
+ return (self.collection)[last_position]
def index(self, card):
index = -1
card_rank = card.rank.__class__
card_suit = card.suit.__class__
for item in range(len(self.collection)):
current_card_rank = (self.collection)[item].rank.__class__
current_card_suit = (self.collection)[item].suit.__class__
index += 1
if (current_card_rank == card_rank and
current_card_suit == card_suit):
return index
raise ValueError("<Card {0}> is not in list".
- format(str(card)))
+ format(card))
def get_sorted_ranks():
ranks = [subclass for subclass in Rank.__subclasses__()]
ranks.sort(key=lambda x: -(x().order))
ranks = [rank().__class__.__name__ for rank in ranks]
return ranks
def get_sorted_suits():
suits = [subclass for subclass in Suit.__subclasses__()]
suits.sort(key=lambda x: x().order)
suits = [suit().__class__.__name__ for suit in suits]
return suits
def StandardDeck():
- standard_deck = []
+ standard_deck = CardCollection()
ranks = get_sorted_ranks()
suits = get_sorted_suits()
for suit in suits:
for rank in ranks:
- standard_deck.append('Card {0} of {1}'.format(rank, suit))
- return standard_deck
+ card = Card(RANKS[rank], SUITS[suit])
+ standard_deck.add(card)
+ return list(standard_deck)
def BeloteDeck():
- belote_deck = []
+ belote_deck = CardCollection()
ranks = get_sorted_ranks()
suits = get_sorted_suits()
- end = 8
+ end = 7
for suit in suits:
for i in range(end):
- belote_deck.append('Card {0} of {1}'.format(ranks[i], suit))
- return belote_deck
+ card = Card(RANKS[ranks[i]], SUITS[suit])
+ belote_deck.add(card)
+ return list(belote_deck)
def SixtySixDeck():
- sixty_six_deck = []
+ sixty_six_deck = CardCollection()
ranks = get_sorted_ranks()
suits = get_sorted_suits()
- end = 6
+ end = 5
for suit in suits:
for i in range(end):
- sixty_six_deck.append('Card {0} of {1}'.format(ranks[i], suit))
- return sixty_six_deck
+ card = Card(RANKS[ranks[i]], SUITS[suit])
+ sixty_six_deck.add(card)
+ return list(sixty_six_deck)