Решение на Тесте карти от Димитър Трендафилов

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

Към профила на Димитър Трендафилов

Резултати

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

Код

from collections import OrderedDict
SYMBOLS = OrderedDict([('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')])
COLORS = OrderedDict([('Diamonds', 'black'), ('Clubs', 'red'),
('Hearts', 'red'), ('Spades', 'black')])
class Rank:
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self):
self.color = COLORS[self.__class__.__name__]
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
RANKS = {key: type(key, (Rank,), {}) for key in SYMBOLS.keys()}
SUITS = {key: type(key, (Suit,), {}) for key in COLORS.keys()}
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __getattr__(self, name):
return self.name
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return self.rank.__class__.__name__ + \
' of ' + self.suit.__class__.__name__
def __repr__(self):
return '<class ' + self.rank.__class__.__name__ + \
' of ' + self.suit.__class__.__name__ + '>'
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
if len(self.cards) == 0:
raise IndexError
return self.cards.pop(index)
def draw_from_top(self):
if len(self.cards) == 0:
raise IndexError
return self.cards.pop()
def draw_from_bottom(self):
if len(self.cards) == 0:
raise IndexError
return self.cards.pop(0)
def bottom_card(self):
return self.cards[0]
def top_card(self):
return self.cards[len(self.cards) - 1]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def __iter__(self):
return iter(self.cards)
def StandardDeck():
return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in
COLORS.keys() for rank in SYMBOLS.keys()])
def BeloteDeck():
return CardCollection([
Card(RANKS[rank], SUITS[suit]) for suit in
COLORS.keys() for rank in list(SYMBOLS.keys())[0:7] + ['Ace']])
def SixtySixDeck():
return CardCollection([
Card(RANKS[rank], SUITS[suit]) for suit in
COLORS.keys() for rank in list(SYMBOLS.keys())[0:5] + ['Ace']])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.022s

OK

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

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

+#from collections import OrderedDict
+SYMBOLS = {'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'}
+COLORS = {'Diamonds': 'black', 'Hearts': 'red',
+ 'Spades': 'black', 'Clubs': 'red'}
+
+
+class Rank:
+
+ def __init__(self):
+ self.symbol = SYMBOLS[self.__class__.__name__]
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+
+ def __init__(self):
+ self.color = COLORS[self.__class__.__name__]
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ORDERED_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+RANKS = {key: type(key, (Rank,), {}) for key in ORDERED_RANKS}
+ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUITS = {key: type(key, (Suit,), {}) for key in ORDERED_SUITS}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+ def __getattr__(self, name):
+ return self.name
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<class ' + str(self) + '>'
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.cards = list(collection)
+
+ def draw(self, index):
+ return self.cards.pop(index)
+
+ def draw_from_top(self):
+ return self.cards.pop()
+
+ def draw_from_bottom(self):
+ return self.cards.pop(0)
+
+ def bottom_card(self):
+ return self.cards[0]
+
+ def top_card(self):
+ return self.cards[len(self.cards) - 1]
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ return self.cards.index(card)
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def __len__(self):
+ return len(self.cards)
+
+
+def StandardDeck():
+ return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS for rank in ORDERED_RANKS]
+
+
+def BeloteDeck():
+ return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS for rank in ORDERED_RANKS[0:7] + ['Ace']]
+
+
+def SixtySixDeck():
+ return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS for rank in ORDERED_RANKS[0:5] + ['Ace']]

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

-#from collections import OrderedDict
+from collections import OrderedDict
SYMBOLS = {'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'}
COLORS = {'Diamonds': 'black', 'Hearts': 'red',
'Spades': 'black', 'Clubs': 'red'}
class Rank:
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self):
self.color = COLORS[self.__class__.__name__]
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
ORDERED_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
RANKS = {key: type(key, (Rank,), {}) for key in ORDERED_RANKS}
ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
SUITS = {key: type(key, (Suit,), {}) for key in ORDERED_SUITS}
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __getattr__(self, name):
return self.name
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<class ' + str(self) + '>'
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
return self.cards.pop(index)
def draw_from_top(self):
return self.cards.pop()
def draw_from_bottom(self):
return self.cards.pop(0)
def bottom_card(self):
return self.cards[0]
def top_card(self):
return self.cards[len(self.cards) - 1]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def StandardDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS for rank in ORDERED_RANKS]
+ return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
+ for rank in ORDERED_RANKS]
def BeloteDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS for rank in ORDERED_RANKS[0:7] + ['Ace']]
+ return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
+ for rank in ORDERED_RANKS[0:7] + ['Ace']]
def SixtySixDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS for rank in ORDERED_RANKS[0:5] + ['Ace']]
+ return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
+ for rank in ORDERED_RANKS[0:5] + ['Ace']]

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

