Решение на Тесте карти от Петър Парушев

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

Към профила на Петър Парушев

Резултати

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

Код

import collections
from functools import cmp_to_key
RANK_SYMBOLS_PRIORITIES = [('A', 'Ace', 1), ('K', 'King', 13),
('Q', 'Queen', 12), ('J', 'Jack', 11),
('10', 'Ten', 10), ('9', 'Nine', 9),
('8', 'Eight', 8), ('7', 'Seven', 7),
('6', 'Six', 6), ('5', 'Five', 5),
('4', 'Four', 4), ('3', 'Three', 3),
('2', 'Two', 2)]
SUIT_COLORS_PRIORITIES = [('Diamonds', 'red', 39), ('Clubs', 'black', 26),
('Hearts', 'red', 13), ('Spades', 'black', 0)]
RANKS = {}
SUITS = {}
class Rank():
symbol = "S"
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
for rank in RANK_SYMBOLS_PRIORITIES:
RANKS[rank[1]] = type(rank[1], (Rank, ), {'symbol': rank[0],
'priority': rank[2]})
class Suit():
color = 'color'
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return self.__class__.__name__
for suit in SUIT_COLORS_PRIORITIES:
SUITS[suit[0]] = type(suit[0], (Suit, ), {'color': suit[1],
'priority': suit[2]})
class Card():
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
@property
def priority(self):
return self.rank.priority + self.suit.priority
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection(collections.deque):
def __init__(self, collection):
for card in collection:
self.append(card)
def draw(self, index):
result = self[index]
del(self[index])
return result
def draw_from_top(self):
return self.pop()
def draw_from_bottom(self):
return self.popleft()
def top_card(self):
return self[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.append(card)
def index(self, card):
for i in range(0, len(self)):
if self[i] == card:
return i
raise ValueError
def generate_deck(game_name=None):
result = []
for suit in SUITS:
for rank in RANKS:
result.append(Card(RANKS[rank], SUITS[suit]))
result.sort(key=cmp_to_key(lambda x, y: y.priority - x.priority))
if game_name == "Belote":
unwanted_cards = []
for card in result:
if 1 < (card.priority % 13) < 7:
unwanted_cards.append(card)
result = [card for card in result if not card in unwanted_cards]
if game_name == "SixtySix":
unwanted_cards = []
for card in result:
if 1 < (card.priority % 13) < 9:
unwanted_cards.append(card)
result = [card for card in result if not card in unwanted_cards]
return result
def StandardDeck():
return CardCollection(generate_deck())
def BeloteDeck():
return CardCollection(generate_deck("Belote"))
def SixtySixDeck():
return CardCollection(generate_deck("SixtySix"))

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

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

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

----------------------------------------------------------------------
Ran 16 tests in 0.024s

FAILED (errors=3)

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

Петър обнови решението на 26.03.2014 01:16 (преди около 10 години)

+import collections
+from functools import cmp_to_key
+
+
+RANK_SYMBOLS_PRIORITIES = [('A', 'Ace', 1), ('K', 'King', 13),
+ ('Q', 'Queen', 12), ('J', 'Jack', 11),
+ ('10', 'Ten', 10), ('9', 'Nine', 9),
+ ('8', 'Eight', 8), ('7', 'Seven', 7),
+ ('6', 'Six', 6), ('5', 'Five', 5),
+ ('4', 'Four', 4), ('3', 'Three', 3),
+ ('2', 'Two', 2)]
+
+
+SUIT_COLORS_PRIORITIES = [('Diamonds', 'red', 39), ('Clubs', 'black', 26),
+ ('Hearts', 'red', 13), ('Spades', 'black', 0)]
+
+
+RANKS = {}
+
+
+SUITS = {}
+
+
+class Rank():
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+for rank in RANK_SYMBOLS_PRIORITIES:
+ RANKS[rank[1]] = type(rank[1], (Rank, ), {'symbol': rank[0],
+ 'priority': rank[2]})
+
+
+class Suit():
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+for suit in SUIT_COLORS_PRIORITIES:
+ SUITS[suit[0]] = type(suit[0], (Rank, ), {'color': suit[1],
+ 'priority': suit[2]})
+
+
+class Card():
+ def __init__(self, rank, suit):
+ self._rank = rank(rank.symbol)
+ self._suit = suit(suit.color)
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ @property
+ def priority(self):
+ return self._rank.priority + self._suit.priority
+
+ def __str__(self):
+ return "{} of {}".format(self.rank.__name__, self.suit.__name__)
+
+ def __eq__(self, other):
+ return self._rank == other.rank and self._suit == other.suit
+
+
+class CardCollection(collections.deque):
+ def __init__(self, collection):
+ for card in collection:
+ self.append(card)
+
+ def draw(self, index):
+ result = self[index]
+ del(self[index])
+ return result
+
+ def draw_from_top(self):
+ return self.pop()
+
+ def draw_from_bottom(self):
+ return self.popleft()
+
+ def top_card(self):
+ return self[len(self) - 1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def add(self, card):
+ self.append(card)
+
+ def index(self, card):
+ for i in range(0, len(self)):
+ if self[i] == card:
+ return i
+ raise ValueError
+
+ def sort_cards(self):
+ self = sorted(self, key = cmp_to_key(lambda x, y:
+ x.priority - y.priority))

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

import collections
from functools import cmp_to_key
RANK_SYMBOLS_PRIORITIES = [('A', 'Ace', 1), ('K', 'King', 13),
('Q', 'Queen', 12), ('J', 'Jack', 11),
('10', 'Ten', 10), ('9', 'Nine', 9),
('8', 'Eight', 8), ('7', 'Seven', 7),
('6', 'Six', 6), ('5', 'Five', 5),
('4', 'Four', 4), ('3', 'Three', 3),
('2', 'Two', 2)]
SUIT_COLORS_PRIORITIES = [('Diamonds', 'red', 39), ('Clubs', 'black', 26),
('Hearts', 'red', 13), ('Spades', 'black', 0)]
RANKS = {}
SUITS = {}
class Rank():
- def __init__(self, symbol):
- self.symbol = symbol
+ symbol = "S"
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
for rank in RANK_SYMBOLS_PRIORITIES:
RANKS[rank[1]] = type(rank[1], (Rank, ), {'symbol': rank[0],
'priority': rank[2]})
class Suit():
- def __init__(self, color):
- self.color = color
+ color = 'color'
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return self.__class__.__name__
for suit in SUIT_COLORS_PRIORITIES:
- SUITS[suit[0]] = type(suit[0], (Rank, ), {'color': suit[1],
+ SUITS[suit[0]] = type(suit[0], (Suit, ), {'color': suit[1],
'priority': suit[2]})
class Card():
def __init__(self, rank, suit):
- self._rank = rank(rank.symbol)
- self._suit = suit(suit.color)
+ self.rank = rank()
+ self.suit = suit()
@property
- def rank(self):
- return self._rank
-
- @property
- def suit(self):
- return self._suit
-
- @property
def priority(self):
- return self._rank.priority + self._suit.priority
+ return self.rank.priority + self.suit.priority
def __str__(self):
- return "{} of {}".format(self.rank.__name__, self.suit.__name__)
+ return "{} of {}".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(collections.deque):
def __init__(self, collection):
for card in collection:
self.append(card)
def draw(self, index):
result = self[index]
del(self[index])
return result
def draw_from_top(self):
return self.pop()
def draw_from_bottom(self):
return self.popleft()
def top_card(self):
return self[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.append(card)
def index(self, card):
for i in range(0, len(self)):
if self[i] == card:
return i
raise ValueError
- def sort_cards(self):
- self = sorted(self, key = cmp_to_key(lambda x, y:
+
- x.priority - y.priority))
+def generate_deck(game_name=None):
+ result = []
+ for suit in SUITS:
+ for rank in RANKS:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ result.sort(key=cmp_to_key(lambda x, y: y.priority - x.priority))
+
+ if game_name == "Belote":
+ unwanted_cards = []
+ for card in result:
+ if 1 < (card.priority % 13) < 7:
+ unwanted_cards.append(card)
+ result = [card for card in result if not card in unwanted_cards]
+
+ if game_name == "SixtySix":
+ unwanted_cards = []
+ for card in result:
+ if 1 < (card.priority % 13) < 9:
+ unwanted_cards.append(card)
+ result = [card for card in result if not card in unwanted_cards]
+
+ return result
+
+
+def StandardDeck():
+ return CardCollection(generate_deck())
+
+
+def BeloteDeck():
+ return CardCollection(generate_deck("Belote"))
+
+
+def SixtySixDeck():
+ return CardCollection(generate_deck("SixtySix"))