Решение на Тесте карти от Виктор Иванов

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

Към профила на Виктор Иванов

Резултати

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

Код

class Rank:
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.name
class Suit:
def __eq__(self, other):
return self.name == other.name
def __str__(self):
return self.name
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
self.name = 'Ace'
class Two(Rank):
def __init__(self):
self.symbol = '2'
self.name = 'Two'
class Three(Rank):
def __init__(self):
self.symbol = '3'
self.name = 'Three'
class Four(Rank):
def __init__(self):
self.symbol = '4'
self.name = 'Four'
class Five(Rank):
def __init__(self):
self.symbol = '5'
self.name = 'Five'
class Six(Rank):
def __init__(self):
self.symbol = '6'
self.name = 'Six'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
self.name = 'Seven'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
self.name = 'Eight'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
self.name = 'Nine'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
self.name = 'Ten'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
self.name = 'Jack'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
self.name = 'Queen'
class King(Rank):
def __init__(self):
self.symbol = 'K'
self.name = 'King'
RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
'Jack': Jack, 'Queen': Queen, 'King': King}
class Clubs(Suit):
def __init__(self):
self.color = 'black'
self.name = 'Clubs'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
self.name = 'Diamonds'
class Hearts(Suit):
def __init__(self):
self.color = 'red'
self.name = 'Hearts'
class Spades(Suit):
def __init__(self):
self.color = 'black'
self.name = 'Spades'
SUITS = {'Clubs': Clubs, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Spades': Spades}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __setattr__(self, name, value):
if hasattr(self, name):
raise AttributeError('can\'t set attribute')
else:
self.__dict__[name] = value
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, index):
return self.collection[index]
def __repr__(self):
return repr(self.collection)
def __len__(self):
return len(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(-1)
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)
SUITS_ORDERED = [Diamonds, Clubs, Hearts, Spades]
RANKS_STANDARD_ORDERED = [King, Queen, Jack, Ten, Nine, Eight, Seven,
Six, Five, Four, Three, Two, Ace]
RANKS_BELOTE_ORDERED = [King, Queen, Jack, Ten, Nine, Eight, Seven, Ace]
RANKS_SIXTY_SIX_ORDERED = [King, Queen, Jack, Ten, Nine, Ace]
def StandardDeck():
result = CardCollection([])
for suit in SUITS_ORDERED:
for rank in RANKS_STANDARD_ORDERED:
result.add(Card(rank, suit))
return result
def BeloteDeck():
result = CardCollection([])
for suit in SUITS_ORDERED:
for rank in RANKS_BELOTE_ORDERED:
result.add(Card(rank, suit))
return result
def SixtySixDeck():
result = CardCollection([])
for suit in SUITS_ORDERED:
for rank in RANKS_SIXTY_SIX_ORDERED:
result.add(Card(rank, suit))
return result

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

...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-6ail9z/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-6ail9z/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <Card Three of Hearts> != <Card Eight of Spades>

----------------------------------------------------------------------
Ran 16 tests in 0.040s

FAILED (failures=2)

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

Виктор обнови решението на 22.03.2014 20:54 (преди над 10 години)

