Решение на Тесте карти от Радослав Георгиев

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

Към профила на Радослав Георгиев

Резултати

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

Код

class Rank:
def __init__(self, name, symbol):
self.name = name
self.symbol = symbol
def __eq__(self, other):
return self.name == other.name and self.symbol == other.symbol
def __call__(self):
return self
def __str__(self):
return self.name
class Suit:
def __init__(self, name, color):
self.name = name
self.color = color
def __eq__(self, other):
return self.name == other.name and self.color == other.color
def __call__(self):
return self
def __str__(self):
return self.name
class King(Rank):
def __init__(self):
Rank.__init__(self, 'King', 'K')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Queen', 'Q')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'Jack', 'J')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, 'Ten', '10')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, 'Nine', '9')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, 'Eight', '8')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, 'Seven', '7')
class Six(Rank):
def __init__(self):
Rank.__init__(self, 'Six', '6')
class Five(Rank):
def __init__(self):
Rank.__init__(self, 'Five', '5')
class Four(Rank):
def __init__(self):
Rank.__init__(self, 'Four', '4')
class Three(Rank):
def __init__(self):
Rank.__init__(self, 'Three', '3')
class Two(Rank):
def __init__(self):
Rank.__init__(self, 'Two', '2')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'Ace', 'A')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'Diamonds', 'red')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'Hearts', 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'Spades', 'black')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'Clubs', 'black')
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', suit())
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __call__(self):
return self
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __delattr__(self, *args):
raise AttributeError("can't set attribute")
class CardCollection:
def __init__(self, collection = []):
self.collection = collection
def __getitem__(self, card):
return self.collection[card]
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
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)
def __str__(self):
return str([str(card) for card in self.collection])
RANKS = dict()
RANKS['King'] = King
RANKS['Queen'] = Queen
RANKS['Jack'] = Jack
RANKS['Ten'] = Ten
RANKS['Nine'] = Nine
RANKS['Eight'] = Eight
RANKS['Seven'] = Seven
RANKS['Six'] = Six
RANKS['Five'] = Five
RANKS['Four'] = Four
RANKS['Three'] = Three
RANKS['Two'] = Two
RANKS['Ace'] = Ace
SUITS = dict()
SUITS['Diamonds'] = Diamonds
SUITS['Hearts'] = Hearts
SUITS['Spades'] = Spades
SUITS['Clubs'] = Clubs
def StandardDeck():
ordered_ranks = (RANKS['King'], RANKS['Queen'], RANKS['Jack'], RANKS['Ten'], RANKS['Nine'], RANKS['Eight'], RANKS['Seven'], RANKS['Six'],
RANKS['Five'], RANKS['Four'], RANKS['Three'], RANKS['Two'], RANKS['Ace'])
ordered_suits = (SUITS['Diamonds'], SUITS['Hearts'], SUITS['Spades'], SUITS['Clubs'])
deck = CardCollection(list())
for suit in ordered_suits:
for rank in ordered_ranks:
deck.add(Card(rank, suit))
return deck
def BeloteDeck():
ordered_ranks = (RANKS['Ace'], RANKS['King'], RANKS['Queen'], RANKS['Jack'], RANKS['Ten'], RANKS['Nine'], RANKS['Eight'], RANKS['Seven'])
ordered_suits = (SUITS['Diamonds'], SUITS['Hearts'], SUITS['Spades'], SUITS['Clubs'])
deck = CardCollection(list())
for suit in ordered_suits:
for rank in ordered_ranks:
deck.add(Card(rank, suit))
return deck
def SixtySixDeck():
ordered_ranks = (RANKS['Ace'], RANKS['King'], RANKS['Queen'], RANKS['Jack'], RANKS['Ten'], RANKS['Nine'])
ordered_suits = (SUITS['Diamonds'], SUITS['Hearts'], SUITS['Spades'], SUITS['Clubs'])
deck = CardCollection(list())
for suit in ordered_suits:
for rank in ordered_ranks:
deck.add(Card(rank, suit))
return deck

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

