Решение на Тесте карти от Емилиан Станков

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

Към профила на Емилиан Станков

Резултати

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

Код

from collections import OrderedDict
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
if self.symbol == other.symbol:
return True
else:
return False
def __str__(self):
return str(self.rank)
class Suit:
def __init__(self, suit):
self.suit = suit
if self.suit == 'Diamonds' or self.suit == 'Hearts':
self.color = 'red'
elif self.suit == 'Spades' or self.suit == 'Clubs':
self.color = 'black'
def __eq__(self, other):
self.suit
other.suit = self.suit
if self.suit == other.suit:
return True
else:
return False
def __str__(self):
return str(self.suit)
class Ace(Rank):
def __init__(self):
Rank.symbol = 'A'
self.rank = 'Ace'
class King(Rank):
def __init__(self):
Rank.symbol = 'K'
self.rank = 'King'
class Queen(Rank):
def __init__(self):
Rank.symbol = 'Q'
self.rank = 'Queen'
class Jack(Rank):
def __init__(self):
Rank.symbol = 'J'
self.rank = 'Jack'
class Ten(Rank):
def __init__(self):
Rank.symbol = '10'
self.rank = 'Ten'
class Nine(Rank):
def __init__(self):
Rank.symbol = '9'
self.rank = 'Nine'
class Eight(Rank):
def __init__(self):
Rank.symbol = '8'
self.rank = 'Eight'
class Seven(Rank):
def __init__(self):
Rank.symbol = '7'
self.rank = 'Seven'
class Six(Rank):
def __init__(self):
Rank.symbol = '6'
self.rank = 'Six'
class Five(Rank):
def __init__(self):
Rank.symbol = '5'
self.rank = 'Five'
class Four(Rank):
def __init__(self):
Rank.symbol = '4'
self.rank = 'Four'
class Three(Rank):
def __init__(self):
Rank.symbol = '3'
self.rank = 'Three'
class Two(Rank):
def __init__(self):
Rank.symbol = '2'
self.rank = 'Two'
class Diamonds(Suit):
def __init__(self):
self.suit = 'Diamonds'
self.color = 'red'
def __key__(self):
return self.suit
class Clubs(Suit):
def __init__(self):
self.suit = 'Clubs'
self.color = 'black'
def __key__(self):
return self.suit
class Hearts(Suit):
def __init__(self):
self.suit = 'Hearts'
self.color = 'red'
def __key__(self):
return self.suit
class Spades(Suit):
def __init__(self):
self.suit = 'Spades'
self.color = 'black'
def __key__(self):
return self.suit
RANKS = {'Ace': Ace,
'King': King,
'Queen': Queen,
'Jack': Jack,
'Ten': Ten,
'Nine': Nine,
'Eight': Eight,
'Seven': Seven,
'Six': Six,
'Five': Five,
'Four': Four,
'Three': Three,
'Two': Two}
RANKS_ORDERED = 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 = {'Diamonds': Diamonds,
'Hearts': Hearts,
'Spades': Spades,
'Clubs': Clubs}
SUITS_ORDERED = OrderedDict([('Diamonds', Diamonds),
('Clubs', Clubs),
('Hearts', Hearts),
('Spades', Spades)])
class Card:
__slots__ = ("rank", "suit")
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __eq__(self, other):
if self.rank == other.rank and self.suit == other.suit:
return True
else:
return False
def __lt__(self, other):
return self.__key__() < other.__key__()
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __setattr__(self, *args):
raise AttributeError('can\'t set attribute')
def __delattr__(self, *args):
raise AttributeError('can\'t delete attribute')
class CardCollection:
def __init__(self, collection=''):
self.collection = list(collection)
def __len__(self):
return len(self.collection)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
deleted = self.collection[index]
del self.collection[index]
return deleted
def draw_from_top(self):
deleted = self.collection[len(self.collection) - 1]
del self.collection[len(self.collection) - 1]
return deleted
def draw_from_bottom(self):
deleted = self.collection[0]
del self.collection[0]
return deleted
def top_card(self):
return self.collection[len(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 self.collection:
return self.collection.index(card)
else:
raise ValueError('<Card {}> is not in list'.format(card))
def StandardDeck():
deck = CardCollection([Card(rank, suit)
for suit in SUITS_ORDERED.values()
for rank in RANKS_ORDERED.values()])
return deck.collection
def BeloteDeck():
allowed_ranks = (list(RANKS_ORDERED.values())[0:7] +
list(RANKS_ORDERED.values())[12:])
deck = CardCollection([Card(rank, suit)
for suit in SUITS_ORDERED.values()
for rank in allowed_ranks])
return deck.collection
def SixtySixDeck():
allowed_ranks = (list(RANKS_ORDERED.values())[0:5] +
list(RANKS_ORDERED.values())[12:])
deck = CardCollection([Card(rank, suit)
for suit in SUITS_ORDERED.values()
for rank in allowed_ranks])
return deck.collection

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

.E.E.E......F...
======================================================================
ERROR: test_deck_add (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_deck_index (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_deck_order (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
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-111a9sk/test.py", line 74, in test_card_equals
    self.assertNotEqual(aos1, solution.Card(solution.RANKS["Ace"], solution.SUITS["Hearts"]))
AssertionError: <Card Ace of Spades> == <Card Ace of Spades>

----------------------------------------------------------------------
Ran 16 tests in 6.544s

FAILED (failures=1, errors=3)

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

Емилиан обнови решението на 24.03.2014 00:30 (преди над 10 години)

+from collections import OrderedDict
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ if self.symbol == other.symbol:
+ return True
+ else:
+ return False
+
+ def __str__(self):
+ return str(self.rank)
+
+
+class Suit:
+ def __init__(self, suit):
+ self.suit = suit
+ if self.suit == 'Diamonds':
+ self.color = 'red'
+ elif self.suit == 'Hearts':
+ self.color = 'red'
+ elif self.suit == 'Spades':
+ self.color = 'black'
+ elif self.suit == 'Clubs':
+ self.color = 'black'
+
+ def __eq__(self, other):
+ self.suit
+ other.suit = self.suit
+ if self.suit == other.suit:
+ return True
+ else:
+ return False
+
+ def __str__(self):
+ return str(self.suit)
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.symbol = 'A'
+ self.rank = 'Ace'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.symbol = 'K'
+ self.rank = 'King'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.symbol = 'Q'
+ self.rank = 'Queen'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.symbol = 'J'
+ self.rank = 'Jack'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.symbol = '10'
+ self.rank = 'Ten'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.symbol = '9'
+ self.rank = 'Nine'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.symbol = '8'
+ self.rank = 'Eight'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.symbol = '7'
+ self.rank = 'Seven'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.symbol = '6'
+ self.rank = 'Six'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.symbol = '5'
+ self.rank = 'Five'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.symbol = '4'
+ self.rank = 'Four'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.symbol = '3'
+ self.rank = 'Three'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.symbol = '2'
+ self.rank = 'Two'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.suit = 'Diamonds'
+ self.color = 'red'
+
+ def __key__(self):
+ return self.suit
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.suit = 'Clubs'
+ self.color = 'black'
+
+ def __key__(self):
+ return self.suit
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.suit = 'Hearts'
+ self.color = 'red'
+
+ def __key__(self):
+ return self.suit
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.suit = 'Spades'
+ self.color = 'black'
+
+ def __key__(self):
+ return self.suit
+
+
+RANKS = {'Ace': Ace,
+ 'King': King,
+ 'Queen': Queen,
+ 'Jack': Jack,
+ 'Ten': Ten,
+ 'Nine': Nine,
+ 'Eight': Eight,
+ 'Seven': Seven,
+ 'Six': Six,
+ 'Five': Five,
+ 'Four': Four,
+ 'Three': Three,
+ 'Two': Two}
+
+RANKS_ORDERED = OrderedDict()
+RANKS_ORDERED['King'] = King
+RANKS_ORDERED['Queen'] = Queen
+RANKS_ORDERED['Jack'] = Jack
+RANKS_ORDERED['Ten'] = Ten
+RANKS_ORDERED['Nine'] = Nine
+RANKS_ORDERED['Eight'] = Eight
+RANKS_ORDERED['Seven'] = Seven
+RANKS_ORDERED['Six'] = Six
+RANKS_ORDERED['Five'] = Five
+RANKS_ORDERED['Four'] = Four
+RANKS_ORDERED['Three'] = Three
+RANKS_ORDERED['Two'] = Two
+RANKS_ORDERED['Ace'] = Ace
+
+SUITS = {'Diamonds': Diamonds,
+ 'Hearts': Hearts,
+ 'Spades': Spades,
+ 'Clubs': Clubs}
+
+SUITS_ORDERED = OrderedDict()
+SUITS_ORDERED['Diamonds'] = Diamonds
+SUITS_ORDERED['Clubs'] = Clubs
+SUITS_ORDERED['Hearts'] = Hearts
+SUITS_ORDERED['Spades'] = Spades
+
+
+class Card:
+ __slots__ = ("rank", "suit")
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, "rank", rank())
+ object.__setattr__(self, "suit", suit())
+
+ def __eq__(self, other):
+ if self.rank == other.rank and self.suit == other.suit:
+ return True
+ else:
+ return False
+
+ def __lt__(self, other):
+ return self.__key__() < other.__key__()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __setattr__(self, *args):
+ raise AttributeError('can\'t set attribute')
+
+ def __delattr__(self, *args):
+ raise AttributeError('can\'t delete attribute')
+
+
+class CardCollection:
+ def __init__(self, collection=''):
+ self.collection = list(collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def draw(self, index):
+ deleted = self.collection[index]
+ del self.collection[index]
+ return deleted
+
+ def draw_from_top(self):
+ deleted = self.collection[len(self.collection) - 1]
+ del self.collection[len(self.collection) - 1]
+ return deleted
+
+ def draw_from_bottom(self):
+ deleted = self.collection[0]
+ del self.collection[0]
+ return deleted
+
+ def top_card(self):
+ return self.collection[len(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 self.collection:
+ return self.collection.index(card)
+ else:
+ raise ValueError('<Card {}> is not in list'.format(card))
+
+
+def StandardDeck():
+ deck = CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED.values()
+ for rank in RANKS_ORDERED.values()])
+
+ return deck.collection
+
+
+def BeloteDeck():
+ deck = CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED.values()
+ for rank in RANKS_ORDERED.values()
+ if rank != RANKS_ORDERED['Two']
+ and rank != RANKS_ORDERED['Three']
+ and rank != RANKS_ORDERED['Four']
+ and rank != RANKS_ORDERED['Five']
+ and rank != RANKS_ORDERED['Six']])
+
+ return deck.collection
+
+
+def SixtySixDeck():
+ deck = CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED.values()
+ for rank in RANKS_ORDERED.values()
+ if rank != RANKS_ORDERED['Two']
+ and rank != RANKS_ORDERED['Three']
+ and rank != RANKS_ORDERED['Four']
+ and rank != RANKS_ORDERED['Five']
+ and rank != RANKS_ORDERED['Six']
+ and rank != RANKS_ORDERED['Seven']
+ and rank != RANKS_ORDERED['Eight']])
+
+ return deck.collection

На тревожно много места имаш почти еднакви редове:

Например:

RANKS_ORDERED = OrderedDict()
RANKS_ORDERED['King'] = King
RANKS_ORDERED['Queen'] = Queen
RANKS_ORDERED['Jack'] = Jack
...

Това е изключително лоша практика която прави кода много тегав за разбиране защото е няколко екрана по-дълъг от колкото може да е. Горното можеш да замениш с:

RANKS_ORDERED = OrderedDict({"King": King, "Queen": Queen, ... })

като пренасяш на нов ред както е нужно за PEP8. Същото важи за редове 21-28, 280-286 и други. В програмирането не е лошо да си мързелив

Емилиан обнови решението на 24.03.2014 14:17 (преди над 10 години)

from collections import OrderedDict
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
if self.symbol == other.symbol:
return True
else:
return False
def __str__(self):
return str(self.rank)
class Suit:
def __init__(self, suit):
self.suit = suit
- if self.suit == 'Diamonds':
+ if self.suit == 'Diamonds' or self.suit == 'Hearts':
self.color = 'red'
- elif self.suit == 'Hearts':
- self.color = 'red'
- elif self.suit == 'Spades':
+ elif self.suit == 'Spades' or self.suit == 'Clubs':
self.color = 'black'
- elif self.suit == 'Clubs':
- self.color = 'black'
def __eq__(self, other):
self.suit
other.suit = self.suit
if self.suit == other.suit:
return True
else:
return False
def __str__(self):
return str(self.suit)
class Ace(Rank):
def __init__(self):
Rank.symbol = 'A'
self.rank = 'Ace'
class King(Rank):
def __init__(self):
Rank.symbol = 'K'
self.rank = 'King'
class Queen(Rank):
def __init__(self):
Rank.symbol = 'Q'
self.rank = 'Queen'
class Jack(Rank):
def __init__(self):
Rank.symbol = 'J'
self.rank = 'Jack'
class Ten(Rank):
def __init__(self):
Rank.symbol = '10'
self.rank = 'Ten'
class Nine(Rank):
def __init__(self):
Rank.symbol = '9'
self.rank = 'Nine'
class Eight(Rank):
def __init__(self):
Rank.symbol = '8'
self.rank = 'Eight'
class Seven(Rank):
def __init__(self):
Rank.symbol = '7'
self.rank = 'Seven'
class Six(Rank):
def __init__(self):
Rank.symbol = '6'
self.rank = 'Six'
class Five(Rank):
def __init__(self):
Rank.symbol = '5'
self.rank = 'Five'
class Four(Rank):
def __init__(self):
Rank.symbol = '4'
self.rank = 'Four'
class Three(Rank):
def __init__(self):
Rank.symbol = '3'
self.rank = 'Three'
class Two(Rank):
def __init__(self):
Rank.symbol = '2'
self.rank = 'Two'
class Diamonds(Suit):
def __init__(self):
self.suit = 'Diamonds'
self.color = 'red'
def __key__(self):
return self.suit
class Clubs(Suit):
def __init__(self):
self.suit = 'Clubs'
self.color = 'black'
def __key__(self):
return self.suit
class Hearts(Suit):
def __init__(self):
self.suit = 'Hearts'
self.color = 'red'
def __key__(self):
return self.suit
class Spades(Suit):
def __init__(self):
self.suit = 'Spades'
self.color = 'black'
def __key__(self):
return self.suit
RANKS = {'Ace': Ace,
'King': King,
'Queen': Queen,
'Jack': Jack,
'Ten': Ten,
'Nine': Nine,
'Eight': Eight,
'Seven': Seven,
'Six': Six,
'Five': Five,
'Four': Four,
'Three': Three,
'Two': Two}
-RANKS_ORDERED = OrderedDict()
-RANKS_ORDERED['King'] = King
-RANKS_ORDERED['Queen'] = Queen
-RANKS_ORDERED['Jack'] = Jack
-RANKS_ORDERED['Ten'] = Ten
-RANKS_ORDERED['Nine'] = Nine
-RANKS_ORDERED['Eight'] = Eight
-RANKS_ORDERED['Seven'] = Seven
-RANKS_ORDERED['Six'] = Six
-RANKS_ORDERED['Five'] = Five
-RANKS_ORDERED['Four'] = Four
-RANKS_ORDERED['Three'] = Three
-RANKS_ORDERED['Two'] = Two
-RANKS_ORDERED['Ace'] = Ace
+RANKS_ORDERED = 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 = {'Diamonds': Diamonds,
'Hearts': Hearts,
'Spades': Spades,
'Clubs': Clubs}
-SUITS_ORDERED = OrderedDict()
-SUITS_ORDERED['Diamonds'] = Diamonds
-SUITS_ORDERED['Clubs'] = Clubs
-SUITS_ORDERED['Hearts'] = Hearts
-SUITS_ORDERED['Spades'] = Spades
+SUITS_ORDERED = OrderedDict([('Diamonds', Diamonds),
+ ('Clubs', Clubs),
+ ('Hearts', Hearts),
+ ('Spades', Spades)])
class Card:
__slots__ = ("rank", "suit")
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __eq__(self, other):
if self.rank == other.rank and self.suit == other.suit:
return True
else:
return False
def __lt__(self, other):
return self.__key__() < other.__key__()
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<Card ' + str(self) + '>'
def __setattr__(self, *args):
raise AttributeError('can\'t set attribute')
def __delattr__(self, *args):
raise AttributeError('can\'t delete attribute')
class CardCollection:
def __init__(self, collection=''):
self.collection = list(collection)
def __len__(self):
return len(self.collection)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
deleted = self.collection[index]
del self.collection[index]
return deleted
def draw_from_top(self):
deleted = self.collection[len(self.collection) - 1]
del self.collection[len(self.collection) - 1]
return deleted
def draw_from_bottom(self):
deleted = self.collection[0]
del self.collection[0]
return deleted
def top_card(self):
return self.collection[len(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 self.collection:
return self.collection.index(card)
else:
raise ValueError('<Card {}> is not in list'.format(card))
def StandardDeck():
deck = CardCollection([Card(rank, suit)
for suit in SUITS_ORDERED.values()
for rank in RANKS_ORDERED.values()])
return deck.collection
def BeloteDeck():
+ allowed_ranks = (list(RANKS_ORDERED.values())[0:7] +
+ list(RANKS_ORDERED.values())[12:])
deck = CardCollection([Card(rank, suit)
for suit in SUITS_ORDERED.values()
- for rank in RANKS_ORDERED.values()
- if rank != RANKS_ORDERED['Two']
- and rank != RANKS_ORDERED['Three']
- and rank != RANKS_ORDERED['Four']
- and rank != RANKS_ORDERED['Five']
- and rank != RANKS_ORDERED['Six']])
+ for rank in allowed_ranks])
return deck.collection
def SixtySixDeck():
+ allowed_ranks = (list(RANKS_ORDERED.values())[0:5] +
+ list(RANKS_ORDERED.values())[12:])
deck = CardCollection([Card(rank, suit)
for suit in SUITS_ORDERED.values()
- for rank in RANKS_ORDERED.values()
- if rank != RANKS_ORDERED['Two']
- and rank != RANKS_ORDERED['Three']
- and rank != RANKS_ORDERED['Four']
- and rank != RANKS_ORDERED['Five']
- and rank != RANKS_ORDERED['Six']
- and rank != RANKS_ORDERED['Seven']
- and rank != RANKS_ORDERED['Eight']])
+ for rank in allowed_ranks])
return deck.collection