+class Rank:
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.name
+
+
+class Suit:
+ def __eq__(self, other):
+ return self.name == other.name
+
+ def __str__(self):
+ return self.name
+
+
+class Ace(Rank):
+ def __init__(self):
+ self.symbol = 'A'
+ self.name = 'Ace'
+
+
+class Two(Rank):
+ def __init__(self):
+ self.symbol = '2'
+ self.name = 'Two'
+
+
+class Three(Rank):
+ def __init__(self):
+ self.symbol = '3'
+ self.name = 'Three'
+
+
+class Four(Rank):
+ def __init__(self):
+ self.symbol = '4'
+ self.name = 'Four'
+
+
+class Five(Rank):
+ def __init__(self):
+ self.symbol = '5'
+ self.name = 'Five'
+
+
+class Six(Rank):
+ def __init__(self):
+ self.symbol = '6'
+ self.name = 'Six'
+
+
+class Seven(Rank):
+ def __init__(self):
+ self.symbol = '7'
+ self.name = 'Seven'
+
+
+class Eight(Rank):
+ def __init__(self):
+ self.symbol = '8'
+ self.name = 'Eight'
+
+
+class Nine(Rank):
+ def __init__(self):
+ self.symbol = '9'
+ self.name = 'Nine'
+
+
+class Ten(Rank):
+ def __init__(self):
+ self.symbol = '10'
+ self.name = 'Ten'
+
+
+class Jack(Rank):
+ def __init__(self):
+ self.symbol = 'J'
+ self.name = 'Jack'
+
+
+class Queen(Rank):
+ def __init__(self):
+ self.symbol = 'Q'
+ self.name = 'Queen'
+
+
+class King(Rank):
+ def __init__(self):
+ self.symbol = 'K'
+ self.name = 'King'
+
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
+ 'Jack': Jack, 'Queen': Queen, 'King': King}
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'black'
+ self.name = 'Clubs'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'red'
+ self.name = 'Diamonds'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+ self.name = 'Hearts'
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+ self.name = 'Spades'
+
+SUITS = {'Clubs': Clubs, 'Diamonds': Diamonds,
+ 'Hearts': Hearts, 'Spades': Spades}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank
+ self.suit = suit
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank()) + ' of ' + str(self.suit())
+
+ def __repr__(self):
+ return '<Card ' + str(self.rank()) + ' of ' + str(self.suit()) + '>'
+
+ def __setattr__(self, name, value):
+ if hasattr(self, name):
+ raise AttributeError('can\'t set attribute')
+ else:
+ self.__dict__[name] = value
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __repr__(self):
+ return repr(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop(-1)
+
+ 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)
+
+suits_ordered = [Diamonds, Clubs, Hearts, Spades]
+ranks_standard_ordered = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace]
+ranks_belote_ordered = [King, Queen, Jack, Ten, Nine, Eight, Seven, Ace]
+ranks_sixty_six_ordered = [King, Queen, Jack, Ten, Nine, Ace]
+
+
+def StandardDeck():
+ result = CardCollection([])
+ for suit in suits_ordered:
+ for rank in ranks_standard_ordered:
+ result.add(Card(rank, suit))
+ return result
+
+
+def BeloteDeck():
+ result = CardCollection([])
+ for suit in suits_ordered:
+ for rank in ranks_belote_ordered:
+ result.add(Card(rank, suit))
+ return result
+
+
+def SixtySixDeck():
+ result = CardCollection([])
+ for suit in suits_ordered:
+ for rank in ranks_sixty_six_ordered:
+ result.add(Card(rank, suit))
+ return result

Виктор обнови решението на 23.03.2014 16:18 (преди над 10 години)

class Rank:
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.name
class Suit:
def __eq__(self, other):
return self.name == other.name
def __str__(self):
return self.name
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
self.name = 'Ace'
class Two(Rank):
def __init__(self):
self.symbol = '2'
self.name = 'Two'
class Three(Rank):
def __init__(self):
self.symbol = '3'
self.name = 'Three'
class Four(Rank):
def __init__(self):
self.symbol = '4'
self.name = 'Four'
class Five(Rank):
def __init__(self):
self.symbol = '5'
self.name = 'Five'
class Six(Rank):
def __init__(self):
self.symbol = '6'
self.name = 'Six'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
self.name = 'Seven'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
self.name = 'Eight'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
self.name = 'Nine'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
self.name = 'Ten'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
self.name = 'Jack'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
self.name = 'Queen'
class King(Rank):
def __init__(self):
self.symbol = 'K'
self.name = 'King'
RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
'Jack': Jack, 'Queen': Queen, 'King': King}
class Clubs(Suit):
def __init__(self):
self.color = 'black'
self.name = 'Clubs'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
self.name = 'Diamonds'
class Hearts(Suit):
def __init__(self):
self.color = 'red'
self.name = 'Hearts'
class Spades(Suit):
def __init__(self):
self.color = 'black'
self.name = 'Spades'
SUITS = {'Clubs': Clubs, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Spades': Spades}
class Card:
def __init__(self, rank, suit):
- self.rank = rank
- self.suit = suit
+ self.rank = rank()
+ self.suit = suit()
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
- return str(self.rank()) + ' of ' + str(self.suit())
+ return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
- return '<Card ' + str(self.rank()) + ' of ' + str(self.suit()) + '>'
+ return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __setattr__(self, name, value):
if hasattr(self, name):
raise AttributeError('can\'t set attribute')
else:
self.__dict__[name] = value
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, index):
return self.collection[index]
def __repr__(self):
return repr(self.collection)
def __len__(self):
return len(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(-1)
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)
suits_ordered = [Diamonds, Clubs, Hearts, Spades]
ranks_standard_ordered = [King, Queen, Jack, Ten, Nine, Eight, Seven,
Six, Five, Four, Three, Two, Ace]
ranks_belote_ordered = [King, Queen, Jack, Ten, Nine, Eight, Seven, Ace]
ranks_sixty_six_ordered = [King, Queen, Jack, Ten, Nine, Ace]
def StandardDeck():
result = CardCollection([])
for suit in suits_ordered:
for rank in ranks_standard_ordered:
result.add(Card(rank, suit))
return result
def BeloteDeck():
result = CardCollection([])
for suit in suits_ordered:
for rank in ranks_belote_ordered:
result.add(Card(rank, suit))
return result
def SixtySixDeck():
result = CardCollection([])
for suit in suits_ordered:
for rank in ranks_sixty_six_ordered:
result.add(Card(rank, suit))
return result

