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

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

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

Резултати

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

Код

digit_to_int = {str(i): i for i in range(2,11)}
letter_to_int = {'J': 11, 'Q': 12, 'K': 13, 'A': 1}
rank_order = dict(
list(digit_to_int.items()) + list(letter_to_int.items())
)
suit_order = {'Diamonds': 4, 'Clubs': 3, 'Hearts': 2, 'Spades': 1}
class Rank():
def __init__(self, symbol):
if symbol in ['2','3','4','5','6','7','8','9','10','J','Q','K','A']:
self.symbol = symbol
def __eq__(self, other):
return type(other) is type(self) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
return rank_order[self.symbol] < rank_order[other.symbol]
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
super().__init__('2')
class Three(Rank):
def __init__(self):
super().__init__('3')
class Four(Rank):
def __init__(self):
super().__init__('4')
class Five(Rank):
def __init__(self):
super().__init__('5')
class Six(Rank):
def __init__(self):
super().__init__('6')
class Seven(Rank):
def __init__(self):
super().__init__('7')
class Eight(Rank):
def __init__(self):
super().__init__('8')
class Nine(Rank):
def __init__(self):
super().__init__('9')
class Ten(Rank):
def __init__(self):
super().__init__('10')
class Jack(Rank):
def __init__(self):
super().__init__('J')
class Queen(Rank):
def __init__(self):
super().__init__('Q')
class King(Rank):
def __init__(self):
super().__init__('K')
class Ace(Rank):
def __init__(self):
super().__init__('A')
class Suit():
def __init__(self, color):
if color in ['red', 'black']:
self.color = color
def __eq__(self, other):
return type(other) is type(self) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
return suit_order[str(self)] < suit_order[str(other)]
def __str__(self):
return self.__class__.__name__
class Clubs(Suit):
def __init__(self):
super().__init__('black')
class Diamonds(Suit):
def __init__(self):
super().__init__('red')
class Hearts(Suit):
def __init__(self):
super().__init__('red')
class Spades(Suit):
def __init__(self):
super().__init__('black')
class Card():
def __init__(self, rank, suit):
if str(rank()) in RANKS.keys() and str(suit()) in SUITS.keys():
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __eq__(self, other):
return type(other) is type(self) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
weaker_suit = self.suit < other.suit
weaker_rank = self.rank < other.rank and self.suit == other.suit
return weaker_suit or weaker_rank
def __str__(self):
return "%s of %s"%(str(self.__rank), str(self.__suit))
def __repr__(self):
return "<Card %s of %s>"%(str(self.rank), str(self.suit))
class CardCollection():
def __init__(self, collection=None):
if collection:
self.collection = list(collection)
else:
self.collection = []
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(-1)
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def __repr__(self):
return str(self.collection)
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 = {
'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts': Hearts, 'Spades': Spades
}
def StandardDeck():
coll = sorted(
[Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()],
reverse=True
)
return CardCollection(coll)
def BeloteDeck():
exclude = [Two, Three, Four, Five, Six]
coll = sorted(
[Card(rank, suit)
for rank in RANKS.values() for suit in SUITS.values()
if rank not in exclude],
reverse=True
)
return CardCollection(coll)
def SixtySixDeck():
exclude = [Two, Three, Four, Five, Six, Seven, Eight]
coll = sorted(
[Card(rank, suit)
for rank in RANKS.values() for suit in SUITS.values()
if rank not in exclude],
reverse=True
)
return CardCollection(coll)

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.037s

OK

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

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

