Решение на Тесте карти от Стоян Ефтимов

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

Към профила на Стоян Ефтимов

Резултати

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

Код

class Rank:
def __init__(self, symbol=''):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
for rank_name, rank_class in RANKS.items():
if rank_class == self.__class__:
return rank_name
class Suit:
def __init__(self, color=''):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
for suit_name, suit_class in SUITS.items():
if suit_class == self.__class__:
return suit_name
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __str__(self):
return str(self.__rank) + ' of ' + str(self.__suit)
def __eq__(self, other):
return str(self) == str(other)
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
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):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __iter__(self):
for card in self.collection:
yield card
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
class Ace(Rank):
symbol = 'A'
class Two(Rank):
symbol = '2'
class Three(Rank):
symbol = '3'
class Four(Rank):
symbol = '4'
class Five(Rank):
symbol = '5'
class Six(Rank):
symbol = '6'
class Seven(Rank):
symbol = '7'
class Eight(Rank):
symbol = '8'
class Nine(Rank):
symbol = '9'
class Ten(Rank):
symbol = '10'
class Jack(Rank):
symbol = 'J'
class Queen(Rank):
symbol = 'Q'
class King(Rank):
symbol = 'K'
class Hearts(Suit):
color = 'red'
class Clubs(Suit):
color = 'black'
class Diamonds(Suit):
color = 'red'
class Spades(Suit):
color = 'black'
RANKS = {}
for cls in Rank.__subclasses__():
RANKS[cls.__name__] = cls
SUITS = {}
for cls in Suit.__subclasses__():
SUITS[cls.__name__] = cls
def StandardDeck():
standard_deck = CardCollection([])
for suit_name in SUITS:
for rank_name in RANKS:
standard_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
return standard_deck
def BeloteDeck():
belote_deck = CardCollection([])
for suit_name in SUITS:
for rank_name in list(RANKS.keys())[:7]:
belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
belote_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
return belote_deck
def SixtySixDeck():
sixty_six_deck = CardCollection([])
for suit_name in SUITS:
for rank_name in list(RANKS.keys())[:5]:
sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
sixty_six_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
return sixty_six_deck

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

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

First differing element 0:
King of Diamonds
Three of Spades

