Решение на Тесте карти от Георги Стайков

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

Към профила на Георги Стайков

Резултати

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

Код

class Rank:
symbol = ''
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Two(Rank):
def __init__(self):
self.symbol = '2'
def __str__(self):
return 'Two'
class Three(Rank):
def __init__(self):
self.symbol = '3'
def __str__(self):
return 'Three'
class Four(Rank):
def __init__(self):
self.symbol = '4'
def __str__(self):
return 'Four'
class Five(Rank):
def __init__(self):
self.symbol = '5'
def __str__(self):
return 'Five'
class Six(Rank):
def __init__(self):
self.symbol = '6'
def __str__(self):
return 'Six'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
def __str__(self):
return 'Seven'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
def __str__(self):
return 'Eight'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
def __str__(self):
return 'Nine'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
def __str__(self):
return 'Ten'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
def __str__(self):
return 'Jack'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
def __str__(self):
return 'Queen'
class King(Rank):
def __init__(self):
self.symbol = 'K'
def __str__(self):
return 'King'
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
def __str__(self):
return 'Ace'
RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
class Suit:
color = ''
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
class Hearts(Suit):
def __init__(self):
self.color = 'red'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Hearts'
class Clubs(Suit):
def __init__(self):
self.color = 'black'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Clubs'
class Spades(Suit):
def __init__(self):
self.color = 'black'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Spades'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Diamonds'
SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
'Spades': Spades, 'Diamonds': Diamonds}
class Card:
rank = Rank('')
suit = Suit('')
def __init__(self, rank, suit):
super(Card, self).__setattr__("rank", rank())
super(Card, self).__setattr__("suit", suit())
def __setattr__(self, name, value):
# tuk se xvurlq ili AttributeError ili TypeError nz koe e po-pravilno
raise AttributeError("Can't set attribute")
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
tmp = self[index]
del self.collection[index]
return tmp
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[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
if card in self:
return card
raise ValueError(str(card) + 'is not in list')
def __len__(self):
return len(self.collection)
def StandardDeck():
return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'])
def BeloteDeck():
return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Ace'])
def SixtySixDeck():
return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'])
def MakeDeck(_RANKS):
_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
return [Card(RANKS[rank], SUITS[suit])
for suit in _SUITS for rank in _RANKS]

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

...E............
======================================================================
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-1hq3y54/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 642, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 632, in _baseAssertEqual
    if not first == second:
  File "/tmp/d20140407-19315-1hq3y54/solution.py", line 212, in __eq__
    return self.rank == other.rank and self.suit == other.suit
AttributeError: 'int' object has no attribute 'rank'

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

FAILED (errors=1)

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

Георги обнови решението на 23.03.2014 22:56 (преди над 10 години)

+class Rank:
+ symbol = ''
+
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __str__(self):
+ return 'Two'
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __str__(self):
+ return 'Three'
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __str__(self):
+ return 'Four'
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __str__(self):
+ return 'Five'
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __str__(self):
+ return 'Six'
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __str__(self):
+ return 'Seven'
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __str__(self):
+ return 'Eight'
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __str__(self):
+ return 'Nine'
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __str__(self):
+ return 'Ten'
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __str__(self):
+ return 'Jack'
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __str__(self):
+ return 'Queen'
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __str__(self):
+ return 'King'
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __str__(self):
+ return 'Ace'
+
+RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
+ 'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
+
+class Suit:
+ color = ''
+
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Hearts'
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Clubs'
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Spades'
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return 'Diamonds'
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades, 'Diamonds': Diamonds}
+
+class Card:
+ rank = Rank('')
+ suit = Suit('')
+
+ def __init__(self, rank, suit):
+ super(Card, self).__setattr__("rank", rank())
+ super(Card, self).__setattr__("suit", suit())
+
+ def __setattr__(self, name, value):
+ #tuk se xvurlq ili AttributeError ili TypeError nz koe e po-pravilno
+ raise AttributeError("Can't set attribute")
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<' + str(self.rank) + ' of ' + str(self.suit) + '>'
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+from collections import deque
+
+class CardCollection:
+ #collection = deque()
+
+ def __init__(self, collection=[]):
+ self.collection = list(collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def draw(self, index):
+ tmp = self[index]
+ del self.collection[index]
+ return tmp
+
+ 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[len(self)-1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ for i in range(0,len(self)):
+ if card == self[i]:
+ return i
+ raise ValueError(str(card) + 'is not in list')
+
+ def __len__(self):
+ return len(self.collection)
+
+
+
+def StandardDeck():
+ _RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+ _SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ result = []
+ for suit in _SUITS:
+ for rank in _RANKS:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def BeloteDeck():
+ _RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ _SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ result = []
+ for suit in _SUITS:
+ for rank in _RANKS:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def SixtySixDeck():
+ _RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ _SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ result = []
+ for suit in _SUITS:
+ for rank in _RANKS:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result

Георги обнови решението на 23.03.2014 23:01 (преди над 10 години)

class Rank:
symbol = ''
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
def __str__(self):
return 'Two'
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
def __str__(self):
return 'Three'
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
def __str__(self):
return 'Four'
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
def __str__(self):
return 'Five'
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
def __str__(self):
return 'Six'
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
def __str__(self):
return 'Seven'
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
def __str__(self):
return 'Eight'
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
def __str__(self):
return 'Nine'
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
def __str__(self):
return 'Ten'
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
def __str__(self):
return 'Jack'
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
def __str__(self):
return 'Queen'
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
def __str__(self):
return 'King'
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
def __str__(self):
return 'Ace'
RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
class Suit:
color = ''
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Hearts'
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Clubs'
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Spades'
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Diamonds'
-SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades, 'Diamonds': Diamonds}
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Spades': Spades, 'Diamonds': Diamonds}
class Card:
rank = Rank('')
suit = Suit('')
def __init__(self, rank, suit):
super(Card, self).__setattr__("rank", rank())
super(Card, self).__setattr__("suit", suit())
def __setattr__(self, name, value):
#tuk se xvurlq ili AttributeError ili TypeError nz koe e po-pravilno
raise AttributeError("Can't set attribute")
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
-
-from collections import deque
-
class CardCollection:
- #collection = deque()
def __init__(self, collection=[]):
self.collection = list(collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
tmp = self[index]
del self.collection[index]
return tmp
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[len(self)-1]
def bottom_card(self):
return self[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
for i in range(0,len(self)):
if card == self[i]:
return i
raise ValueError(str(card) + 'is not in list')
def __len__(self):
return len(self.collection)
def StandardDeck():
_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
result = []
for suit in _SUITS:
for rank in _RANKS:
result.append(Card(RANKS[rank], SUITS[suit]))
return result
def BeloteDeck():
_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
result = []
for suit in _SUITS:
for rank in _RANKS:
result.append(Card(RANKS[rank], SUITS[suit]))
return result
def SixtySixDeck():
_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
result = []
for suit in _SUITS:
for rank in _RANKS:
result.append(Card(RANKS[rank], SUITS[suit]))
return result

Това нещо с писането на 101 класа има ли как да стане по-кратко (по-елегантно) или като цяло идеята на задачата си е такава ?

П.П Не успях да присъствам на последната лекция лекцията и също така в нета не успях да намеря нищо подходящо

Георги обнови решението на 25.03.2014 19:38 (преди над 10 години)

class Rank:
symbol = ''
-
+
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
+
class Two(Rank):
+
def __init__(self):
Rank.__init__(self, '2')
def __str__(self):
return 'Two'
+
class Three(Rank):
+
def __init__(self):
Rank.__init__(self, '3')
def __str__(self):
return 'Three'
-
+
+
class Four(Rank):
+
def __init__(self):
Rank.__init__(self, '4')
def __str__(self):
return 'Four'
-
+
+
class Five(Rank):
+
def __init__(self):
Rank.__init__(self, '5')
def __str__(self):
return 'Five'
-
+
+
class Six(Rank):
+
def __init__(self):
Rank.__init__(self, '6')
def __str__(self):
return 'Six'
-
+
+
class Seven(Rank):
+
def __init__(self):
Rank.__init__(self, '7')
def __str__(self):
return 'Seven'
-
+
+
class Eight(Rank):
+
def __init__(self):
Rank.__init__(self, '8')
def __str__(self):
return 'Eight'
-
+
+
class Nine(Rank):
+
def __init__(self):
Rank.__init__(self, '9')
def __str__(self):
return 'Nine'
-
+
+
class Ten(Rank):
+
def __init__(self):
Rank.__init__(self, '10')
def __str__(self):
return 'Ten'
-
+
+
class Jack(Rank):
+
def __init__(self):
Rank.__init__(self, 'J')
def __str__(self):
return 'Jack'
-
+
+
class Queen(Rank):
+
def __init__(self):
Rank.__init__(self, 'Q')
def __str__(self):
return 'Queen'
-
+
+
class King(Rank):
+
def __init__(self):
Rank.__init__(self, 'K')
def __str__(self):
return 'King'
-
+
+
class Ace(Rank):
+
def __init__(self):
Rank.__init__(self, 'A')
def __str__(self):
return 'Ace'
RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
+
class Suit:
color = ''
-
+
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
+
class Hearts(Suit):
+
def __init__(self):
Suit.__init__(self, 'red')
def __eq__(self, other):
return str(self) == str(other)
-
+
def __str__(self):
return 'Hearts'
+
class Clubs(Suit):
+
def __init__(self):
Suit.__init__(self, 'black')
def __eq__(self, other):
return str(self) == str(other)
-
+
def __str__(self):
return 'Clubs'
-
+
+
class Spades(Suit):
+
def __init__(self):
Suit.__init__(self, 'black')
def __eq__(self, other):
return str(self) == str(other)
-
+
def __str__(self):
return 'Spades'
+
class Diamonds(Suit):
+
def __init__(self):
Suit.__init__(self, 'red')
def __eq__(self, other):
return str(self) == str(other)
-
+
def __str__(self):
return 'Diamonds'
SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
'Spades': Spades, 'Diamonds': Diamonds}
+
class Card:
rank = Rank('')
suit = Suit('')
-
+
def __init__(self, rank, suit):
super(Card, self).__setattr__("rank", rank())
super(Card, self).__setattr__("suit", suit())
def __setattr__(self, name, value):
- #tuk se xvurlq ili AttributeError ili TypeError nz koe e po-pravilno
+ # tuk se xvurlq ili AttributeError ili TypeError nz koe e po-pravilno
raise AttributeError("Can't set attribute")
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
+
class CardCollection:
-
+
def __init__(self, collection=[]):
self.collection = list(collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
tmp = self[index]
del self.collection[index]
return tmp
def draw_from_top(self):
- return self.draw(len(self)-1)
+ return self.draw(len(self) - 1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
- return self[len(self)-1]
+ return self[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
- for i in range(0,len(self)):
- if card == self[i]:
- return i
+ if card in self:
+ return card
raise ValueError(str(card) + 'is not in list')
-
+
def __len__(self):
return len(self.collection)
-
-
+
def StandardDeck():
- _RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
- 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
- _SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
- result = []
- for suit in _SUITS:
- for rank in _RANKS:
- result.append(Card(RANKS[rank], SUITS[suit]))
- return result
+ return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'])
+
def BeloteDeck():
- _RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
- _SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
- result = []
- for suit in _SUITS:
- for rank in _RANKS:
- result.append(Card(RANKS[rank], SUITS[suit]))
- return result
+ return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Ace'])
+
def SixtySixDeck():
- _RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'])
+
+
+def MakeDeck(_RANKS):
_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
- result = []
- for suit in _SUITS:
+ return [Card(RANKS[rank], SUITS[suit])
- for rank in _RANKS:
+ for suit in _SUITS for rank in _RANKS]
- result.append(Card(RANKS[rank], SUITS[suit]))
- return result

Името MakeDeck ок ли е ? Според стандартите кое е по-добре: да използвам новата функция MakeDeck или да си оставя решението по стария начин (също така пренаписах някои функции в CardCollection) ?

П.П Тъй като това с динамичното построяване на класовете нещо ми убягва (успях да го направя, но начина по които го правя не ми харесва), може ли да ми напишете как ще стане? (няма да го използвам в решението си просто ми е интересно как ще стане)

Георги обнови решението на 25.03.2014 21:47 (преди над 10 години)

class Rank:
symbol = ''
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Two(Rank):
def __init__(self):
- Rank.__init__(self, '2')
+ self.symbol = '2'
def __str__(self):
return 'Two'
class Three(Rank):
def __init__(self):
- Rank.__init__(self, '3')
+ self.symbol = '3'
def __str__(self):
return 'Three'
class Four(Rank):
def __init__(self):
- Rank.__init__(self, '4')
+ self.symbol = '4'
def __str__(self):
return 'Four'
class Five(Rank):
def __init__(self):
- Rank.__init__(self, '5')
+ self.symbol = '5'
def __str__(self):
return 'Five'
class Six(Rank):
def __init__(self):
- Rank.__init__(self, '6')
+ self.symbol = '6'
def __str__(self):
return 'Six'
class Seven(Rank):
def __init__(self):
- Rank.__init__(self, '7')
+ self.symbol = '7'
def __str__(self):
return 'Seven'
class Eight(Rank):
def __init__(self):
- Rank.__init__(self, '8')
+ self.symbol = '8'
def __str__(self):
return 'Eight'
class Nine(Rank):
def __init__(self):
- Rank.__init__(self, '9')
+ self.symbol = '9'
def __str__(self):
return 'Nine'
class Ten(Rank):
def __init__(self):
- Rank.__init__(self, '10')
+ self.symbol = '10'
def __str__(self):
return 'Ten'
class Jack(Rank):
def __init__(self):
- Rank.__init__(self, 'J')
+ self.symbol = 'J'
def __str__(self):
return 'Jack'
class Queen(Rank):
def __init__(self):
- Rank.__init__(self, 'Q')
+ self.symbol = 'Q'
def __str__(self):
return 'Queen'
class King(Rank):
def __init__(self):
- Rank.__init__(self, 'K')
+ self.symbol = 'K'
def __str__(self):
return 'King'
class Ace(Rank):
def __init__(self):
- Rank.__init__(self, 'A')
+ self.symbol = 'A'
def __str__(self):
return 'Ace'
RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
class Suit:
color = ''
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
class Hearts(Suit):
def __init__(self):
- Suit.__init__(self, 'red')
+ self.color = 'red'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Hearts'
class Clubs(Suit):
def __init__(self):
- Suit.__init__(self, 'black')
+ self.color = 'black'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Clubs'
class Spades(Suit):
def __init__(self):
- Suit.__init__(self, 'black')
+ self.color = 'black'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Spades'
class Diamonds(Suit):
def __init__(self):
- Suit.__init__(self, 'red')
+ self.color = 'red'
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return 'Diamonds'
SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
'Spades': Spades, 'Diamonds': Diamonds}
class Card:
rank = Rank('')
suit = Suit('')
def __init__(self, rank, suit):
super(Card, self).__setattr__("rank", rank())
super(Card, self).__setattr__("suit", suit())
def __setattr__(self, name, value):
# tuk se xvurlq ili AttributeError ili TypeError nz koe e po-pravilno
raise AttributeError("Can't set attribute")
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def __getitem__(self, index):
return self.collection[index]
def draw(self, index):
tmp = self[index]
del self.collection[index]
return tmp
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[len(self) - 1]
def bottom_card(self):
return self[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
if card in self:
return card
raise ValueError(str(card) + 'is not in list')
def __len__(self):
return len(self.collection)
def StandardDeck():
return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace'])
def BeloteDeck():
return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine',
'Eight', 'Seven', 'Ace'])
def SixtySixDeck():
return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'])
def MakeDeck(_RANKS):
_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
return [Card(RANKS[rank], SUITS[suit])
for suit in _SUITS for rank in _RANKS]

Името MakeDeck ок ли е ? Според стандартите кое е по-добре: да използвам новата функция MakeDeck или да си оставя решението по стария начин (също така пренаписах някои функции в CardCollection) ?

П.П Тъй като това с динамичното построяване на класовете нещо ми убягва (успях да го направя, но начина по които го правя не ми харесва), може ли да ми напишете как ще стане? (няма да го използвам в решението си просто ми е интересно как ще стане)