Решение на Тесте карти от Любослав Павлов

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

Към профила на Любослав Павлов

Резултати

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

Код

#usin deque because it matches pretty good our requirements and it has good functionality
from collections import deque
ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6', 'Seven': '7',
'Eight': '8', 'Nine': '9', 'Ten': '10', 'Jack': 'J', 'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
suits = {'Clubs': 'black', 'Diamonds': 'red', 'Hearts': 'red', 'Spades': 'black'}
class Rank:
rank = ""
symbol = ""
def __str__(self):
return self.rank
def __eq__(self, other):
return self.rank == other.rank
class Suit:
suits = ""
color = ""
def __str__(self):
return self.suit
def __eq__(self, other):
return self.suits == other.suits
RANKS = dict((i, type(i, (Rank, ), dict(rank = i, symbol = ranks[i] ))) for i in ranks.keys())
SUITS = dict((i, type(i, (Suit, ), dict(suit = i, color = suits[i] ))) for i in suits.keys())
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return "%s of %s" % (self.rank, self.suit)
# the same as "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(deque):
def __init__(self, collection):
self.collection = deque(collection)
def __getitem__(self, i):
return (self.collection)[i]
def draw(self,index):
self.collection.rotate(-index)
self.collection.popleft()
self.collection.rotate(index)
def draw_from_top(self):
self.collection.pop()
def draw_from_bottom(self):
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)
def index(self, card):
if card in CardCollection:
pass
def StandartDeck():
deck = []
for i in SUITS:
for j in RANKS:
deck.append(str(Card(RANKS[j], SUITS[i])))
return deck

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

EEEE.EEE....F...
======================================================================
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-3zstl1/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-3zstl1/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-3zstl1/test.py", line 141, in test_deck_draw
    self.assertEqual(card, deck.draw_from_top())
  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-3zstl1/solution.py", line 49, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'NoneType' object has no attribute 'rank'

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

======================================================================
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-3zstl1/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_card_equals (test.CardTest)
----------------------------------------------------------------------
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-3zstl1/test.py", line 74, in test_card_equals
    self.assertNotEqual(aos1, solution.Card(solution.RANKS["Ace"], solution.SUITS["Hearts"]))
AssertionError: <solution.Card object at 0xb77d4ccc> == <solution.Card object at 0xb77d4e6c>

----------------------------------------------------------------------
Ran 16 tests in 0.021s

FAILED (failures=1, errors=7)

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

Любослав обнови решението на 26.03.2014 13:30 (преди над 10 години)

+ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6', 'Seven': '7',
+ 'Eight': '8', 'Nine': '9', 'Ten': '10', 'Jack': 'J', 'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
+
+suits = {'Spades': 'black', 'Clubs': 'black', 'Hearts': 'red', 'Diamonds': 'red'}
+
+
+
+class Rank(object):
+
+ rank = ""
+ symbol = ""
+
+ def __str__(self):
+ return self.rank
+
+ def __eq__(self, other):
+ return self.rank == others.rank
+
+
+class Suit(object):
+
+ suits = ""
+ color = ""
+
+ def __str__(self):
+ return self.suits
+
+ def __eq__(self, other):
+ return self.suits == other.suits
+
+
+
+RANKS = dict((i, type(i, (Rank, ), dict(rank = i, symbol =[i] ))) for i in ranks.keys())
+
+SUITS = dict((i, type(i, (Rank, ), dict(suit = i, color = [i] ))) for i in suits.keys())
+
+
+class Card(object):
+
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return "%s of %s" % (self.rank,self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit

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

+#usin deque because it matches pretty good our requirements and it has good functionality
+from collections import deque
+
ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6', 'Seven': '7',
'Eight': '8', 'Nine': '9', 'Ten': '10', 'Jack': 'J', 'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
-suits = {'Spades': 'black', 'Clubs': 'black', 'Hearts': 'red', 'Diamonds': 'red'}
+suits = {'Clubs': 'black', 'Diamonds': 'red', 'Hearts': 'red', 'Spades': 'black'}
+class Rank:
-class Rank(object):
-
rank = ""
symbol = ""
def __str__(self):
return self.rank
def __eq__(self, other):
- return self.rank == others.rank
+ return self.rank == other.rank
-class Suit(object):
+class Suit:
suits = ""
color = ""
def __str__(self):
- return self.suits
+ return self.suit
def __eq__(self, other):
return self.suits == other.suits
+RANKS = dict((i, type(i, (Rank, ), dict(rank = i, symbol = ranks[i] ))) for i in ranks.keys())
-RANKS = dict((i, type(i, (Rank, ), dict(rank = i, symbol =[i] ))) for i in ranks.keys())
+SUITS = dict((i, type(i, (Suit, ), dict(suit = i, color = suits[i] ))) for i in suits.keys())
-SUITS = dict((i, type(i, (Rank, ), dict(suit = i, color = [i] ))) for i in suits.keys())
+class Card:
-
-class Card(object):
-
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
-
+
def __str__(self):
- return "%s of %s" % (self.rank,self.suit)
-
+ return "%s of %s" % (self.rank, self.suit)
+ # the same as "return "{0} of {1}".format(self.rank,self.suit)"
+
def __eq__(self, other):
- return self.rank == other.rank and self.suit == other.suit
+ return self.rank == other.rank and self.suit == other.suit
+
+class CardCollection(deque):
+
+ def __init__(self, collection):
+ self.collection = deque(collection)
+
+ def __getitem__(self, i):
+ return (self.collection)[i]
+
+ def draw(self,index):
+ self.collection.rotate(-index)
+ self.collection.popleft()
+ self.collection.rotate(index)
+
+ def draw_from_top(self):
+ self.collection.pop()
+
+ def draw_from_bottom(self):
+ 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)
+
+ def index(self, card):
+ if card in CardCollection:
+ pass
+
+
+
+def StandartDeck():
+ deck = []
+ for i in SUITS:
+ for j in RANKS:
+ deck.append(str(Card(RANKS[j], SUITS[i])))
+ return deck