Решение на Тесте карти от Драгомир Тунчев

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

Към профила на Драгомир Тунчев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 16 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Rank:
def __init__(self):
pass
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
self.symbol = '2'
class Three(Rank):
def __init__(self):
self.symbol = '3'
class Four(Rank):
def __init__(self):
self.symbol = '4'
class Five(Rank):
def __init__(self):
self.symbol = '5'
class Six(Rank):
def __init__(self):
self.symbol = '6'
class Seven(Rank):
def __init__(self):
self.symbol = '7'
class Eight(Rank):
def __init__(self):
self.symbol = '8'
class Nine(Rank):
def __init__(self):
self.symbol = '9'
class Ten(Rank):
def __init__(self):
self.symbol = '10'
class Jack(Rank):
def __init__(self):
self.symbol = 'J'
class Queen(Rank):
def __init__(self):
self.symbol = 'Q'
class King(Rank):
def __init__(self):
self.symbol = 'K'
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
class Suit:
def __init__(self):
pass
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Spades(Suit):
def __init__(self):
self.color = 'black'
class Hearts(Suit):
def __init__(self):
self.color = 'red'
class Diamonds(Suit):
def __init__(self):
self.color = 'red'
class Clubs(Suit):
def __init__(self):
self.color = 'black'
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}
SUITS = {'Spades': Spades, 'Hearts': Hearts,
'Diamonds': Diamonds, 'Clubs': Clubs}
class Card:
def __init__(self, rank, suit):
super(Card, self).__setattr__('rank', rank())
super(Card, self).__setattr__('suit', suit())
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __repr__(self):
return '<Card {0} of {1}>'.format(self.rank, self.suit)
def __str__(self):
return '{0} of {1}'.format(self.rank, self.suit)
def __setattr__(self, name, value):
raise AttributeError("can't set attribute")
def __delattr__(self, name):
raise AttributeError("can't delete attribute")
class CardCollection:
def __init__(self, collection=None):
if collection is not None:
self.deck = [card for card in collection]
else:
self.deck = []
def __getitem__(self, index):
return self.deck[index]
def __len__(self):
return len(self.deck)
def __repr__(self):
return '{}'.format([repr(card) for card in self.deck])
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop(-1)
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return (self.deck.index(card))
def MakeDeck(ranks):
deck = CardCollection()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ranks:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
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'])

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

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

OK

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

Драгомир обнови решението на 24.03.2014 21:46 (преди над 10 години)

