Решение на Тесте карти от Момчил Станчев

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

Към профила на Момчил Станчев

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __init__(self):
pass
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return "{}".format(self.symbol)
class Suit:
def __init__(self):
pass
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return "{}".format(self.color)
RANKS = OrderedDict([
('Ace', type('Ace', (Rank,), {'symbol': 'Ace'})),
('King', type('King', (Rank,), {'symbol': 'King'})),
('Queen', type('Queen', (Rank,), {'symbol': 'Queen'})),
('Jack', type('Jack', (Rank,), {'symbol': 'Jack'})),
('Ten', type('Ten', (Rank,), {'symbol': 'Ten'})),
('Nine', type('Nine', (Rank,), {'symbol': 'Nine'})),
('Eight', type('Eight', (Rank,), {'symbol': 'Eight'})),
('Seven', type('Seven', (Rank,), {'symbol': 'Seven'})),
('Six', type('Six', (Rank,), {'symbol': 'Six'})),
('Five', type('Five', (Rank,), {'symbol': 'Five'})),
('Four', type('Four', (Rank,), {'symbol': 'Four'})),
('Three', type('Three', (Rank,), {'symbol': 'Three'})),
('Two', type('Two', (Rank,), {'symbol': 'Two'}))
])
SUITS = OrderedDict([
('Clubs', type('Clubs', (Suit,), {'color': 'Clubs'})),
('Diamonds', type('Diamonds', (Suit,), {'color': 'Diamonds'})),
('Hearts', type('Hearts', (Suit,), {'color': 'Hearts'})),
('Spades', type('Spades', (Suit,), {'color': '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 "{} of {}".format(str(self.rank), str(self.suit))
@property
def rank(self):
return self._rank
@property
def suit(self):
return self._suit
class CardCollection:
def __init__(self, collection=None):
self._collection = []
if collection is not None:
self._collection = list(collection)
def __getitem__(self, item_position):
return self._collection[item_position]
def __delitem__(self, item_position):
del self._collection[item_position]
def __len__(self):
return len(self.collection)
def __str__(self):
return str(["{}".format(card) for card in self.collection])
@property
def collection(self):
return self._collection
def index(self, card):
return self.collection.index(card)
def add(self, card):
self._collection.append(card)
def top_card(self):
return self[-1]
def bottom_card(self):
return self[0]
def draw(self, index):
drawn_card = self[index]
del self[index]
return drawn_card
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def StandardDeck(lowest_rank='Two'):
lowest_rank_index = list(RANKS.keys()).index(lowest_rank)
return CardCollection([
Card(RANKS[rank], SUITS[suit])
for suit in SUITS.keys()
for (index, (rank, _)) in enumerate(RANKS.items())
if index <= lowest_rank_index
])
def BeloteDeck():
return StandardDeck(lowest_rank='Seven')
def SixtySixDeck():
return StandardDeck(lowest_rank='Nine')

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

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

First differing element 0:
King of Diamonds
Ace of Clubs

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

First differing element 0:
King of Diamonds
Ace of Clubs

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

First differing element 0:
King of Diamonds
Ace of Clubs

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

----------------------------------------------------------------------
Ran 16 tests in 0.044s

FAILED (failures=3)

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

Момчил обнови решението на 25.03.2014 17:16 (преди около 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 "{}".format(self.symbol)
+
+ @property
+ def symbol(self):
+ return self.__symbol
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ace')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Two')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Three')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Four')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Five')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Six')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Seven')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Eight')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Nine')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ten')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Jack')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Queen')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'King')
+
+RANKS = OrderedDict([
+ ('Ace', Ace), ('King', King), ('Queen', Queen), ('Jack', Jack),
+ ('Ten', Ten), ('Nine', Nine), ('Eight', Eight), ('Seven', Seven),
+ ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three),
+ ('Two', Two)
+ ])
+
+
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return "{}".format(self.color)
+
+ @property
+ def color(self):
+ return self.__color
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Hearts')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Clubs')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Spades')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Diamonds')
+
+
+SUITS = OrderedDict([
+ ('Clubs', Clubs), ('Diamonds', Diamonds),
+ ('Hearts', Hearts), ('Spades', Spades)
+ ])
+
+
+class Card(Rank, Suit):
+ 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 "{} of {}".format(str(self.rank), str(self.suit))
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ self.__collection = []
+ if collection is not None:
+ self.__collection = list(collection)
+
+ def __getitem__(self, item_position):
+ return self.__collection[item_position]
+
+ def __delitem__(self, item_position):
+ del self.__collection[item_position]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ return str(["{}".format(card) for card in self.collection])
+
+ @property
+ def collection(self):
+ return self.__collection
+
+ def index(self, card):
+ return self.collection.index(card)
+
+ def add(self, card):
+ self.__collection.append(card)
+
+ def top_card(self):
+ return self[-1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def draw(self, index):
+ drawn_card = self[index]
+ del self[index]
+ return drawn_card
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+
+def StandardDeck(lowest_rank='Two'):
+ lowest_rank_index = list(RANKS.keys()).index(lowest_rank)
+ return CardCollection([
+ Card(RANKS[rank], SUITS[suit])
+ for suit in SUITS.keys()
+ for (index, (rank, _)) in enumerate(RANKS.items())
+ if index <= lowest_rank_index
+ ])
+
+
+def BeloteDeck():
+ return StandardDeck(lowest_rank='Seven')
+
+
+def SixtySixDeck():
+ return StandardDeck(lowest_rank='Nine')
+
+
+# std_deck = StandardDeck()
+# print(std_deck)
+#
+# belot_deck = BeloteDeck()
+# print(belot_deck)
+#
+# santase_deck = SixtySixDeck()
+# print(santase_deck)
  • За базовите класове няма голям смисъл да ползваш private променливи.
  • Rank.__init__(self, 'Ace') - можеш просто да направиш така: self.symbol = 'Ace', няма голям смисъл да викаш конструктора на базовия клас.
  • Помисли за вариант динамично да конструираш под-класовете. Hint: това става с type. :)

