Решение на Тесте карти от Ирина Иванова

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

Към профила на Ирина Иванова

Резултати

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

Код

RANKS_AND_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
('Seven', '7'), ('Six', '6'), ('Five', '5'),
('Four', '4'), ('Three', '3'), ('Two', '2'),
('Ace', 'A')]
SUITS_AND_COLORS = [('Diamonds', 'red'), ('Clubs', 'black'),
('Hearts', 'red'), ('Spades', 'black')]
class Rank:
def __init__(self, symbol):
self.__symbol = symbol
def __eq__(self, other):
return self.__symbol == other.__symbol
@classmethod
def __str__(cls):
return str(cls.__name__)
class Suit:
def __init__(self, color):
self.__color = color
def __eq__(self, other):
return str(self) == str(other)
@classmethod
def __str__(cls):
return str(cls.__name__)
def ClassFactory(name, value, base_class):
def __init__(self):
base_class.__init__(self, value)
return type(name, (base_class,), {"__init__": __init__})
RANKS = {x[0]: ClassFactory(x[0], x[1], Rank) for x in RANKS_AND_SYMBOLS}
SUITS = {x[0]: ClassFactory(x[0], x[1], Suit) for x in SUITS_AND_COLORS}
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 "<Card {}>".format(str(self))
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def __str__(self):
return "[{}]".format(", ".join(repr(x) for x in self.collection))
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.draw(len(self) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.collection[len(self) - 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 StandartDeck():
return CardCollection([Card(RANKS[rank[0]], SUITS[suit[0]])
for suit in SUITS_AND_COLORS
for rank in RANKS_AND_SYMBOLS])
def BeloteDeck():
return CardCollection([x for x in StandartDeck()
if not x.rank._Rank__symbol.isdigit()
or (x.rank._Rank__symbol.isdigit()
and int(x.rank._Rank__symbol) > 6)])
def SixtySixDeck():
return CardCollection([x for x in StandartDeck()
if not x.rank._Rank__symbol.isdigit()
or (x.rank._Rank__symbol.isdigit()
and int(x.rank._Rank__symbol) > 8)])

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

...F.F.E........
======================================================================
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-q4xrn/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_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-q4xrn/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-q4xrn/test.py", line 135, in test_deck_order
    self.assertEqual(deck[1], card2)
AssertionError: <Card Jack of Hearts> != <Card Queen of Spades>

----------------------------------------------------------------------
Ran 16 tests in 0.023s

FAILED (failures=2, errors=1)

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

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

+RANKS_AND_SYMBOLS = [['King', 'K'], ['Queen', 'Q'], ['Jack', 'J'],
+ ['Ten', '10'], ['Nine', '9'], ['Eight', '8'],
+ ['Seven', '7'], ['Six', '6'], ['Five', '5'],
+ ['Four', '4'], ['Three', '3'], ['Two', '2'],
+ ['Ace', 'A']]
+
+
+SUITS_AND_COLORS = [['Diamonds', 'red'], ['Clubs', 'black'],
+ ['Hearts', 'red'], ['Spades', 'black']]
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __eq__(self, other):
+ return self.__symbol == other.__symbol
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+
+def ClassFactory(name, value, base_class):
+ def __init__(self):
+ base_class.__init__(self, value)
+ return type(name, (base_class,), {"__init__": __init__})
+
+
+RANKS = {x[0]: ClassFactory(x[0], x[1], Rank) for x in RANKS_AND_SYMBOLS}
+SUITS = {x[0]: ClassFactory(x[0], x[1], Suit) for x in SUITS_AND_COLORS}
+
+
+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 "<Card {}>".format(str(self))
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ return "[{}]".format(", ".join(repr(x) for x in self.collection))
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(len(self) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.collection[len(self) - 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 StandartDeck():
+ return CardCollection([Card(RANKS[rank[0]], SUITS[suit[0]])
+ for suit in SUITS_AND_COLORS
+ for rank in RANKS_AND_SYMBOLS])
+
+
+def BeloteDeck():
+ return CardCollection([x for x in StandartDeck()
+ if not x.rank._Rank__symbol.isdigit()
+ or (x.rank._Rank__symbol.isdigit()
+ and int(x.rank._Rank__symbol) > 6)])
+
+
+def SixtySixDeck():
+ return CardCollection([x for x in StandartDeck()
+ if not x.rank._Rank__symbol.isdigit()
+ or (x.rank._Rank__symbol.isdigit()
+ and int(x.rank._Rank__symbol) > 8)])

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

-RANKS_AND_SYMBOLS = [['King', 'K'], ['Queen', 'Q'], ['Jack', 'J'],
- ['Ten', '10'], ['Nine', '9'], ['Eight', '8'],
- ['Seven', '7'], ['Six', '6'], ['Five', '5'],
- ['Four', '4'], ['Three', '3'], ['Two', '2'],
- ['Ace', 'A']]
+RANKS_AND_SYMBOLS = [('King', 'K'), ('Queen', 'Q'), ('Jack', 'J'),
+ ('Ten', '10'), ('Nine', '9'), ('Eight', '8'),
+ ('Seven', '7'), ('Six', '6'), ('Five', '5'),
+ ('Four', '4'), ('Three', '3'), ('Two', '2'),
+ ('Ace', 'A')]
-SUITS_AND_COLORS = [['Diamonds', 'red'], ['Clubs', 'black'],
- ['Hearts', 'red'], ['Spades', 'black']]
+SUITS_AND_COLORS = [('Diamonds', 'red'), ('Clubs', 'black'),
+ ('Hearts', 'red'), ('Spades', 'black')]
class Rank:
def __init__(self, symbol):
self.__symbol = symbol
def __eq__(self, other):
return self.__symbol == other.__symbol
@classmethod
def __str__(cls):
return str(cls.__name__)
class Suit:
def __init__(self, color):
self.__color = color
def __eq__(self, other):
return str(self) == str(other)
@classmethod
def __str__(cls):
return str(cls.__name__)
def ClassFactory(name, value, base_class):
def __init__(self):
base_class.__init__(self, value)
return type(name, (base_class,), {"__init__": __init__})
RANKS = {x[0]: ClassFactory(x[0], x[1], Rank) for x in RANKS_AND_SYMBOLS}
SUITS = {x[0]: ClassFactory(x[0], x[1], Suit) for x in SUITS_AND_COLORS}
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 "<Card {}>".format(str(self))
class CardCollection:
def __init__(self, collection=[]):
self.collection = collection
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def __str__(self):
return "[{}]".format(", ".join(repr(x) for x in self.collection))
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.draw(len(self) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self.collection[len(self) - 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 StandartDeck():
return CardCollection([Card(RANKS[rank[0]], SUITS[suit[0]])
for suit in SUITS_AND_COLORS
for rank in RANKS_AND_SYMBOLS])
def BeloteDeck():
return CardCollection([x for x in StandartDeck()
if not x.rank._Rank__symbol.isdigit()
or (x.rank._Rank__symbol.isdigit()
and int(x.rank._Rank__symbol) > 6)])
def SixtySixDeck():
return CardCollection([x for x in StandartDeck()
if not x.rank._Rank__symbol.isdigit()
or (x.rank._Rank__symbol.isdigit()
and int(x.rank._Rank__symbol) > 8)])