+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__
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+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}
+
+
+SUITS = {'Spades': Spades, 'Hearts': Hearts,
+ 'Diamonds': Diamonds, 'Clubs': Clubs}
+
+
+class Card: # to make it immutable
+ 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 __repr__(self):
+ return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ self.deck = []
+ if collection is not None:
+ for card in collection:
+ self.deck.append(card)
+
+ def __getitem__(self, index):
+ return self.deck[index]
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __repr__(self):
+ return str([repr(card) for card in self.deck])
+
+ def draw(self, index):
+ card = self.deck[index]
+ del self.deck[index]
+ return card
+
+ def draw_from_top(self):
+ card = self.deck[-1]
+ del self.deck[-1]
+ return card
+
+ def draw_from_bottom(self):
+ card = self.deck[0]
+ del self.deck[0]
+ return card
+
+ def top_card(self):
+ return self.deck[-1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ return (self.deck.index(card))
+
+
+def StandardDeck():
+ deck = CardCollection()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def BeloteDeck():
+ deck = CardCollection()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def SixtySixDeck():
+ deck = CardCollection()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck

Драгомир обнови решението на 24.03.2014 22:10 (преди над 10 години)

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__
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
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}
SUITS = {'Spades': Spades, 'Hearts': Hearts,
'Diamonds': Diamonds, 'Clubs': Clubs}
-class Card: # to make it immutable
+class Card:
def __init__(self, rank, suit):
- self.rank = rank()
- self.suit = suit()
+ super(Card, self).__setattr__('rank', rank())
+ super(Card, self).__setattr__('suit', suit())
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __repr__(self):
return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
+
+ def __setattr__(self, name, value):
+ raise AttributeError("can't set attribute")
+
+ def __delattr__(self, name):
+ raise AttributeError("can't delete attribute")
class CardCollection:
def __init__(self, collection=None):
self.deck = []
if collection is not None:
for card in collection:
self.deck.append(card)
def __getitem__(self, index):
return self.deck[index]
def __len__(self):
return len(self.deck)
def __repr__(self):
return str([repr(card) for card in self.deck])
def draw(self, index):
card = self.deck[index]
del self.deck[index]
return card
def draw_from_top(self):
card = self.deck[-1]
del self.deck[-1]
return card
def draw_from_bottom(self):
card = self.deck[0]
del self.deck[0]
return card
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return (self.deck.index(card))
def StandardDeck():
deck = CardCollection()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
def BeloteDeck():
deck = CardCollection()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ['King', 'Queen', 'Jack', 'Ten',
'Nine', 'Eight', 'Seven', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
def SixtySixDeck():
deck = CardCollection()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
  • help(list.pop) !!
  • по-добрия вариант за symbol е да го направиш атрибут на съответните класове вместо да го подаваш на __init__, същото важи и за color
  • относно __str__ и __repr__ препоръчително е да ползваш str.format вместо изрично преобразуване до низ и конкатенация с +
  • можеш ли да го направиш с по-малко нива на влагане? Може би, с list comprehension:

    def __init__(self, collection=None):
        self.deck = []
        if collection is not None:
            for card in collection:
                self.deck.append(card)
    
  • в StandardDeck, BeloteDeck, SixtySixDeck имаш голямо количество повтарящ се код. Можеш ли да го намалиш чрез някаква адекватна форма на преизползване ?

Драгомир обнови решението на 25.03.2014 18:17 (преди над 10 години)

class Rank:
- def __init__(self, symbol):
- self.symbol = symbol
+ def __init__(self):
+ pass
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
- Rank.__init__(self, '2')
+ self.symbol = '2'
class Three(Rank):
def __init__(self):
- Rank.__init__(self, '3')
+ self.symbol = '3'
class Four(Rank):
def __init__(self):
- Rank.__init__(self, '4')
+ self.symbol = '4'
class Five(Rank):
def __init__(self):
- Rank.__init__(self, '5')
+ self.symbol = '5'
class Six(Rank):
def __init__(self):
- Rank.__init__(self, '6')
+ self.symbol = '6'
class Seven(Rank):
def __init__(self):
- Rank.__init__(self, '7')
+ self.symbol = '7'
class Eight(Rank):
def __init__(self):
- Rank.__init__(self, '8')
+ self.symbol = '8'
class Nine(Rank):
def __init__(self):
- Rank.__init__(self, '9')
+ self.symbol = '9'
class Ten(Rank):
def __init__(self):
- Rank.__init__(self, '10')
+ self.symbol = '10'
class Jack(Rank):
def __init__(self):
- Rank.__init__(self, 'J')
+ self.symbol = 'J'
class Queen(Rank):
def __init__(self):
- Rank.__init__(self, 'Q')
+ self.symbol = 'Q'
class King(Rank):
def __init__(self):
- Rank.__init__(self, 'K')
+ self.symbol = 'K'
class Ace(Rank):
def __init__(self):
- Rank.__init__(self, 'A')
+ self.symbol = 'A'
class Suit:
- def __init__(self, color):
- self.color = color
+ def __init__(self):
+ pass
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Spades(Suit):
def __init__(self):
- Suit.__init__(self, 'black')
+ self.color = 'black'
class Hearts(Suit):
def __init__(self):
- Suit.__init__(self, 'red')
+ self.color = 'red'
class Diamonds(Suit):
def __init__(self):
- Suit.__init__(self, 'red')
+ self.color = 'red'
class Clubs(Suit):
def __init__(self):
- Suit.__init__(self, 'black')
+ self.color = 'black'
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}
SUITS = {'Spades': Spades, 'Hearts': Hearts,
'Diamonds': Diamonds, 'Clubs': Clubs}
class Card:
def __init__(self, rank, suit):
super(Card, self).__setattr__('rank', rank())
super(Card, self).__setattr__('suit', suit())
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __repr__(self):
- return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
+ return '<Card {0} of {1}>'.format(self.rank, self.suit)
def __str__(self):
- return str(self.rank) + ' of ' + str(self.suit)
+ return '{0} of {1}'.format(self.rank, self.suit)
def __setattr__(self, name, value):
raise AttributeError("can't set attribute")
def __delattr__(self, name):
raise AttributeError("can't delete attribute")
class CardCollection:
def __init__(self, collection=None):
- self.deck = []
if collection is not None:
- for card in collection:
- self.deck.append(card)
+ self.deck = [card for card in collection]
+ else:
+ self.deck = []
def __getitem__(self, index):
return self.deck[index]
def __len__(self):
return len(self.deck)
def __repr__(self):
- return str([repr(card) for card in self.deck])
+ return '{}'.format([repr(card) for card in self.deck])
def draw(self, index):
- card = self.deck[index]
- del self.deck[index]
- return card
+ return self.deck.pop(index)
def draw_from_top(self):
- card = self.deck[-1]
- del self.deck[-1]
- return card
+ return self.deck.pop(-1)
def draw_from_bottom(self):
- card = self.deck[0]
- del self.deck[0]
- return card
+ return self.deck.pop(0)
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
return (self.deck.index(card))
-def StandardDeck():
+def MakeDeck(ranks):
deck = CardCollection()
for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
- for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
+ for rank in ranks:
deck.add(Card(RANKS[rank], SUITS[suit]))
return deck
+def StandardDeck():
+ return MakeDeck(['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Six',
+ 'Five', 'Four', 'Three', 'Two', 'Ace'])
+
+
def BeloteDeck():
- deck = CardCollection()
- for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
- for rank in ['King', 'Queen', 'Jack', 'Ten',
- 'Nine', 'Eight', 'Seven', 'Ace']:
- deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck
+ return MakeDeck(['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace'])
def SixtySixDeck():
- deck = CardCollection()
- for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ return MakeDeck(['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace'])
- for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']:
- deck.add(Card(RANKS[rank], SUITS[suit]))
- return deck