Момчил обнови решението на 26.03.2014 16:39 (преди около 10 години)

from collections import OrderedDict
class Rank:
- def __init__(self, symbol):
- self.__symbol = symbol
+ def __init__(self):
+ pass
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return "{}".format(self.symbol)
- @property
- def symbol(self):
- return self.__symbol
-
-class Ace(Rank):
+class Suit:
def __init__(self):
- Rank.__init__(self, 'Ace')
+ pass
-
-class Two(Rank):
- def __init__(self):
- Rank.__init__(self, 'Two')
-
-
-class Three(Rank):
- def __init__(self):
- Rank.__init__(self, 'Three')
-
-
-class Four(Rank):
- def __init__(self):
- Rank.__init__(self, 'Four')
-
-
-class Five(Rank):
- def __init__(self):
- Rank.__init__(self, 'Five')
-
-
-class Six(Rank):
- def __init__(self):
- Rank.__init__(self, 'Six')
-
-
-class Seven(Rank):
- def __init__(self):
- Rank.__init__(self, 'Seven')
-
-
-class Eight(Rank):
- def __init__(self):
- Rank.__init__(self, 'Eight')
-
-
-class Nine(Rank):
- def __init__(self):
- Rank.__init__(self, 'Nine')
-
-
-class Ten(Rank):
- def __init__(self):
- Rank.__init__(self, 'Ten')
-
-
-class Jack(Rank):
- def __init__(self):
- Rank.__init__(self, 'Jack')
-
-
-class Queen(Rank):
- def __init__(self):
- Rank.__init__(self, 'Queen')
-
-
-class King(Rank):
- def __init__(self):
- Rank.__init__(self, 'King')
-
-RANKS = OrderedDict([
- ('Ace', Ace), ('King', King), ('Queen', Queen), ('Jack', Jack),
- ('Ten', Ten), ('Nine', Nine), ('Eight', Eight), ('Seven', Seven),
- ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three),
- ('Two', Two)
- ])
-
-
-class Suit:
- def __init__(self, color):
- self.__color = color
-
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return "{}".format(self.color)
- @property
- def color(self):
- return self.__color
+RANKS = OrderedDict([
+ ('Ace', type('Ace', (Rank,), {'symbol': 'Ace'})),
+ ('King', type('King', (Rank,), {'symbol': 'King'})),
+ ('Queen', type('Queen', (Rank,), {'symbol': 'Queen'})),
+ ('Jack', type('Jack', (Rank,), {'symbol': 'Jack'})),
+ ('Ten', type('Ten', (Rank,), {'symbol': 'Ten'})),
+ ('Nine', type('Nine', (Rank,), {'symbol': 'Nine'})),
+ ('Eight', type('Eight', (Rank,), {'symbol': 'Eight'})),
+ ('Seven', type('Seven', (Rank,), {'symbol': 'Seven'})),
+ ('Six', type('Six', (Rank,), {'symbol': 'Six'})),
+ ('Five', type('Five', (Rank,), {'symbol': 'Five'})),
+ ('Four', type('Four', (Rank,), {'symbol': 'Four'})),
+ ('Three', type('Three', (Rank,), {'symbol': 'Three'})),
+ ('Two', type('Two', (Rank,), {'symbol': 'Two'}))
+ ])
-class Hearts(Suit):
- def __init__(self):
- Suit.__init__(self, 'Hearts')
-
-class Clubs(Suit):
- def __init__(self):
- Suit.__init__(self, 'Clubs')
-
-
-class Spades(Suit):
- def __init__(self):
- Suit.__init__(self, 'Spades')
-
-
-class Diamonds(Suit):
- def __init__(self):
- Suit.__init__(self, 'Diamonds')
-
-
SUITS = OrderedDict([
- ('Clubs', Clubs), ('Diamonds', Diamonds),
- ('Hearts', Hearts), ('Spades', Spades)
+ ('Clubs', type('Clubs', (Suit,), {'color': 'Clubs'})),
+ ('Diamonds', type('Diamonds', (Suit,), {'color': 'Diamonds'})),
+ ('Hearts', type('Hearts', (Suit,), {'color': 'Hearts'})),
+ ('Spades', type('Spades', (Suit,), {'color': 'Spades'})),
])
-class Card(Rank, Suit):
+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 "{} of {}".format(str(self.rank), str(self.suit))
@property
def rank(self):
- return self.__rank
+ return self._rank
@property
def suit(self):
- return self.__suit
+ return self._suit
class CardCollection:
def __init__(self, collection=None):
- self.__collection = []
+ self._collection = []
if collection is not None:
- self.__collection = list(collection)
+ self._collection = list(collection)
def __getitem__(self, item_position):
- return self.__collection[item_position]
+ return self._collection[item_position]
def __delitem__(self, item_position):
- del self.__collection[item_position]
+ del self._collection[item_position]
def __len__(self):
return len(self.collection)
def __str__(self):
return str(["{}".format(card) for card in self.collection])
@property
def collection(self):
- return self.__collection
+ return self._collection
def index(self, card):
return self.collection.index(card)
def add(self, card):
- self.__collection.append(card)
+ self._collection.append(card)
def top_card(self):
return self[-1]
def bottom_card(self):
return self[0]
def draw(self, index):
drawn_card = self[index]
del self[index]
return drawn_card
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def StandardDeck(lowest_rank='Two'):
lowest_rank_index = list(RANKS.keys()).index(lowest_rank)
return CardCollection([
Card(RANKS[rank], SUITS[suit])
for suit in SUITS.keys()
for (index, (rank, _)) in enumerate(RANKS.items())
if index <= lowest_rank_index
])
def BeloteDeck():
return StandardDeck(lowest_rank='Seven')
def SixtySixDeck():
return StandardDeck(lowest_rank='Nine')
-
-
-# std_deck = StandardDeck()
-# print(std_deck)
-#
-# belot_deck = BeloteDeck()
-# print(belot_deck)
-#
-# santase_deck = SixtySixDeck()
-# print(santase_deck)