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

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

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

Резултати

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

Код

class Operator:
def __eq__(self, second):
return self.symbol == second.symbol
def __str__(self):
return self.__class__.__name__
class Rank(Operator):
symbol = None
class Suit(Operator):
symbol = None
SCHNAPSEN_DECK = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
BELOTE_DECK = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven']
SYMBOLS = {'King': 'K', 'Queen': 'Q', 'Jack': 'J', 'Ten': '10', 'Nine': '9',
'Eight': '8', 'Seven': '7', 'Six': '6', 'Five': '5', 'Four': '4',
'Three': '3', 'Two': '2', 'Ace': 'A'}
COLORS = {'Diamonds': 'red', 'Hearts':
'red', 'Spades': 'black', 'Clubs': 'black'}
RANKS = {name: type(name, (Rank,), {'symbol': symbol}) for name, symbol in SYMBOLS.items()}
SUITS = {name: type(name, (Suit,), {'color': color}) for name, color in COLORS.items()}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return "{} of {}".format(str(self.rank), str(self.suit))
def __eq__(self, second):
return self.rank == second.rank and self.suit == second.suit
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
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[-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 __setitem__(self, index, card):
self.cards[index] = card
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def __iter__(self):
for card in self.cards:
yield card
def __str__(self):
card_str = []
for card in self.cards:
card_str.append(str(card))
return', '.join(card_str)
def full_deck(ranks):
deck = CardCollection([])
for suit in COLORS.keys():
for rank in ranks:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
def SixtySixDeck():
return full_deck(SCHNAPSEN_DECK)
def StandardDeck():
return full_deck(SYMBOLS.keys())
def BeloteDeck():
return full_deck(BELOTE_DECK)

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

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

First differing element 0:
King of Diamonds
Ace of Diamonds

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

First differing element 0:
King of Diamonds
Ace of Diamonds

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

======================================================================
FAIL: test_card_equals (test.CardTest)
----------------------------------------------------------------------
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-zset5f/test.py", line 74, in test_card_equals
    self.assertNotEqual(aos1, solution.Card(solution.RANKS["Ace"], solution.SUITS["Hearts"]))
AssertionError: <solution.Card object at 0xb782e6ac> == <solution.Card object at 0xb782e8cc>

----------------------------------------------------------------------
Ran 16 tests in 0.064s

FAILED (failures=4)

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

Диана обнови решението на 24.03.2014 22:18 (преди над 10 години)

+class Rank:
+ symbol = None
+
+
+class Suit:
+ symbol = None
+
+
+class Operator:
+ def __eq__(self, second):
+ return self.symbol == second.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Two(Rank, Operator):
+ symbol = '2'
+
+
+class Three(Rank, Operator):
+ symbol = '3'
+
+
+class Four(Rank, Operator):
+ symbol = '4'
+
+
+class Five(Rank, Operator):
+ symbol = '5'
+
+
+class Six(Rank, Operator):
+ symbol = '6'
+
+
+class Seven(Rank, Operator):
+ symbol = '7'
+
+
+class Eight(Rank, Operator):
+ symbol = '8'
+
+
+class Nine(Rank, Operator):
+ symbol = '9'
+
+
+class Ten(Rank, Operator):
+ symbol = '10'
+
+
+class Jack(Rank, Operator):
+ symbol = 'J'
+
+
+class Queen(Rank, Operator):
+ symbol = 'Q'
+
+
+class King(Rank, Operator):
+ symbol = 'K'
+
+
+class Ace(Rank, Operator):
+ symbol = 'A'
+
+
+class Diamonds(Suit, Operator):
+ color = 'red'
+
+
+class Hearts(Suit, Operator):
+ color = 'red'
+
+
+class Spades(Suit, Operator):
+ color = 'black'
+
+
+class Clubs(Suit, Operator):
+ color = 'black'
+
+SCHNAPSEN_DECK = [Ace, King, Queen, Jack, Ten, Nine]
+BELOTE_DECK = [King, Queen, Jack, Ten, Nine, Eight, Seven]
+STANDARD_DECK = [King, Queen, Jack, Ten, Nine, Eight,
+ Seven, Six, Five, Four, Three, Two, Ace]
+ALL_SUITS = [Diamonds, Hearts, Spades, Clubs]
+
+RANKS = {rank.__name__: rank for rank in STANDARD_DECK}
+SUITS = {suit.__name__: suit for suit in ALL_SUITS}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return "{} of {}".format(str(self.rank), str(self.suit))
+
+ def __eq__(self, second):
+ return self.rank == second.rank and self.suit == second.suit
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.cards = list(collection)
+
+ 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[-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 __setitem__(self, index, card):
+ self.cards[index] = card
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def __len__(self):
+ return len(self.cards)
+
+ def __iter__(self):
+ for card in self.cards:
+ yield card
+
+ def __str__(self):
+ card_str = []
+ for card in self.cards:
+ card_str.append(str(card))
+ return', '.join(card_str)
+
+
+def full_deck(ranks):
+ deck = CardCollection([])
+ for suit in ALL_SUITS:
+ for rank in ranks:
+ deck.add(Card(rank, suit))
+ return deck

Идеята да използваш mixin Operator не е много подходяща в случая. По-скоро изнеси дъндър методите в базовия клас Rank. Помисли си за начин, по който можеш динамично да конструираш подкласовете. Hint: това става с използването на type.

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

-class Rank:
- symbol = None
-
-class Suit:
- symbol = None
-
-
class Operator:
def __eq__(self, second):
return self.symbol == second.symbol
def __str__(self):
return self.__class__.__name__
-class Two(Rank, Operator):
- symbol = '2'
+class Rank(Operator):
+ symbol = None
-class Three(Rank, Operator):
- symbol = '3'
+class Suit(Operator):
+ symbol = None
-class Four(Rank, Operator):
- symbol = '4'
+SCHNAPSEN_DECK = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
+BELOTE_DECK = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven']
+SYMBOLS = {'King': 'K', 'Queen': 'Q', 'Jack': 'J', 'Ten': '10', 'Nine': '9',
+ 'Eight': '8', 'Seven': '7', 'Six': '6', 'Five': '5', 'Four': '4',
+ 'Three': '3', 'Two': '2', 'Ace': 'A'}
+COLORS = {'Diamonds': 'red', 'Hearts':
+ 'red', 'Spades': 'black', 'Clubs': 'black'}
-class Five(Rank, Operator):
- symbol = '5'
+RANKS = {name: type(name, (Rank,), {'symbol': symbol}) for name, symbol in SYMBOLS.items()}
+SUITS = {name: type(name, (Suit,), {'color': color}) for name, color in COLORS.items()}
-class Six(Rank, Operator):
- symbol = '6'
-
-
-class Seven(Rank, Operator):
- symbol = '7'
-
-
-class Eight(Rank, Operator):
- symbol = '8'
-
-
-class Nine(Rank, Operator):
- symbol = '9'
-
-
-class Ten(Rank, Operator):
- symbol = '10'
-
-
-class Jack(Rank, Operator):
- symbol = 'J'
-
-
-class Queen(Rank, Operator):
- symbol = 'Q'
-
-
-class King(Rank, Operator):
- symbol = 'K'
-
-
-class Ace(Rank, Operator):
- symbol = 'A'
-
-
-class Diamonds(Suit, Operator):
- color = 'red'
-
-
-class Hearts(Suit, Operator):
- color = 'red'
-
-
-class Spades(Suit, Operator):
- color = 'black'
-
-
-class Clubs(Suit, Operator):
- color = 'black'
-
-SCHNAPSEN_DECK = [Ace, King, Queen, Jack, Ten, Nine]
-BELOTE_DECK = [King, Queen, Jack, Ten, Nine, Eight, Seven]
-STANDARD_DECK = [King, Queen, Jack, Ten, Nine, Eight,
- Seven, Six, Five, Four, Three, Two, Ace]
-ALL_SUITS = [Diamonds, Hearts, Spades, Clubs]
-
-RANKS = {rank.__name__: rank for rank in STANDARD_DECK}
-SUITS = {suit.__name__: suit for suit in ALL_SUITS}
-
-
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return "{} of {}".format(str(self.rank), str(self.suit))
def __eq__(self, second):
return self.rank == second.rank and self.suit == second.suit
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
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[-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 __setitem__(self, index, card):
self.cards[index] = card
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def __iter__(self):
for card in self.cards:
yield card
def __str__(self):
card_str = []
for card in self.cards:
card_str.append(str(card))
return', '.join(card_str)
-
def full_deck(ranks):
deck = CardCollection([])
- for suit in ALL_SUITS:
+ for suit in COLORS.keys():
for rank in ranks:
- deck.add(Card(rank, suit))
+ deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
+
+
+def SixtySixDeck():
+ return full_deck(SCHNAPSEN_DECK)
+
+
+def StandardDeck():
+ return full_deck(SYMBOLS.keys())
+
+
+def BeloteDeck():
+ return full_deck(BELOTE_DECK)