Решение на Тесте карти от Филип Митов

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

Към профила на Филип Митов

Резултати

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

Код

from collections import OrderedDict
ranks_in_order = [
('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')
]
all_ranks = OrderedDict(ranks_in_order)
class Rank:
symbol = ''
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
for element in all_ranks.items():
if element[1] == self.symbol:
return "{}".format(element[0])
RANKS = {}
for key, element in all_ranks.items():
RANKS[key] = type(key, (Rank, ), {'symbol': element})
suits_in_order = [
('Diamonds', 'red'), ('Clubs', 'black'),
('Hearts', 'red'), ('Spades', 'black')
]
all_suits = OrderedDict(suits_in_order)
class Suit:
color = ''
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return "{}".format(self.__class__.__name__)
SUITS = {}
for key, element in all_suits.items():
SUITS[key] = type(key, (Suit, ), {'color': element})
class Card:
rank = 0
suit = 0
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', suit())
def __setattr__(self, index, value):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and other.suit == other.suit
def __str__(self):
return '{} of {}'.format(self.rank, self.suit)
class CardCollection:
def __init__(self, collection=list()):
self.card_collection = list(collection)
def add(self, card):
self.card_collection.append(card)
def draw_from_top(self):
return self.card_collection.pop()
def draw_from_bottom(self):
return self.card_collection.pop(0)
def top_card(self):
return self.card_collection[len(self.card_collection) - 1]
def bottom_card(self):
return self.card_collection[0]
def index(self, card):
for index in range(len(self.card_collection)):
if self.card_collection[index].rank == card.rank \
and self.card_collection[index].suit == card.suit:
return index
raise ValueError("{} is not in list".format(card))
def __getitem__(self, i):
return self.card_collection.__getitem__(i)
def __iter__(self):
return self.card_collection.__iter__()
def __len__(self):
return len(self.card_collection)
def __str__(self, index):
return "Card {}".format(self.card_collection[index])
def StandardDeck():
standart_deck = []
for one_suit in all_suits:
for one_rank in all_ranks:
standart_deck.append(Card(RANKS[one_rank], SUITS[one_suit]))
return CardCollection(standart_deck)
def BeloteDeck():
belot_deck = []
for one_suit in all_suits:
for one_rank in range(0, 7):
belot_deck.append(
Card(RANKS[ranks_in_order[one_rank][0]], SUITS[one_suit])
)
belot_deck.append(
Card(RANKS[ranks_in_order[-1][0]], SUITS[one_suit])
)
return CardCollection(belot_deck)
def SixtySixDeck():
sixtysix_deck = []
for one_suit in all_suits:
for one_rank in range(0, 5):
sixtysix_deck.append(
Card(RANKS[ranks_in_order[one_rank][0]], SUITS[one_suit])
)
sixtysix_deck.append(
Card(RANKS[ranks_in_order[-1][0]], SUITS[one_suit])
)
return CardCollection(sixtysix_deck)

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

..E.........F...
======================================================================
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-1svp9ae/test.py", line 150, in test_deck_draw
    self.assertEqual(card, deck.draw(i))
AttributeError: 'CardCollection' object has no attribute 'draw'

======================================================================
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-1svp9ae/test.py", line 74, in test_card_equals
    self.assertNotEqual(aos1, solution.Card(solution.RANKS["Ace"], solution.SUITS["Hearts"]))
AssertionError: <solution.Card object at 0xb77e9f4c> == <solution.Card object at 0xb77e9fcc>

----------------------------------------------------------------------
Ran 16 tests in 0.026s

FAILED (failures=1, errors=1)

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

Филип обнови решението на 26.03.2014 13:01 (преди над 10 години)

+from collections import OrderedDict
+
+ranks_in_order = [
+ ('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')
+]
+
+all_ranks = OrderedDict(ranks_in_order)
+
+class Rank:
+ symbol = ''
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ for element in all_ranks.items():
+ if element[1] == self.symbol:
+ return "{}".format(element[0])
+
+RANKS = {}
+for key, element in all_ranks.items():
+ RANKS[key] = type(key, (Rank, ), {'symbol': element})
+
+suits_in_order = [
+ ('Diamonds','red'), ('Clubs','black'),
+ ('Hearts','red'), ('Spades','black')
+]
+
+all_suits = OrderedDict(suits_in_order)
+
+class Suit:
+ color = ''
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+ def __str__(self):
+ return "{}".format(self.__class__.__name__)
+
+SUITS = {}
+for key, element in all_suits.items():
+ SUITS[key] = type(key, (Suit, ), {'color': element})
+
+class Card:
+ rank = 0
+ suit = 0
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __setattr__(self, index, value):
+ raise AttributeError("can't set attribute")
+
+ def __eq__(self, other):
+ return self.rank == other.rank and other.suit == other.suit
+
+ def __str__(self):
+ return '{} of {}'.format(self.rank, self.suit)
+
+class CardCollection:
+ def __init__(self, collection=list()):
+ self.card_collection = list(collection)
+
+ def add(self, card):
+ self.card_collection.append(card)
+
+ def draw_from_top(self):
+ return self.card_collection.pop()
+
+ def draw_from_bottom(self):
+ return self.card_collection.pop(0)
+
+ def top_card(self):
+ return self.card_collection[len(self.card_collection) - 1]
+
+ def bottom_card(self):
+ return self.card_collection[0]
+
+ def index(self, card):
+ for index in range(len(self.card_collection)):
+ if self.card_collection[index].rank == card.rank and self.card_collection[index].suit == card.suit:
+ return index
+ raise ValueError("{} is not in list".format(card))
+
+ def __getitem__(self, i):
+ return self.card_collection.__getitem__(i)
+
+ def __iter__(self):
+ return self.card_collection.__iter__()
+
+ def __len__(self):
+ return len(self.card_collection)
+
+ def __str__(self, index):
+ return "Card {}".format(self.card_collection[index])
+
+def StandardDeck():
+ standart_deck = []
+ for one_suit in all_suits:
+ for one_rank in all_ranks:
+ standart_deck.append(Card(RANKS[one_rank], SUITS[one_suit]))
+ return CardCollection(standart_deck)
+
+def BeloteDeck():
+ belot_deck = []
+ for one_suit in all_suits:
+ for one_rank in range(0, 7):
+ belot_deck.append(Card(RANKS[ranks_in_order[one_rank][0]],
+ SUITS[one_suit]))
+
+ belot_deck.append(Card(RANKS[ranks_in_order[-1][0]],
+ SUITS[one_suit]))
+ return CardCollection(belot_deck)
+
+def SixtySixDeck():
+ sixtysix_deck = []
+ for one_suit in all_suits:
+ for one_rank in range(0, 5):
+ sixtysix_deck.append(Card(RANKS[ranks_in_order[one_rank][0]], SUITS[one_suit]))
+ sixtysix_deck.append(Card(RANKS[ranks_in_order[-1][0]],
+ SUITS[one_suit]))
+ return CardCollection(sixtysix_deck)

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