from collections import OrderedDict
-SYMBOLS = {'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'}
-COLORS = {'Diamonds': 'black', 'Hearts': 'red',
- 'Spades': 'black', 'Clubs': 'red'}
+SYMBOLS = OrderedDict([('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')])
+COLORS = OrderedDict([('Diamonds', 'black'), ('Hearts', 'red'),
+ ('Spades', 'black'), ('Clubs', 'red')])
class Rank:
-
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
-
def __eq__(self, other):
return self.symbol == other.symbol
-
def __str__(self):
return self.__class__.__name__
class Suit:
-
def __init__(self):
self.color = COLORS[self.__class__.__name__]
-
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
-
def __str__(self):
return self.__class__.__name__
-ORDERED_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
- 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
-RANKS = {key: type(key, (Rank,), {}) for key in ORDERED_RANKS}
-ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
-SUITS = {key: type(key, (Suit,), {}) for key in ORDERED_SUITS}
+RANKS = {key: type(key, (Rank,), {}) for key in SYMBOLS.keys()}
+SUITS = {key: type(key, (Suit,), {}) for key in COLORS.keys()}
class Card:
-
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
-
@property
def rank(self):
return self.__rank
-
@property
def suit(self):
return self.__suit
-
def __getattr__(self, name):
return self.name
-
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
-
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
-
def __repr__(self):
return '<class ' + str(self) + '>'
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
return self.cards.pop(index)
def draw_from_top(self):
return self.cards.pop()
def draw_from_bottom(self):
return self.cards.pop(0)
def bottom_card(self):
return self.cards[0]
def top_card(self):
return self.cards[len(self.cards) - 1]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def StandardDeck():
return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
- for rank in ORDERED_RANKS]
+ for rank in SYMBOLS.keys()]
def BeloteDeck():
return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
- for rank in ORDERED_RANKS[0:7] + ['Ace']]
+ for rank in list(SYMBOLS.keys())[0:7] + ['Ace']]
def SixtySixDeck():
return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
- for rank in ORDERED_RANKS[0:5] + ['Ace']]
+ for rank in list(SYMBOLS.keys())[0:5] + ['Ace']]

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

from collections import OrderedDict
SYMBOLS = OrderedDict([('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')])
COLORS = OrderedDict([('Diamonds', 'black'), ('Hearts', 'red'),
('Spades', 'black'), ('Clubs', 'red')])
class Rank:
+
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
+
def __eq__(self, other):
return self.symbol == other.symbol
+
def __str__(self):
return self.__class__.__name__
class Suit:
+
def __init__(self):
self.color = COLORS[self.__class__.__name__]
+
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
+
def __str__(self):
return self.__class__.__name__
RANKS = {key: type(key, (Rank,), {}) for key in SYMBOLS.keys()}
SUITS = {key: type(key, (Suit,), {}) for key in COLORS.keys()}
class Card:
+
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
+
@property
def rank(self):
return self.__rank
+
@property
def suit(self):
return self.__suit
+
def __getattr__(self, name):
return self.name
+
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
+
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
+
def __repr__(self):
return '<class ' + str(self) + '>'
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
return self.cards.pop(index)
def draw_from_top(self):
return self.cards.pop()
def draw_from_bottom(self):
return self.cards.pop(0)
def bottom_card(self):
return self.cards[0]
def top_card(self):
return self.cards[len(self.cards) - 1]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def StandardDeck():
return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
for rank in SYMBOLS.keys()]
def BeloteDeck():
return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
for rank in list(SYMBOLS.keys())[0:7] + ['Ace']]
def SixtySixDeck():
return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
for rank in list(SYMBOLS.keys())[0:5] + ['Ace']]

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