Diff is 1331 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-vima1i/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['Three of Spades', 'Ace of Sp...

First differing element 0:
King of Diamonds
Three of Spades

Diff is 1030 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-vima1i/test.py", line 171, in test_standard_deck
    self.assertEqual(STANDARD_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['Three of Spades', 'Ace of Sp...

First differing element 0:
King of Diamonds
Three of Spades

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

----------------------------------------------------------------------
Ran 16 tests in 0.068s

FAILED (failures=3)

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

Стоян обнови решението на 21.03.2014 17:50 (преди над 10 години)

+class Rank:
+
+ def __init__(self, symbol=''):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ for rank_name, rank_class in RANKS.items():
+ if rank_class == self.__class__:
+ return rank_name
+
+
+class Suit:
+
+ def __init__(self, color=''):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ for suit_name, suit_class in SUITS.items():
+ if suit_class == self.__class__:
+ return suit_name
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+ def __str__(self):
+ return str(self.__rank) + ' of ' + str(self.__suit)
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.collection = list(collection)
+
+ def draw(self, index):
+ drawed_card = self.collection[index]
+ del self.collection[index]
+ return drawed_card
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ bottom_card = self.collection[0]
+ del self.collection[0]
+ return bottom_card
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+ def __iter__(self):
+ for card in self.collection:
+ yield card
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def __len__(self):
+ return len(self.collection)
+
+
+class Ace(Rank):
+ symbol = 'A'
+
+
+class Two(Rank):
+ symbol = '2'
+
+
+class Three(Rank):
+ symbol = '3'
+
+
+class Four(Rank):
+ symbol = '4'
+
+
+class Five(Rank):
+ symbol = '5'
+
+
+class Six(Rank):
+ symbol = '6'
+
+
+class Seven(Rank):
+ symbol = '7'
+
+
+class Eight(Rank):
+ symbol = '8'
+
+
+class Nine(Rank):
+ symbol = '9'
+
+
+class Ten(Rank):
+ symbol = '10'
+
+
+class Jack(Rank):
+ symbol = 'J'
+
+
+class Queen(Rank):
+ symbol = 'Q'
+
+
+class King(Rank):
+ symbol = 'K'
+
+
+class Hearts(Suit):
+ color = 'red'
+
+
+class Clubs(Suit):
+ color = 'black'
+
+
+class Diamonds(Suit):
+ color = 'red'
+
+
+class Spades(Suit):
+ color = 'black'
+
+ranks_name = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
+ranks_class = (King, Queen, Jack, Ten, Nine, Eight,
+ Seven, Six, Five, Four, Three, Two, Ace)
+suits_name = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
+suits_class = (Diamonds, Clubs, Hearts, Spades)
+
+RANKS = {rank: rank_class for rank, rank_class in zip(ranks_name, ranks_class)}
+SUITS = {suit: suit_class for suit, suit_class in zip(suits_name, suits_class)}
+
+
+def StandartDeck():
+ standart_deck = CardCollection([])
+ for suit_name in suits_name:
+ for rank_name in ranks_name:
+ standart_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return standart_deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection([])
+ for suit_name in suits_name:
+ for rank_name in ranks_name[:7]:
+ belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ belote_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection([])
+ for suit_name in suits_name:
+ for rank_name in ranks_name[:5]:
+ sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ sixty_six_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
+ return sixty_six_deck

Стоян обнови решението на 23.03.2014 14:46 (преди над 10 години)

class Rank:
def __init__(self, symbol=''):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
for rank_name, rank_class in RANKS.items():
if rank_class == self.__class__:
return rank_name
class Suit:
def __init__(self, color=''):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
for suit_name, suit_class in SUITS.items():
if suit_class == self.__class__:
return suit_name
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __str__(self):
return str(self.__rank) + ' of ' + str(self.__suit)
def __eq__(self, other):
return str(self) == str(other)
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def draw(self, index):
drawed_card = self.collection[index]
del self.collection[index]
return drawed_card
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
bottom_card = self.collection[0]
del self.collection[0]
return bottom_card
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __iter__(self):
for card in self.collection:
yield card
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
class Ace(Rank):
symbol = 'A'
class Two(Rank):
symbol = '2'
class Three(Rank):
symbol = '3'
class Four(Rank):
symbol = '4'
class Five(Rank):
symbol = '5'
class Six(Rank):
symbol = '6'
class Seven(Rank):
symbol = '7'
class Eight(Rank):
symbol = '8'
class Nine(Rank):
symbol = '9'
class Ten(Rank):
symbol = '10'
class Jack(Rank):
symbol = 'J'
class Queen(Rank):
symbol = 'Q'
class King(Rank):
symbol = 'K'
class Hearts(Suit):
color = 'red'
class Clubs(Suit):
color = 'black'
class Diamonds(Suit):
color = 'red'
class Spades(Suit):
color = 'black'
ranks_name = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
ranks_class = (King, Queen, Jack, Ten, Nine, Eight,
Seven, Six, Five, Four, Three, Two, Ace)
suits_name = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
suits_class = (Diamonds, Clubs, Hearts, Spades)
RANKS = {rank: rank_class for rank, rank_class in zip(ranks_name, ranks_class)}
SUITS = {suit: suit_class for suit, suit_class in zip(suits_name, suits_class)}
-def StandartDeck():
- standart_deck = CardCollection([])
+def StandardDeck():
+ standard_deck = CardCollection([])
for suit_name in suits_name:
for rank_name in ranks_name:
- standart_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
- return standart_deck
+ standard_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return standard_deck
def BeloteDeck():
belote_deck = CardCollection([])
for suit_name in suits_name:
for rank_name in ranks_name[:7]:
belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
belote_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
return belote_deck
def SixtySixDeck():
sixty_six_deck = CardCollection([])
for suit_name in suits_name:
for rank_name in ranks_name[:5]:
sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
sixty_six_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
return sixty_six_deck
  • Наистина ли имаш нужда от това да пазиш имената и класовете на всички видове и бои? Освен това не са константи, а би трябвало
  • И draw_from_bottom можеш да го направиш с list.pop(). Прочети документацията за него и ще се сетиш :)

Стоян обнови решението на 25.03.2014 01:05 (преди над 10 години)

class Rank:
def __init__(self, symbol=''):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
for rank_name, rank_class in RANKS.items():
if rank_class == self.__class__:
return rank_name
class Suit:
def __init__(self, color=''):
self.color = color
def __eq__(self, other):
return self.__class__ == other.__class__
def __str__(self):
for suit_name, suit_class in SUITS.items():
if suit_class == self.__class__:
return suit_name
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __str__(self):
return str(self.__rank) + ' of ' + str(self.__suit)
def __eq__(self, other):
return str(self) == str(other)
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def draw(self, index):
- drawed_card = self.collection[index]
- del self.collection[index]
- return drawed_card
+ return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
- bottom_card = self.collection[0]
- del self.collection[0]
- return bottom_card
+ 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):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __iter__(self):
for card in self.collection:
yield card
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
class Ace(Rank):
symbol = 'A'
class Two(Rank):
symbol = '2'
class Three(Rank):
symbol = '3'
class Four(Rank):
symbol = '4'
class Five(Rank):
symbol = '5'
class Six(Rank):
symbol = '6'
class Seven(Rank):
symbol = '7'
class Eight(Rank):
symbol = '8'
class Nine(Rank):
symbol = '9'
class Ten(Rank):
symbol = '10'
class Jack(Rank):
symbol = 'J'
class Queen(Rank):
symbol = 'Q'
class King(Rank):
symbol = 'K'
class Hearts(Suit):
color = 'red'
class Clubs(Suit):
color = 'black'
class Diamonds(Suit):
color = 'red'
class Spades(Suit):
color = 'black'
-ranks_name = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
-ranks_class = (King, Queen, Jack, Ten, Nine, Eight,
- Seven, Six, Five, Four, Three, Two, Ace)
-suits_name = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
-suits_class = (Diamonds, Clubs, Hearts, Spades)
-RANKS = {rank: rank_class for rank, rank_class in zip(ranks_name, ranks_class)}
-SUITS = {suit: suit_class for suit, suit_class in zip(suits_name, suits_class)}
+RANKS = {}
+for cls in Rank.__subclasses__():
+ RANKS[cls.__name__] = cls
+SUITS = {}
+for cls in Suit.__subclasses__():
+ SUITS[cls.__name__] = cls
def StandardDeck():
standard_deck = CardCollection([])
- for suit_name in suits_name:
- for rank_name in ranks_name:
+ for suit_name in SUITS:
+ for rank_name in RANKS:
standard_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
return standard_deck
def BeloteDeck():
belote_deck = CardCollection([])
- for suit_name in suits_name:
- for rank_name in ranks_name[:7]:
+ for suit_name in SUITS:
+ for rank_name in list(RANKS.keys())[:7]:
belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
belote_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
return belote_deck
def SixtySixDeck():
sixty_six_deck = CardCollection([])
- for suit_name in suits_name:
- for rank_name in ranks_name[:5]:
+ for suit_name in SUITS:
+ for rank_name in list(RANKS.keys())[:5]:
sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
sixty_six_deck.add(Card(RANKS["Ace"], SUITS[suit_name]))
return sixty_six_deck