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

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

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

Резултати

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

Код

from collections import OrderedDict
class Rank():
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
self.symbol = '2'
class Three(Rank):
def __init__(self):
self.symbol = '3'
class Four(Rank):
def __init__(self):
self.symbol = '4'
class Five(Rank):
def __init__(self):
self.symbol = '5'
class Six(Rank):
def __init__(self):
self.symbol = '6'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
class King(Rank):
def __init__(self):
self.symbol = 'K'
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
class Suit():
def __init__(self, color):
self.color = color
self.suit = self.__class__.__name__
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.__class__.__name__
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
RANKS = OrderedDict([('King', type(King())),
('Queen', type(Queen())),
('Jack', type(Jack())),
('Ten', type(Ten())),
('Nine', type(Nine())),
('Eight', type(Eight())),
('Seven', type(Seven())),
('Six', type(Six())),
('Five', type(Five())),
('Four', type(Four())),
('Three', type(Three())),
('Two', type(Two())),
('Ace', type(Ace()))])
SIXTY_SIX_RANKS = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
BELOTE_RANKS = SIXTY_SIX_RANKS + ['Eight', 'Seven']
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
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 __eq__(self, other):
return self._suit == other._suit and self._rank == other._rank
def __str__(self):
return "{0} of {1}".format(str(self._rank()), str(self._suit()))
class CardCollection():
def __init__(self, collection=[]):
self.cards = []
for item in collection:
self.cards.append(item)
def __len__(self):
return len(self.cards)
def __getitem__(self, i):
return self.cards[i]
def draw(self, index):
return str(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[len(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 StandardDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values()]
standard_deck = CardCollection(self_deck)
return standard_deck
def BeloteDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values() if str(rank()) in BELOTE_RANKS]
belote_deck = CardCollection(self_deck)
return belote_deck
def SixtySixDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values() if str(rank()) in SIXTY_SIX_RANKS]
sixty_six_deck = CardCollection(self_deck)
return sixty_six_deck

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

..E.............
======================================================================
ERROR: test_deck_draw (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-1wp12qm/test.py", line 150, in test_deck_draw
    self.assertEqual(card, deck.draw(i))
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 642, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 632, in _baseAssertEqual
    if not first == second:
  File "/tmp/d20140407-19315-1wp12qm/solution.py", line 151, in __eq__
    return self._suit == other._suit and self._rank == other._rank
AttributeError: 'str' object has no attribute '_suit'

----------------------------------------------------------------------
Ran 16 tests in 0.024s

FAILED (errors=1)

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

Иван обнови решението на 24.03.2014 23:54 (преди над 10 години)

+from collections import OrderedDict
+import itertools
+
+
+class Rank():
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Suit():
+ def __init__(self, color):
+ self.color = color
+ self.suit = self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.suit == other.suit
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+RANKS = OrderedDict([('King', type(King())),
+ ('Queen', type(Queen())),
+ ('Jack', type(Jack())),
+ ('Ten', type(Ten())),
+ ('Nine', type(Nine())),
+ ('Eight', type(Eight())),
+ ('Seven', type(Seven())),
+ ('Six', type(Six())),
+ ('Five', type(Five())),
+ ('Four', type(Four())),
+ ('Three', type(Three())),
+ ('Two', type(Two())),
+ ('Ace', type(Ace()))])
+
+
+SIXTY_SIX_RANKS = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
+
+
+BELOTE_RANKS = SIXTY_SIX_RANKS + ['Eight', 'Seven']
+
+
+SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
+ ('Hearts', type(Hearts())), ('Spades', type(Spades()))])
+
+
+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 __eq__(self, other):
+ return self._suit == other._suit and self._rank == other._rank
+
+ def __str__(self):
+ return "{0} of {1}".format(str(self._rank()), str(self._suit()))
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.cards = []
+ for item in collection:
+ self.cards.append(item)
+
+ def __len__(self):
+ return len(self.cards)
+
+ def __getitem__(self, i):
+ return self.cards[i]
+
+ def draw(self, index):
+ return str(self.cards.pop(index))
+
+ def draw_from_top(self):
+ return str(self.cards.pop())
+
+ def draw_from_bottom(self):
+ return str(self.cards.pop(0))
+
+ def top_card(self):
+ return str(self.cards[len(self.cards)-1])
+
+ def bottom_card(self):
+ return str(self.cards[0])
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ for i in range(len(self.cards)):
+ if card == self.cards[i]:
+ return i
+ raise ValueError('{0} is not in list'.format(str(card)))
+
+
+def StandardDeck():
+ self_deck = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values()]
+ standard_deck = CardCollection(self_deck)
+ return standard_deck
+
+
+def BeloteDeck():
+ self_deck = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values() if str(rank()) in BELOTE_RANKS]
+ belote_deck = CardCollection(self_deck)
+ return belote_deck
+
+
+def SixtySixDeck():
+ self_deck = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values() if str(rank()) in SIXTY_SIX_RANKS]
+ sixty_six_deck = CardCollection(self_deck)
+ return sixty_six_deck

Това не е най-добрия начин да зададеш стойност на атрибут: Rank.__init__(self, '2'). Можеш просто да го сложиш към self. Също така помисли за вариант динамично да дефинираш подкласовете на Suit и Rank. Hint: за целта можеш да ползваш type.

Иван обнови решението на 26.03.2014 02:13 (преди над 10 години)

