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

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

Към профила на Константин Тодоров

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 11 успешни тест(а)
  • 5 неуспешни тест(а)

Код

class Rank:
def __init__(self, symbol_):
self.symbol = symbol_
class Suit:
def __init__(self, color_):
self.color = color_
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
def __str__(self):
return 'Two'
def __eq__(self, other):
return self.symbol == other.symbol
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
def __str__(self):
return 'Three'
def __eq__(self, other):
return self.symbol == other.symbol
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
def __str__(self):
return 'Four'
def __eq__(self, other):
return self.symbol == other.symbol
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
def __str__(self):
return 'Five'
def __eq__(self, other):
return self.symbol == other.symbol
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
def __str__(self):
return 'Six'
def __eq__(self, other):
return self.symbol == other.symbol
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
def __str__(self):
return 'Seven'
def __eq__(self, other):
return self.symbol == other.symbol
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
def __str__(self):
return 'Eight'
def __eq__(self, other):
return self.symbol == other.symbol
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
def __str__(self):
return 'Nine'
def __eq__(self, other):
return self.symbol == other.symbol
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
def __str__(self):
return 'Ten'
def __eq__(self, other):
return self.symbol == other.symbol
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
def __str__(self):
return 'Jack'
def __eq__(self, other):
return self.symbol == other.symbol
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
def __str__(self):
return 'Queen'
def __eq__(self, other):
return self.symbol == other.symbol
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
def __str__(self):
return 'King'
def __eq__(self, other):
return self.symbol == other.symbol
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
def __str__(self):
return 'Ace'
def __eq__(self, other):
return self.symbol == other.symbol
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
def __str__(self):
return 'Spades'
def __eq__(self, other):
return type(self) == type(other)
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
def __str__(self):
return 'Diamonds'
def __eq__(self, other):
return type(self) == type(other)
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
def __str__(self):
return 'Clubs'
def __eq__(self, other):
return type(self) == type(other)
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
def __str__(self):
return 'Hearts'
def __eq__(self, other):
return type(self) == type(other)
from collections import OrderedDict
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()))])
SUITS = OrderedDict([('Hearts', type(Hearts())), ('Spades', type(Spades())),
('Clubs', type(Clubs())), ('Diamonds', type(Diamonds()))])
class Card():
def __init__(self, rank_, suit_):
object.__setattr__(self, 'rank', rank_())
object.__setattr__(self, 'suit', suit_())
def __str__(self):
return "{0} of {1}".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __setattr__(self, name, value):
raise AttributeError(": can't set attribute")
class CardCollection:
def __init__(self, collection=list()):
self.deck = list(collection)
def __len__(self):
return len(self.deck)
def __getitem__(self, i):
return (self.deck)[i]
def add(self, card):
return self.deck.append(card)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[len(self.deck) - 1]
def bottom_card(sefl):
return self.deck[0]
def index(self, card):
return self.deck.index(card)
def all_cards():
cards = list()
for suit in SUITS:
for rank in RANKS:
cards.append(Card(RANKS[str(rank)], SUITS[str(suit)]))
return cards
def StandratDeck():
deck = CardCollection(all_cards())
return deck
def BeloteDeck():
deck = list()
no_need_ranks = [Two(), Three(), Four(), Five(), Six()]
cards = all_cards()
for card in cards:
if card.rank not in no_need_ranks:
deck.append(card)
return CardCollection(deck)
def SixtySixDeck():
deck = list()
no_need_ranks = [Two(), Three(), Four(), Five(), Six(), Seven(), Eight()]
cards = all_cards()
for card in cards:
if card.rank not in no_need_ranks:
deck.append(card)
return CardCollection(deck)

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

F.E..EFE........
======================================================================
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-1vuikbs/test.py", line 144, in test_deck_draw
    card = deck.bottom_card()
  File "/tmp/d20140407-19315-1vuikbs/solution.py", line 272, in bottom_card
    return self.deck[0]
NameError: global name 'self' is not defined

======================================================================
ERROR: 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-1vuikbs/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
  File "/tmp/d20140407-19315-1vuikbs/solution.py", line 272, in bottom_card
    return self.deck[0]
