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

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

Към профила на Марианна Владимирова

Резултати

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

Код

from collections import deque
class Rank:
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.rank
class King(Rank):
def __init__(self):
self.symbol = 'K'
self.rank = 'King'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
self.rank = 'Queen'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
self.rank = 'Jack'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
self.rank = 'Ten'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
self.rank = 'Nine'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
self.rank = 'Eight'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
self.rank = 'Seven'
class Six(Rank):
def __init__(self):
self.symbol = '6'
self.rank = 'Six'
class Five(Rank):
def __init__(self):
self.symbol = '5'
self.rank = 'Five'
class Four(Rank):
def __init__(self):
self.symbol = '4'
self.rank = 'Four'
class Three(Rank):
def __init__(self):
self.symbol = '3'
self.rank = 'Three'
class Two(Rank):
def __init__(self):
self.symbol = '2'
self.rank = 'Two'
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
self.rank = 'Ace'
class Suit:
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.suit
class Hearts(Suit):
def __init__(self):
self.color = 'red'
self.suit = 'Hearts'
class Clubs(Suit):
def __init__(self):
self.color = 'black'
self.suit = 'Clubs'
class Spades(Suit):
def __init__(self):
self.color = 'black'
self.suit = 'Spades'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
self.suit = 'Diamonds'
RANKS = {'King': King, 'Queen': Queen, 'Jack': Jack, 'Ten': Ten, 'Nine': Nine,
'Eight': Eight, 'Seven': Seven, 'Six': Six, 'Five': Five,
'Four': Four, 'Three': Three, 'Two': Two, 'Ace': Ace}
SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
'Diamonds': Diamonds}
class Card():
def __init__(self, rank, suit):
self.rank = rank()
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
class CardCollection:
def __init__(self, collection):
self.collection = deque(collection)
def __getitem__(self, i):
return (self.collection)[i]
def draw(self, index):
card = self.collection.rotate(-index)
self.collection.popleft()
self.collection.rotate(index)
return card
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.popleft()
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
rank_order = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six',
'Five', 'Four', 'Three', 'Two', 'Ace']
suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
def StandardDeck():
standard_deck = []
for suit in suit_order:
for rank in rank_order:
standard_deck.append("Card {0} of {1}".format(RANKS[rank],
SUITS[suit]))
deck = CardCollection(standard_deck)
return deck

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

EEEE.EEF........
======================================================================
ERROR: 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-1k2ob5g/test.py", line 174, in test_belote_deck
    cards = [str(card) for card in solution.BeloteDeck()]
AttributeError: 'module' object has no attribute 'BeloteDeck'

======================================================================
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-1k2ob5g/test.py", line 117, in test_deck_add
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

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

======================================================================
ERROR: 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-1k2ob5g/test.py", line 154, in test_deck_index
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

======================================================================
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-1k2ob5g/test.py", line 129, in test_deck_order
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

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

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

First differing element 0:
King of Diamonds
Card <class 'solution.King'> of <class 'solution.Diamonds'>

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

----------------------------------------------------------------------
Ran 16 tests in 0.047s

FAILED (failures=1, errors=6)

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

Марианна обнови решението на 24.03.2014 08:39 (преди около 10 години)