from collections import OrderedDict
-import itertools
class Rank():
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
- Rank.__init__(self, '2')
+ self.symbol = '2'
class Three(Rank):
def __init__(self):
- Rank.__init__(self, '3')
+ self.symbol = '3'
class Four(Rank):
def __init__(self):
- Rank.__init__(self, '4')
+ self.symbol = '4'
class Five(Rank):
def __init__(self):
- Rank.__init__(self, '5')
+ self.symbol = '5'
class Six(Rank):
def __init__(self):
- Rank.__init__(self, '6')
+ self.symbol = '6'
class Seven(Rank):
def __init__(self):
- Rank.__init__(self, '7')
+ self.symbol = '7'
class Eight(Rank):
def __init__(self):
- Rank.__init__(self, '8')
+ self.symbol = '8'
class Nine(Rank):
def __init__(self):
- Rank.__init__(self, '9')
+ self.symbol = '9'
class Ten(Rank):
def __init__(self):
- Rank.__init__(self, '10')
+ self.symbol = '10'
class Jack(Rank):
def __init__(self):
- Rank.__init__(self, 'J')
+ self.symbol = 'J'
class Queen(Rank):
def __init__(self):
- Rank.__init__(self, 'Q')
+ self.symbol = 'Q'
class King(Rank):
def __init__(self):
- Rank.__init__(self, 'K')
+ self.symbol = 'K'
class Ace(Rank):
def __init__(self):
- Rank.__init__(self, 'A')
+ self.symbol = 'A'
class Suit():
def __init__(self, color):
self.color = color
self.suit = self.__class__.__name__
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.__class__.__name__
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
RANKS = OrderedDict([('King', type(King())),
('Queen', type(Queen())),
('Jack', type(Jack())),
('Ten', type(Ten())),
('Nine', type(Nine())),
('Eight', type(Eight())),
('Seven', type(Seven())),
('Six', type(Six())),
('Five', type(Five())),
('Four', type(Four())),
('Three', type(Three())),
('Two', type(Two())),
('Ace', type(Ace()))])
SIXTY_SIX_RANKS = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
BELOTE_RANKS = SIXTY_SIX_RANKS + ['Eight', 'Seven']
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
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 __eq__(self, other):
return self._suit == other._suit and self._rank == other._rank
def __str__(self):
return "{0} of {1}".format(str(self._rank()), str(self._suit()))
class CardCollection():
def __init__(self, collection=[]):
self.cards = []
for item in collection:
self.cards.append(item)
def __len__(self):
return len(self.cards)
def __getitem__(self, i):
return self.cards[i]
def draw(self, index):
return str(self.cards.pop(index))
def draw_from_top(self):
return str(self.cards.pop())
def draw_from_bottom(self):
return str(self.cards.pop(0))
def top_card(self):
return str(self.cards[len(self.cards)-1])
def bottom_card(self):
return str(self.cards[0])
def add(self, card):
self.cards.append(card)
def index(self, card):
- for i in range(len(self.cards)):
- if card == self.cards[i]:
- return i
- raise ValueError('{0} is not in list'.format(str(card)))
+ return self.cards.index(card)
def StandardDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
- for rank in RANKS.values()]
+ for rank in RANKS.values()]
standard_deck = CardCollection(self_deck)
return standard_deck
def BeloteDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values() if str(rank()) in BELOTE_RANKS]
belote_deck = CardCollection(self_deck)
return belote_deck
def SixtySixDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values() if str(rank()) in SIXTY_SIX_RANKS]
sixty_six_deck = CardCollection(self_deck)
return sixty_six_deck

Иван обнови решението на 26.03.2014 15:35 (преди над 10 години)

from collections import OrderedDict
class Rank():
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
self.symbol = '2'
class Three(Rank):
def __init__(self):
self.symbol = '3'
class Four(Rank):
def __init__(self):
self.symbol = '4'
class Five(Rank):
def __init__(self):
self.symbol = '5'
class Six(Rank):
def __init__(self):
self.symbol = '6'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
class King(Rank):
def __init__(self):
self.symbol = 'K'
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
class Suit():
def __init__(self, color):
self.color = color
self.suit = self.__class__.__name__
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.__class__.__name__
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
RANKS = OrderedDict([('King', type(King())),
('Queen', type(Queen())),
('Jack', type(Jack())),
('Ten', type(Ten())),
('Nine', type(Nine())),
('Eight', type(Eight())),
('Seven', type(Seven())),
('Six', type(Six())),
('Five', type(Five())),
('Four', type(Four())),
('Three', type(Three())),
('Two', type(Two())),
('Ace', type(Ace()))])
SIXTY_SIX_RANKS = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
BELOTE_RANKS = SIXTY_SIX_RANKS + ['Eight', 'Seven']
SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
('Hearts', type(Hearts())), ('Spades', type(Spades()))])
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 __eq__(self, other):
return self._suit == other._suit and self._rank == other._rank
def __str__(self):
return "{0} of {1}".format(str(self._rank()), str(self._suit()))
class CardCollection():
def __init__(self, collection=[]):
self.cards = []
for item in collection:
self.cards.append(item)
def __len__(self):
return len(self.cards)
def __getitem__(self, i):
return self.cards[i]
def draw(self, index):
return str(self.cards.pop(index))
def draw_from_top(self):
- return str(self.cards.pop())
+ return self.cards.pop()
def draw_from_bottom(self):
- return str(self.cards.pop(0))
+ return self.cards.pop(0)
def top_card(self):
- return str(self.cards[len(self.cards)-1])
+ return self.cards[len(self.cards)-1]
def bottom_card(self):
- return str(self.cards[0])
+ return self.cards[0]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def StandardDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values()]
standard_deck = CardCollection(self_deck)
return standard_deck
def BeloteDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values() if str(rank()) in BELOTE_RANKS]
belote_deck = CardCollection(self_deck)
return belote_deck
def SixtySixDeck():
self_deck = [Card(rank, suit) for suit in SUITS.values()
for rank in RANKS.values() if str(rank()) in SIXTY_SIX_RANKS]
sixty_six_deck = CardCollection(self_deck)
return sixty_six_deck