NameError: global name 'self' is not defined

======================================================================
ERROR: 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-1vuikbs/test.py", line 170, in test_standard_deck
    cards = [str(card) for card in solution.StandardDeck()]
AttributeError: 'module' object has no attribute 'StandardDeck'

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

First differing element 0:
King of Diamonds
King of Hearts

Diff is 1123 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-1vuikbs/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Hearts', 'Queen of H...

First differing element 0:
King of Diamonds
King of Hearts

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

----------------------------------------------------------------------
Ran 16 tests in 0.042s

FAILED (failures=2, errors=3)

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

Константин обнови решението на 26.03.2014 12:14 (преди над 10 години)

+class Rank:
+
+ def __init__(self, symbol_):
+ self.symbol = symbol_
+
+
+class Suit:
+
+ def __init__(self, color_):
+ self.color = color_
+
+
+class Two(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __str__(self):
+ return 'Two'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Three(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __str__(self):
+ return 'Three'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Four(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __str__(self):
+ return 'Four'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Five(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __str__(self):
+ return 'Five'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Six(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __str__(self):
+ return 'Six'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Seven(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __str__(self):
+ return 'Seven'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Eight(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __str__(self):
+ return 'Eight'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Nine(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __str__(self):
+ return 'Nine'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Ten(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __str__(self):
+ return 'Ten'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Jack(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __str__(self):
+ return 'Jack'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Queen(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __str__(self):
+ return 'Queen'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class King(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __str__(self):
+ return 'King'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Ace(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __str__(self):
+ return 'Ace'
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class Spades(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __str__(self):
+ return 'Spades'
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+
+class Diamonds(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __str__(self):
+ return 'Diamonds'
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+
+class Clubs(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __str__(self):
+ return 'Clubs'
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+
+class Hearts(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __str__(self):
+ return 'Hearts'
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+from collections import OrderedDict
+
+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()))])
+
+SUITS = OrderedDict([('Hearts', type(Hearts())), ('Spades', type(Spades())),
+ ('Clubs', type(Clubs())), ('Diamonds', type(Diamonds()))])
+
+
+class Card():
+
+ def __init__(self, rank_, suit_):
+ object.__setattr__(self, 'rank', rank_())
+ object.__setattr__(self, 'suit', suit_())
+
+ def __str__(self):
+ return "{0} of {1}".format(self.rank, self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __setattr__(self, name, value):
+ raise AttributeError(": can't set attribute")
+
+
+class CardCollection:
+
+ def __init__(self, collection=list()):
+ self.deck = list(collection)
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __getitem__(self, i):
+ return (self.deck)[i]
+
+ def add(self, card):
+ return self.deck.append(card)
+
+ def draw(self, index):
+ return self.deck.pop(index)
+
+ def draw_from_top(self):
+ return self.deck.pop()
+
+ def draw_from_bottom(self):
+ return self.deck.pop(0)
+
+ def top_card(self):
+ return self.deck[len(self.deck) - 1]
+
+ def bottom_card(sefl):
+ return self.deck[0]
+
+ def index(self, card):
+ return self.deck.index(card)
+
+
+def all_cards():
+ cards = list()
+ for suit in SUITS:
+ for rank in RANKS:
+ cards.append(Card(RANKS[str(rank)], SUITS[str(suit)]))
+ return cards
+
+
+def StandratDeck():
+ deck = CardCollection(all_cards())
+ return deck
+
+
+def BeloteDeck():
+ deck = list()
+ no_need_ranks = [Two(), Three(), Four(), Five(), Six()]
+ cards = all_cards()
+ for card in cards:
+ if card.rank not in no_need_ranks:
+ deck.append(card)
+ return CardCollection(deck)
+
+
+def SixtySixDeck():
+ deck = list()
+ no_need_ranks = [Two(), Three(), Four(), Five(), Six(), Seven(), Eight()]
+ cards = all_cards()
+ for card in cards:
+ if card.rank not in no_need_ranks:
+ deck.append(card)
+ return CardCollection(deck)