+class Rank:
+ def __eq__(self,other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return str(self.symbol)
+
+class King(Rank):
+ def __init__(self):
+ self.symbol = 'K'

Имам един проблем, ако може да ми погнете. Като се опитам да създам обект от тип King (k = King()) и ми дава следния проблем: NameError: name 'King' is not defined

Пробвах да работя и с примера за вектора от лекциите, но и от там получавам същия проблем.

Версията на Python, която ползвам е 3.3.5rc1 на Windows ,и 3.3.2 на Fedora

Благодаря Ви предварително!

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

+from collections import deque
+
class Rank:
- def __eq__(self,other):
+ def __eq__(self, other):
return self.symbol == other.symbol
-
+
def __str__(self):
- return str(self.symbol)
+ return self.rank
class King(Rank):
def __init__(self):
- self.symbol = 'K'
+ self.symbol = 'K'
+ self.rank = 'King'
+
+class Queen(Rank):
+ def __init__(self):
+ self.symbol = 'Q'
+ self.rank = 'Queen'
+
+class Jack(Rank):
+ def __init__(self):
+ self.symbol = 'J'
+ self.rank = 'Jack'
+
+class Ten(Rank):
+ def __init__(self):
+ self.symbol = '10'
+ self.rank = 'Ten'
+
+class Nine(Rank):
+ def __init__(self):
+ self.symbol = '9'
+ self.rank = 'Nine'
+
+class Eight(Rank):
+ def __init__(self):
+ self.symbol = '8'
+ self.rank = 'Eight'
+
+class Seven(Rank):
+ def __init__(self):
+ self.symbol = '7'
+ self.rank = 'Seven'
+
+class Six(Rank):
+ def __init__(self):
+ self.symbol = '6'
+ self.rank = 'Six'
+
+class Five(Rank):
+ def __init__(self):
+ self.symbol = '5'
+ self.rank = 'Five'
+
+class Four(Rank):
+ def __init__(self):
+ self.symbol = '4'
+ self.rank = 'Four'
+
+class Three(Rank):
+ def __init__(self):
+ self.symbol = '3'
+ self.rank = 'Three'
+
+class Two(Rank):
+ def __init__(self):
+ self.symbol = '2'
+ self.rank = 'Two'
+
+class Ace(Rank):
+ def __init__(self):
+ self.symbol = 'A'
+ self.rank = 'Ace'
+
+class Suit:
+ def __eq__(self, other):
+ return self.suit == other.suit
+
+ def __str__(self):
+ return self.suit
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+ self.suit = 'Hearts'
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'black'
+ self.suit = 'Clubs'
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+ self.suit = 'Spades'
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'red'
+ self.suit = 'Diamonds'
+
+RANKS = {'King':King, 'Queen':Queen, 'Jack':Jack, 'Ten':Ten, 'Nine':Nine,
+ 'Eight':Eight, 'Seven':Seven, 'Six':Six, 'Five':Five, 'Four':Four,
+ 'Three':Three, 'Two':Two, 'Ace':Ace}
+
+SUITS = {'Hearts':Hearts, 'Clubs':Clubs, 'Spades':Spades, 'Diamonds':Diamonds}
+
+class Card():
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ 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
+
+class CardCollection:
+ def __init__(self, collection):
+ self.collection = deque(collection)
+
+ def __getitem__(self, i):
+ return (self.collection)[i]
+
+ def draw(self,index):
+ card = self.collection.rotate(-index)
+ self.collection.popleft()
+ self.collection.rotate(index)
+ return card
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.popleft()
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)

Марианна обнови решението на 26.03.2014 14:29 (преди около 10 години)

