Решение на Тесте карти от Николай Масларски

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

Към профила на Николай Масларски

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return (self.symbol)
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'Ace')
class Two(Rank):
def __init__(self):
Rank.__init__(self, 'Two')
class Three(Rank):
def __init__(self):
Rank.__init__(self, 'Three')
class Four(Rank):
def __init__(self):
Rank.__init__(self, 'Four')
class Five(Rank):
def __init__(self):
Rank.__init__(self, 'Five')
class Six(Rank):
def __init__(self):
Rank.__init__(self, 'Six')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, 'Seven')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, 'Eight')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, 'Nine')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, 'Ten')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'Jack')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Queen')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'King')
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return (self.color)
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'Clubs')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'Diamonds')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'Hearts')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'Spades')
RANKS = OrderedDict([('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 = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs),
('Hearts', Hearts), ('Spades', Spades)])
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return ("{} of {}".format(self.rank, self.suit))
def __repr__(self):
return ("{} of {}".format(self.rank, self.suit))
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def add(self, card):
self.collection.append(card)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(-1)
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 index(self, card):
return self.collection.index(card)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def __str__(self):
return self.collection
def CreateDeck(last_card=None):
deck_cards = []
for suit_key in SUITS:
for rank_key in RANKS:
if rank_key != last_card:
deck_cards.append(Card(RANKS[rank_key], SUITS[suit_key]))
else:
deck_cards.append(Card(RANKS["Ace"], SUITS[suit_key]))
break
return CardCollection(deck_cards)
def StandardDeck():
return CreateDeck()
def BeloteDeck():
return CreateDeck("Six")
def SixtySixDeck():
return CreateDeck("Eight")

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

...F.F..........
======================================================================
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-17tyeeg/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-17tyeeg/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: Seven of Hearts != Five of Clubs

----------------------------------------------------------------------
Ran 16 tests in 0.027s

FAILED (failures=2)

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

Николай обнови решението на 26.03.2014 03:12 (преди над 10 години)

+from collections import OrderedDict
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return (self.symbol)
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ace')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Two')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Three')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Four')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Five')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Six')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Seven')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Eight')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Nine')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ten')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Jack')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Queen')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'King')
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return (self.color)
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Clubs')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Diamonds')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Hearts')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Spades')
+
+
+RANKS = OrderedDict([('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 = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs),
+ ('Hearts', Hearts), ('Spades', Spades)])
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return ("{} of {}".format(self.rank, self.suit))
+
+ def __repr__(self):
+ return ("{} of {}".format(self.rank, self.suit))
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop(-1)
+
+ 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 index(self, card):
+ return self.collection.index(card)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ return self.collection
+
+
+def CreateDeck(last_card=None):
+ deck_cards = []
+ for suit_key in SUITS:
+ for rank_key in RANKS:
+ if rank_key != last_card:
+ deck_cards.append(Card(RANKS[rank_key], SUITS[suit_key]))
+ else:
+ deck_cards.append(Card(RANKS["Ace"], SUITS[suit_key]))
+ break
+ return CardCollection(deck_cards)
+
+
+def StandardDeck():
+ return CreateDeck()
+
+
+def BeloteDeck():
+ return CreateDeck("Six")
+
+
+def SixtySixDeck():
+ return CreateDeck("Eight")