FEEF.FFF........
======================================================================
ERROR: test_deck_add (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-y1dom5/test.py", line 126, in test_deck_add
    self.assertEqual(len(deck), 2)
TypeError: object of type 'CardCollection' has no len()

======================================================================
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-y1dom5/test.py", line 142, in test_deck_draw
    self.assertEqual(len(deck), 51)
TypeError: object of type 'CardCollection' has no len()

======================================================================
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-y1dom5/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_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 975 characters long. Set self.maxDiff to None to see it.

======================================================================
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-y1dom5/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-y1dom5/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <solution.Card object at 0xb7828e6c> != <solution.Card object at 0xb783ba4c>

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

First differing element 13:
King of Clubs
King of Hearts

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

----------------------------------------------------------------------
Ran 16 tests in 0.046s

FAILED (failures=5, errors=2)

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

Радослав обнови решението на 26.03.2014 16:55 (преди над 10 години)

+class Rank:
+ def __init__(self, name, symbol):
+ self.name = name
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.name == other.name and self.symbol == other.symbol
+
+ def __call__(self):
+ return self
+
+ def __str__(self):
+ return self.name
+
+
+class Suit:
+ def __init__(self, name, color):
+ self.name = name
+ self.color = color
+
+ def __eq__(self, other):
+ return self.name == other.name and self.color == other.color
+
+ def __call__(self):
+ return self
+
+ def __str__(self):
+ return self.name
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'King', 'K')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Queen', 'Q')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Jack', 'J')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ten', '10')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Nine', '9')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Eight', '8')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Seven', '7')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Six', '6')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Five', '5')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Four', '4')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Three', '3')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Two', '2')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ace', 'A')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Diamonds', 'red')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Hearts', 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Spades', 'black')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Clubs', 'black')
+
+
+class Card:
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __call__(self):
+ return self
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+ def __delattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+
+class CardCollection:
+ def __init__(self, collection = []):
+ self.collection = collection
+
+ def __getitem__(self, card):
+ return self.collection[card]
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ 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)
+
+ def __str__(self):
+ return str([str(card) for card in self.collection])
+
+
+RANKS = dict()
+RANKS['King'] = King
+RANKS['Queen'] = Queen
+RANKS['Jack'] = Jack
+RANKS['Ten'] = Ten
+RANKS['Nine'] = Nine
+RANKS['Eight'] = Eight
+RANKS['Seven'] = Seven
+RANKS['Six'] = Six
+RANKS['Five'] = Five
+RANKS['Four'] = Four
+RANKS['Three'] = Three
+RANKS['Two'] = Two
+RANKS['Ace'] = Ace
+
+
+SUITS = dict()
+SUITS['Diamonds'] = Diamonds
+SUITS['Hearts'] = Hearts
+SUITS['Spades'] = Spades
+SUITS['Clubs'] = Clubs
+
+
+def StandardDeck():
+ ordered_ranks = (RANKS['King'], RANKS['Queen'], RANKS['Jack'], RANKS['Ten'], RANKS['Nine'], RANKS['Eight'], RANKS['Seven'], RANKS['Six'],
+ RANKS['Five'], RANKS['Four'], RANKS['Three'], RANKS['Two'], RANKS['Ace'])
+ ordered_suits = (SUITS['Diamonds'], SUITS['Hearts'], SUITS['Spades'], SUITS['Clubs'])
+ deck = CardCollection(list())
+
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ deck.add(Card(rank, suit))
+
+ return deck
+
+
+def BeloteDeck():
+ ordered_ranks = (RANKS['Ace'], RANKS['King'], RANKS['Queen'], RANKS['Jack'], RANKS['Ten'], RANKS['Nine'], RANKS['Eight'], RANKS['Seven'])
+ ordered_suits = (SUITS['Diamonds'], SUITS['Hearts'], SUITS['Spades'], SUITS['Clubs'])
+ deck = CardCollection(list())
+
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ deck.add(Card(rank, suit))
+
+ return deck
+
+def SixtySixDeck():
+ ordered_ranks = (RANKS['Ace'], RANKS['King'], RANKS['Queen'], RANKS['Jack'], RANKS['Ten'], RANKS['Nine'])
+ ordered_suits = (SUITS['Diamonds'], SUITS['Hearts'], SUITS['Spades'], SUITS['Clubs'])
+ deck = CardCollection(list())
+
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ deck.add(Card(rank, suit))
+
+ return deck