Решение на Тесте карти от Мариан Ламбовски

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

Към профила на Мариан Ламбовски

Резултати

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

Код

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
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
RANKS = {'Two' : Two, 'Three' : Three, 'Four' : Four,
'Five' : Five, 'Six' : Six, 'Seven' : Seven,
'Eight' : Eight, 'Nine': Nine, 'Ten' : Ten,
'Jack' : Jack, 'Queen' : Queen, 'King' : King,
'Ace' : Ace}
SUITS = {'Spades' : Spades, 'Hearts' : Hearts,
'Diamonds' : Diamonds, 'Clubs' : Clubs}
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', suit())
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return "{0} of {1}".format(self.rank, self.suit)
class CardCollection:
def __init__(self, collection = []):
self.collection = collection
def index(self, card):
for i in range(len(self.collection)):
if self.collection[i] == card:
return i
raise ValueError("<Card {}> is not in list".format(card))
def __getitem__(self, i):
return "Card " + str(self.collection[i])
def __len__(self):
return len(self.collection)
def draw(self, index):
print(self.collection[index])
self.collection.pop(index)
def draw_from_top(self):
print(self.collection[-1])
self.collection.pop()
def draw_from_bottom(self):
print(self.collection[0])
self.collection.pop(0)
def top_card(self):
print(self[-1])
def bottom_card(self):
print(self[0])
def add(self, card):
self.collection.append(card)
def StandardDeck():
rank_list = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Six', 'Five', 'Four',
'Three', 'Two', 'Ace']
suit_list = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
deck = CardCollection()
standard_deck = []
for suit in suit_list:
for rank in rank_list:
deck.add(Card(RANKS[rank],SUITS[suit]))
for card in deck:
standard_deck.append(str(card))
return standard_deck
def BeloteDeck():
rank_list = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Ace']
suit_list = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
deck = CardCollection()
belote_deck = []
for suit in suit_list:
for rank in rank_list:
deck.add(Card(RANKS[rank],SUITS[suit]))
for card in deck:
belote_deck.append(str(card))
return belote_deck
def SixtySixDeck():
rank_list = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
suit_list = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
deck = CardCollection()
sixty_six_deck = []
for suit in suit_list:
for rank in rank_list:
deck.add(Card(RANKS[rank],SUITS[suit]))
for card in deck:
sixty_six_deck.append(str(card))
return sixty_six_deck

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

FEF
Stdout:
Card Nine of Diamonds
Nine of Diamonds
Card Four of Hearts
Four of Hearts
Ace of Hearts
FFE
Stdout:
Card Ten of Hearts
FF........
======================================================================
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-1imnpr5/test.py", line 121, in test_deck_add
    self.assertEqual(deck[0], card1)
  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-1imnpr5/solution.py", line 107, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'str' object has no attribute 'rank'

======================================================================
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-1imnpr5/test.py", line 133, in test_deck_order
    self.assertEqual(deck.top_card(), card3)
  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-1imnpr5/solution.py", line 107, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'NoneType' object has no attribute 'rank'

Stdout:
Card Ten of Hearts

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

First differing element 0:
King of Diamonds
Card King of Diamonds

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

======================================================================
FAIL: 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-1imnpr5/test.py", line 150, in test_deck_draw
    self.assertEqual(card, deck.draw(i))
AssertionError: 'Card Ace of Hearts' != None

Stdout:
Card Nine of Diamonds
Nine of Diamonds
Card Four of Hearts
Four of Hearts
Ace of Hearts

======================================================================
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-1imnpr5/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
AssertionError: 0 != 33

======================================================================
FAIL: test_deck_iteration (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-1imnpr5/test.py", line 167, in test_deck_iteration
    self.assertIsInstance(card, solution.Card)
AssertionError: 'Card Four of Hearts' is not an instance of <class 'solution.Card'>

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

First differing element 0:
King of Diamonds
Card King of Diamonds

Second list contains 38 additional elements.
First extra element 24:
Card King of Spades

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

First differing element 0:
King of Diamonds
Card King of Diamonds

Second list contains 62 additional elements.
First extra element 52:
Card Jack of Hearts

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

----------------------------------------------------------------------
Ran 16 tests in 1.113s

FAILED (failures=6, errors=2)

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

Мариан обнови решението на 26.03.2014 16:20 (преди над 10 години)

+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
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+RANKS = {'Two' : Two, 'Three' : Three, 'Four' : Four,
+ 'Five' : Five, 'Six' : Six, 'Seven' : Seven,
+ 'Eight' : Eight, 'Nine': Nine, 'Ten' : Ten,
+ 'Jack' : Jack, 'Queen' : Queen, 'King' : King,
+ 'Ace' : Ace}
+
+SUITS = {'Spades' : Spades, 'Hearts' : Hearts,
+ 'Diamonds' : Diamonds, 'Clubs' : Clubs}
+
+class Card:
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return "{0} of {1}".format(self.rank, self.suit)
+
+class CardCollection:
+ def __init__(self, collection = []):
+ self.collection = collection
+
+ def index(self, card):
+ for i in range(len(self.collection)):
+ if self.collection[i] == card:
+ return i
+ raise ValueError("<Card {}> is not in list".format(card))
+
+ def __getitem__(self, i):
+ return "Card " + str(self.collection[i])
+
+ def __len__(self):
+ return len(self.collection)
+
+ def draw(self, index):
+ print(self.collection[index])
+ self.collection.pop(index)
+
+ def draw_from_top(self):
+ print(self.collection[-1])
+ self.collection.pop()
+
+ def draw_from_bottom(self):
+ print(self.collection[0])
+ self.collection.pop(0)
+
+ def top_card(self):
+ print(self[-1])
+
+ def bottom_card(self):
+ print(self[0])
+
+ def add(self, card):
+ self.collection.append(card)
+
+def StandardDeck():
+ rank_list = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Six', 'Five', 'Four',
+ 'Three', 'Two', 'Ace']
+ suit_list = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ deck = CardCollection()
+ standard_deck = []
+
+ for suit in suit_list:
+ for rank in rank_list:
+ deck.add(Card(RANKS[rank],SUITS[suit]))
+
+ for card in deck:
+ standard_deck.append(str(card))
+
+ return standard_deck
+
+def BeloteDeck():
+ rank_list = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Ace']
+ suit_list = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ deck = CardCollection()
+ belote_deck = []
+
+ for suit in suit_list:
+ for rank in rank_list:
+ deck.add(Card(RANKS[rank],SUITS[suit]))
+
+ for card in deck:
+ belote_deck.append(str(card))
+
+ return belote_deck
+
+def SixtySixDeck():
+ rank_list = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ suit_list = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ deck = CardCollection()
+ sixty_six_deck = []
+
+ for suit in suit_list:
+ for rank in rank_list:
+ deck.add(Card(RANKS[rank],SUITS[suit]))
+
+ for card in deck:
+ sixty_six_deck.append(str(card))
+
+ return sixty_six_deck