from collections import OrderedDict
ranks_in_order = [
('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')
]
all_ranks = OrderedDict(ranks_in_order)
class Rank:
symbol = ''
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
for element in all_ranks.items():
if element[1] == self.symbol:
return "{}".format(element[0])
RANKS = {}
for key, element in all_ranks.items():
RANKS[key] = type(key, (Rank, ), {'symbol': element})
suits_in_order = [
- ('Diamonds','red'), ('Clubs','black'),
- ('Hearts','red'), ('Spades','black')
+ ('Diamonds', 'red'), ('Clubs', 'black'),
+ ('Hearts', 'red'), ('Spades', 'black')
]
all_suits = OrderedDict(suits_in_order)
class Suit:
color = ''
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return "{}".format(self.__class__.__name__)
SUITS = {}
for key, element in all_suits.items():
SUITS[key] = type(key, (Suit, ), {'color': element})
class Card:
rank = 0
suit = 0
def __init__(self, rank, suit):
object.__setattr__(self, 'rank', rank())
object.__setattr__(self, 'suit', suit())
def __setattr__(self, index, value):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and other.suit == other.suit
def __str__(self):
return '{} of {}'.format(self.rank, self.suit)
class CardCollection:
def __init__(self, collection=list()):
self.card_collection = list(collection)
def add(self, card):
self.card_collection.append(card)
def draw_from_top(self):
return self.card_collection.pop()
def draw_from_bottom(self):
return self.card_collection.pop(0)
def top_card(self):
return self.card_collection[len(self.card_collection) - 1]
def bottom_card(self):
return self.card_collection[0]
def index(self, card):
for index in range(len(self.card_collection)):
- if self.card_collection[index].rank == card.rank and self.card_collection[index].suit == card.suit:
+ if self.card_collection[index].rank == card.rank \
+ and self.card_collection[index].suit == card.suit:
return index
raise ValueError("{} is not in list".format(card))
def __getitem__(self, i):
return self.card_collection.__getitem__(i)
def __iter__(self):
return self.card_collection.__iter__()
def __len__(self):
return len(self.card_collection)
def __str__(self, index):
return "Card {}".format(self.card_collection[index])
def StandardDeck():
standart_deck = []
for one_suit in all_suits:
for one_rank in all_ranks:
standart_deck.append(Card(RANKS[one_rank], SUITS[one_suit]))
return CardCollection(standart_deck)
def BeloteDeck():
belot_deck = []
for one_suit in all_suits:
for one_rank in range(0, 7):
- belot_deck.append(Card(RANKS[ranks_in_order[one_rank][0]],
- SUITS[one_suit]))
+ belot_deck.append(
+ Card(RANKS[ranks_in_order[one_rank][0]], SUITS[one_suit])
+ )
- belot_deck.append(Card(RANKS[ranks_in_order[-1][0]],
- SUITS[one_suit]))
+ belot_deck.append(
+ Card(RANKS[ranks_in_order[-1][0]], SUITS[one_suit])
+ )
return CardCollection(belot_deck)
def SixtySixDeck():
sixtysix_deck = []
for one_suit in all_suits:
for one_rank in range(0, 5):
- sixtysix_deck.append(Card(RANKS[ranks_in_order[one_rank][0]], SUITS[one_suit]))
- sixtysix_deck.append(Card(RANKS[ranks_in_order[-1][0]],
- SUITS[one_suit]))
+ sixtysix_deck.append(
+ Card(RANKS[ranks_in_order[one_rank][0]], SUITS[one_suit])
+ )
+
+ sixtysix_deck.append(
+ Card(RANKS[ranks_in_order[-1][0]], SUITS[one_suit])
+ )
return CardCollection(sixtysix_deck)