from collections import OrderedDict
SYMBOLS = OrderedDict([('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')])
-COLORS = OrderedDict([('Diamonds', 'black'), ('Hearts', 'red'),
- ('Spades', 'black'), ('Clubs', 'red')])
+COLORS = OrderedDict([('Diamonds', 'black'), ('Clubs', 'red'),
+ ('Hearts', 'red'),('Spades', 'black')])
class Rank:
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self):
self.color = COLORS[self.__class__.__name__]
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
RANKS = {key: type(key, (Rank,), {}) for key in SYMBOLS.keys()}
SUITS = {key: type(key, (Suit,), {}) for key in COLORS.keys()}
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __getattr__(self, name):
return self.name
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __repr__(self):
return '<class ' + str(self) + '>'
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
return self.cards.pop(index)
def draw_from_top(self):
return self.cards.pop()
def draw_from_bottom(self):
return self.cards.pop(0)
def bottom_card(self):
return self.cards[0]
def top_card(self):
return self.cards[len(self.cards) - 1]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
def StandardDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
+ return [Card(RANKS[rank], SUITS[suit]) for suit in COLORS.keys()
for rank in SYMBOLS.keys()]
def BeloteDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
+ return [Card(RANKS[rank], SUITS[suit]) for suit in COLORS.keys()
for rank in list(SYMBOLS.keys())[0:7] + ['Ace']]
def SixtySixDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in ORDERED_SUITS
+ return [Card(RANKS[rank], SUITS[suit]) for suit in COLORS.keys()
for rank in list(SYMBOLS.keys())[0:5] + ['Ace']]

Решението ти ми харесва. Но: нямаш интервал между импорта и първия клас, също така __repr__ метода не ми изглежда напълно ок, кастване на обект към стринг не е най-добрата опция в такива случаи. Използвай __class__.__name__ експлицитно за да дефинираш репрезентацията на обекта.

Димитър обнови решението на 26.03.2014 16:08 (преди над 10 години)

from collections import OrderedDict
+
SYMBOLS = OrderedDict([('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')])
-COLORS = OrderedDict([('Diamonds', 'black'), ('Clubs', 'red'),
- ('Hearts', 'red'),('Spades', 'black')])
+COLORS = OrderedDict([('Diamonds', 'black'), ('Clubs', 'red'),
+ ('Hearts', 'red'), ('Spades', 'black')])
class Rank:
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self):
self.color = COLORS[self.__class__.__name__]
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
RANKS = {key: type(key, (Rank,), {}) for key in SYMBOLS.keys()}
SUITS = {key: type(key, (Suit,), {}) for key in COLORS.keys()}
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __getattr__(self, name):
return self.name
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
- return str(self.rank) + ' of ' + str(self.suit)
+ return self.rank.__class__.__name__ + \
+ ' of ' + self.suit.__class__.__name__
def __repr__(self):
- return '<class ' + str(self) + '>'
+ return '<class ' + self.rank.__class__.__name__ + \
+ ' of ' + self.suit.__class__.__name__ + '>'
class CardCollection:
def __init__(self, collection=[]):
self.cards = list(collection)
def draw(self, index):
+ if len(self.cards) == 0:
+ raise IndexError
return self.cards.pop(index)
def draw_from_top(self):
+ if len(self.cards) == 0:
+ raise IndexError
return self.cards.pop()
def draw_from_bottom(self):
+ if len(self.cards) == 0:
+ raise IndexError
return self.cards.pop(0)
def bottom_card(self):
- return self.cards[0]
+ return self.cards[0]
def top_card(self):
return self.cards[len(self.cards) - 1]
def add(self, card):
self.cards.append(card)
def index(self, card):
return self.cards.index(card)
def __getitem__(self, index):
return self.cards[index]
def __len__(self):
return len(self.cards)
+ def __iter__(self):
+ return iter(self.cards)
+
def StandardDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in COLORS.keys()
- for rank in SYMBOLS.keys()]
+ return CardCollection([Card(RANKS[rank], SUITS[suit]) for suit in
+ COLORS.keys() for rank in SYMBOLS.keys()])
def BeloteDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in COLORS.keys()
- for rank in list(SYMBOLS.keys())[0:7] + ['Ace']]
+ return CardCollection([
+ Card(RANKS[rank], SUITS[suit]) for suit in
+ COLORS.keys() for rank in list(SYMBOLS.keys())[0:7] + ['Ace']])
def SixtySixDeck():
- return [Card(RANKS[rank], SUITS[suit]) for suit in COLORS.keys()
- for rank in list(SYMBOLS.keys())[0:5] + ['Ace']]
+ return CardCollection([
+ Card(RANKS[rank], SUITS[suit]) for suit in
+ COLORS.keys() for rank in list(SYMBOLS.keys())[0:5] + ['Ace']])