Реда на тестетата ти си е верен. Не ти е верен реда на достъпване на картите -- bottom_card() връща грешна карта -- вж си условието.

Киро има в предвид за suits_ordered и подобни че трябва да се пишат с главни букви. Това подсказва че са константи дефинирани извън функции и методи и не трябва да бъдат променяни.

Виктор обнови решението на 24.03.2014 18:37 (преди над 10 години)

class Rank:
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.name
class Suit:
def __eq__(self, other):
return self.name == other.name
def __str__(self):
return self.name
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
self.name = 'Ace'
class Two(Rank):
def __init__(self):
self.symbol = '2'
self.name = 'Two'
class Three(Rank):
def __init__(self):
self.symbol = '3'
self.name = 'Three'
class Four(Rank):
def __init__(self):
self.symbol = '4'
self.name = 'Four'
class Five(Rank):
def __init__(self):
self.symbol = '5'
self.name = 'Five'
class Six(Rank):
def __init__(self):
self.symbol = '6'
self.name = 'Six'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
self.name = 'Seven'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
self.name = 'Eight'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
self.name = 'Nine'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
self.name = 'Ten'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
self.name = 'Jack'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
self.name = 'Queen'
class King(Rank):
def __init__(self):
self.symbol = 'K'
self.name = 'King'
RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
'Jack': Jack, 'Queen': Queen, 'King': King}
class Clubs(Suit):
def __init__(self):
self.color = 'black'
self.name = 'Clubs'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
self.name = 'Diamonds'
class Hearts(Suit):
def __init__(self):
self.color = 'red'
self.name = 'Hearts'
class Spades(Suit):
def __init__(self):
self.color = 'black'
self.name = 'Spades'
SUITS = {'Clubs': Clubs, 'Diamonds': Diamonds,
'Hearts': Hearts, 'Spades': Spades}
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __setattr__(self, name, value):
if hasattr(self, name):
raise AttributeError('can\'t set attribute')
else:
self.__dict__[name] = value
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, index):
return self.collection[index]
def __repr__(self):
return repr(self.collection)
def __len__(self):
return len(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(-1)
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)
-suits_ordered = [Diamonds, Clubs, Hearts, Spades]
-ranks_standard_ordered = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+SUITS_ORDERED = [Diamonds, Clubs, Hearts, Spades]
+RANKS_STANDARD_ORDERED = [King, Queen, Jack, Ten, Nine, Eight, Seven,
Six, Five, Four, Three, Two, Ace]
-ranks_belote_ordered = [King, Queen, Jack, Ten, Nine, Eight, Seven, Ace]
-ranks_sixty_six_ordered = [King, Queen, Jack, Ten, Nine, Ace]
+RANKS_BELOTE_ORDERED = [King, Queen, Jack, Ten, Nine, Eight, Seven, Ace]
+RANKS_SIXTY_SIX_ORDERED = [King, Queen, Jack, Ten, Nine, Ace]
def StandardDeck():
result = CardCollection([])
- for suit in suits_ordered:
- for rank in ranks_standard_ordered:
+ for suit in SUITS_ORDERED:
+ for rank in RANKS_STANDARD_ORDERED:
result.add(Card(rank, suit))
return result
def BeloteDeck():
result = CardCollection([])
- for suit in suits_ordered:
- for rank in ranks_belote_ordered:
+ for suit in SUITS_ORDERED:
+ for rank in RANKS_BELOTE_ORDERED:
result.add(Card(rank, suit))
return result
def SixtySixDeck():
result = CardCollection([])
- for suit in suits_ordered:
- for rank in ranks_sixty_six_ordered:
+ for suit in SUITS_ORDERED:
+ for rank in RANKS_SIXTY_SIX_ORDERED:
result.add(Card(rank, suit))
return result