from collections import deque
+
class Rank:
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.rank
+
class King(Rank):
def __init__(self):
self.symbol = 'K'
self.rank = 'King'
+
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
self.rank = 'Queen'
+
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
self.rank = 'Jack'
+
class Ten(Rank):
def __init__(self):
self.symbol = '10'
self.rank = 'Ten'
+
class Nine(Rank):
def __init__(self):
self.symbol = '9'
self.rank = 'Nine'
+
class Eight(Rank):
def __init__(self):
self.symbol = '8'
self.rank = 'Eight'
+
class Seven(Rank):
def __init__(self):
self.symbol = '7'
self.rank = 'Seven'
+
class Six(Rank):
def __init__(self):
self.symbol = '6'
self.rank = 'Six'
+
class Five(Rank):
def __init__(self):
self.symbol = '5'
self.rank = 'Five'
+
class Four(Rank):
def __init__(self):
self.symbol = '4'
self.rank = 'Four'
+
class Three(Rank):
def __init__(self):
self.symbol = '3'
self.rank = 'Three'
+
class Two(Rank):
def __init__(self):
self.symbol = '2'
self.rank = 'Two'
+
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
self.rank = 'Ace'
+
class Suit:
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.suit
+
class Hearts(Suit):
def __init__(self):
self.color = 'red'
self.suit = 'Hearts'
+
class Clubs(Suit):
def __init__(self):
self.color = 'black'
self.suit = 'Clubs'
+
class Spades(Suit):
def __init__(self):
self.color = 'black'
self.suit = 'Spades'
+
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
self.suit = 'Diamonds'
-RANKS = {'King':King, 'Queen':Queen, 'Jack':Jack, 'Ten':Ten, 'Nine':Nine,
- 'Eight':Eight, 'Seven':Seven, 'Six':Six, 'Five':Five, 'Four':Four,
- 'Three':Three, 'Two':Two, 'Ace':Ace}
-SUITS = {'Hearts':Hearts, 'Clubs':Clubs, 'Spades':Spades, 'Diamonds':Diamonds}
+RANKS = {'King': King, 'Queen': Queen, 'Jack': Jack, 'Ten': Ten, 'Nine': Nine,
+ 'Eight': Eight, 'Seven': Seven, 'Six': Six, 'Five': Five,
+ 'Four': Four, 'Three': Three, 'Two': Two, 'Ace': Ace}
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
+ 'Diamonds': Diamonds}
+
+
class Card():
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
-
+
def __str__(self):
- return "{0} of {1}".format(self.rank,self.suit)
+ return "{0} of {1}".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
+
class CardCollection:
def __init__(self, collection):
self.collection = deque(collection)
-
+
def __getitem__(self, i):
return (self.collection)[i]
- def draw(self,index):
+ def draw(self, index):
card = self.collection.rotate(-index)
self.collection.popleft()
self.collection.rotate(index)
return card
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.popleft()
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
- self.collection.append(card)
+ self.collection.append(card)

Марианна обнови решението на 26.03.2014 15:52 (преди около 10 години)

from collections import deque
class Rank:
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.rank
class King(Rank):
def __init__(self):
self.symbol = 'K'
self.rank = 'King'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
self.rank = 'Queen'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
self.rank = 'Jack'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
self.rank = 'Ten'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
self.rank = 'Nine'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
self.rank = 'Eight'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
self.rank = 'Seven'
class Six(Rank):
def __init__(self):
self.symbol = '6'
self.rank = 'Six'
class Five(Rank):
def __init__(self):
self.symbol = '5'
self.rank = 'Five'
class Four(Rank):
def __init__(self):
self.symbol = '4'
self.rank = 'Four'
class Three(Rank):
def __init__(self):
self.symbol = '3'
self.rank = 'Three'
class Two(Rank):
def __init__(self):
self.symbol = '2'
self.rank = 'Two'
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
self.rank = 'Ace'
class Suit:
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.suit
class Hearts(Suit):
def __init__(self):
self.color = 'red'
self.suit = 'Hearts'
class Clubs(Suit):
def __init__(self):
self.color = 'black'
self.suit = 'Clubs'
class Spades(Suit):
def __init__(self):
self.color = 'black'
self.suit = 'Spades'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
self.suit = 'Diamonds'
RANKS = {'King': King, 'Queen': Queen, 'Jack': Jack, 'Ten': Ten, 'Nine': Nine,
'Eight': Eight, 'Seven': Seven, 'Six': Six, 'Five': Five,
'Four': Four, 'Three': Three, 'Two': Two, 'Ace': Ace}
SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
'Diamonds': Diamonds}
class Card():
def __init__(self, rank, suit):
self.rank = rank()
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
class CardCollection:
def __init__(self, collection):
self.collection = deque(collection)
def __getitem__(self, i):
return (self.collection)[i]
def draw(self, index):
card = self.collection.rotate(-index)
self.collection.popleft()
self.collection.rotate(index)
return card
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.popleft()
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
- self.collection.append(card)
+ self.collection.append(card)
+
+rank_order = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six',
+ 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+suit_order = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+def StandardDeck():
+ standard_deck = []
+ for suit in suit_order:
+ for rank in rank_order:
+ standard_deck.append("Card {0} of {1}".format(RANKS[rank],
+ SUITS[suit]))
+ deck = CardCollection(standard_deck)
+ return deck