+digit_to_int = {str(i): i for i in range(2,11)}
+letter_to_int = {'J': 11, 'Q': 12, 'K': 13, 'A': 1}
+rank_order = dict(
+ list(digit_to_int.items()) + list(letter_to_int.items())
+ )
+suit_order = {'Diamonds': 4, 'Clubs': 3, 'Hearts': 2, 'Spades': 1}
+
+
+
+class Rank(object):
+ def __init__(self, symbol):
+ if symbol in ['2','3','4','5','6','7','8','9','10','J','Q','K','A']:
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return type(other) is type(self) and self.__dict__ == other.__dict__
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __lt__(self, other):
+ return rank_order[self.symbol] < rank_order[other.symbol]
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Two(Rank):
+ def __init__(self):
+ super().__init__('2')
+
+
+class Three(Rank):
+ def __init__(self):
+ super().__init__('3')
+
+
+class Four(Rank):
+ def __init__(self):
+ super().__init__('4')
+
+
+class Five(Rank):
+ def __init__(self):
+ super().__init__('5')
+
+
+class Six(Rank):
+ def __init__(self):
+ super().__init__('6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ super().__init__('7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ super().__init__('8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ super().__init__('9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ super().__init__('10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ super().__init__('J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ super().__init__('Q')
+
+
+class King(Rank):
+ def __init__(self):
+ super().__init__('K')
+
+
+class Ace(Rank):
+ def __init__(self):
+ super().__init__('A')
+
+
+class Suit(object):
+ def __init__(self, color):
+ if color in ['red', 'black']:
+ self.color = color
+
+ def __eq__(self, other):
+ return type(other) is type(self) and self.__dict__ == other.__dict__
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __lt__(self, other):
+ return suit_order[str(self)] < suit_order[str(other)]
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Clubs(Suit):
+ def __init__(self):
+ super().__init__('black')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ super().__init__('red')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ super().__init__('red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ super().__init__('black')
+
+
+class Card(object):
+ def __init__(self, rank, suit):
+ if str(rank()) in RANKS.keys() and str(suit()) in SUITS.keys():
+ self.__rank = rank()
+ self.__suit = suit()
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+ def __eq__(self, other):
+ return type(other) is type(self) and self.__dict__ == other.__dict__
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __lt__(self, other):
+ weaker_suit = self.suit < other.suit
+ weaker_rank = self.rank < other.rank and self.suit == other.suit
+ return weaker_suit or weaker_rank
+
+ def __str__(self):
+ return "%s of %s"%(str(self.__rank), str(self.__suit))
+
+ def __repr__(self):
+ return "<Card %s of %s>"%(str(self.rank), str(self.suit))
+
+
+class CardCollection(object):
+ def __init__(self, collection=None):
+ if collection:
+ self.collection = list(collection)
+ else:
+ self.collection = []
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop(-1)
+
+ def draw_from_bottom(self):
+ return self.collection.pop(0)
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __repr__(self):
+ return str(self.collection)
+
+
+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 = {
+ 'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts': Hearts, 'Spades': Spades
+ }
+
+
+def StandartDeck():
+ coll = sorted(
+ [Card(rank, suit) for rank in RANKS.values()
+ for suit in SUITS.values()],
+ reverse=True
+ )
+ return CardCollection(coll)
+
+
+def BeloteDeck():
+ exclude = [Two, Three, Four, Five, Six]
+ coll = sorted(
+ [Card(rank, suit)
+ for rank in RANKS.values() for suit in SUITS.values()
+ if rank not in exclude],
+ reverse=True
+ )
+ return CardCollection(coll)
+
+
+def SixtySixDeck():
+ exclude = [Two, Three, Four, Five, Six, Seven, Eight]
+ coll = sorted(
+ [Card(rank, suit)
+ for rank in RANKS.values() for suit in SUITS.values()
+ if rank not in exclude],
+ reverse=True
+ )
+ return CardCollection(coll)

Имаш правописна грешка StandartDeck -> StandardDeck.

В python3 не е нужно да наследяваш object, той е базов клас по подразбиране. Също така виж какво става като се опиташ да достъпиш несъществуващ индекс в колекцията.

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

digit_to_int = {str(i): i for i in range(2,11)}
letter_to_int = {'J': 11, 'Q': 12, 'K': 13, 'A': 1}
rank_order = dict(
list(digit_to_int.items()) + list(letter_to_int.items())
)
suit_order = {'Diamonds': 4, 'Clubs': 3, 'Hearts': 2, 'Spades': 1}
-
-class Rank(object):
+class Rank():
def __init__(self, symbol):
- if symbol in ['2','3','4','5','6','7','8','9','10','J','Q','K','A']:
- self.symbol = symbol
+ if symbol in ['2','3','4','5','6','7','8','9','10','J','Q','K','A']:
+ self.symbol = symbol
def __eq__(self, other):
return type(other) is type(self) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
return rank_order[self.symbol] < rank_order[other.symbol]
def __str__(self):
return self.__class__.__name__
class Two(Rank):
def __init__(self):
super().__init__('2')
class Three(Rank):
def __init__(self):
super().__init__('3')
class Four(Rank):
def __init__(self):
super().__init__('4')
class Five(Rank):
def __init__(self):
super().__init__('5')
class Six(Rank):
def __init__(self):
super().__init__('6')
class Seven(Rank):
def __init__(self):
super().__init__('7')
class Eight(Rank):
def __init__(self):
super().__init__('8')
class Nine(Rank):
def __init__(self):
super().__init__('9')
class Ten(Rank):
def __init__(self):
super().__init__('10')
class Jack(Rank):
def __init__(self):
super().__init__('J')
class Queen(Rank):
def __init__(self):
super().__init__('Q')
class King(Rank):
def __init__(self):
super().__init__('K')
class Ace(Rank):
def __init__(self):
super().__init__('A')
-class Suit(object):
+class Suit():
def __init__(self, color):
if color in ['red', 'black']:
self.color = color
def __eq__(self, other):
return type(other) is type(self) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
return suit_order[str(self)] < suit_order[str(other)]
def __str__(self):
return self.__class__.__name__
class Clubs(Suit):
def __init__(self):
super().__init__('black')
class Diamonds(Suit):
def __init__(self):
super().__init__('red')
class Hearts(Suit):
def __init__(self):
super().__init__('red')
class Spades(Suit):
def __init__(self):
super().__init__('black')
-class Card(object):
+class Card():
def __init__(self, rank, suit):
if str(rank()) in RANKS.keys() and str(suit()) in SUITS.keys():
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __eq__(self, other):
return type(other) is type(self) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
weaker_suit = self.suit < other.suit
weaker_rank = self.rank < other.rank and self.suit == other.suit
return weaker_suit or weaker_rank
def __str__(self):
return "%s of %s"%(str(self.__rank), str(self.__suit))
def __repr__(self):
return "<Card %s of %s>"%(str(self.rank), str(self.suit))
-class CardCollection(object):
+class CardCollection():
def __init__(self, collection=None):
if collection:
self.collection = list(collection)
else:
self.collection = []
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop(-1)
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def add(self, card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def __repr__(self):
return str(self.collection)
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 = {
'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts': Hearts, 'Spades': Spades
}
-def StandartDeck():
+def StandardDeck():
coll = sorted(
[Card(rank, suit) for rank in RANKS.values()
for suit in SUITS.values()],
reverse=True
)
return CardCollection(coll)
def BeloteDeck():
exclude = [Two, Three, Four, Five, Six]
coll = sorted(
[Card(rank, suit)
for rank in RANKS.values() for suit in SUITS.values()
if rank not in exclude],
reverse=True
)
return CardCollection(coll)
def SixtySixDeck():
exclude = [Two, Three, Four, Five, Six, Seven, Eight]
coll = sorted(
[Card(rank, suit)
for rank in RANKS.values() for suit in SUITS.values()
if rank not in exclude],
reverse=True
)
return CardCollection(coll)

Здравей, премахнах object от дефинициите на класовете и си оправих името на функцията. Не разбрах какъв е проблемът с индексирането, тъй като при невалиден индекс програмата ми се държи съгласно условието - връща IndexError.