Решение на Тесте карти от Деян Камбуров

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

Към профила на Деян Камбуров

Резултати

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

Код

from itertools import product
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", "Rank": ""
}
COLORS = {
"Diamonds": "red", "Hearts": "red", "Clubs": "black", "Spades": "black",
"Suit": ""
}
ORDERED_RANKS = [
"King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
"Six", "Five", "Four", "Three", "Two", "Ace"
]
ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank:
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
def __str__(self):
return type(self).__name__
def __eq__(self, other):
return type(self) == type(other)
class Suit:
def __init__(self):
self.color = COLORS[self.__class__.__name__]
def __str__(self):
return type(self).__name__
def __eq__(self, other):
return type(self) == type(other)
for rank in ORDERED_RANKS:
exec("class %s(Rank): pass" % rank)
for suit in ORDERED_SUITS:
exec("class %s(Suit): pass" % suit)
class Card:
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 __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return "<" + str(self) + ">"
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = [element for element in collection]
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def __str__(self):
return str([str(element) for element in self.collection])
def __repr__(self):
return repr([repr(element) for element in self.collection])
def draw(self, index):
drawn_card = self.collection[index]
del self.collection[index]
return drawn_card
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(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)
RANKS = {rank.__name__: rank for rank in Rank.__subclasses__()}
SUITS = {suit.__name__: suit for suit in Suit.__subclasses__()}
def StandardDeck():
standard_deck = []
for suit, rank in product(ORDERED_SUITS, ORDERED_RANKS):
standard_deck.append(Card(RANKS[rank], SUITS[suit]))
return CardCollection(standard_deck)
def BeloteDeck():
return CardCollection(card for card in StandardDeck()
if card.rank.symbol not in '23456')
def SixtySixDeck():
return CardCollection(card for card in StandardDeck()
if card.rank.symbol not in '2345678')

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

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

OK

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

Деян обнови решението на 23.03.2014 14:31 (преди над 10 години)

+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __str__(self):
+ return type(self).__name__
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __str__(self):
+ return type(self).__name__
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+
+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 = '6'
+
+
+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 Clubs(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Card:
+ 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 __str__(self):
+ return str(self.rank) + " of " + str(self.suit)
+
+ def __repr__(self):
+ return "<" + str(self) + ">"
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = [element for element in collection]
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ return str([str(x) for x in self.collection])
+
+ def __repr__(self):
+ return repr([repr(x) for x in self.collection])
+
+ def draw(self, index):
+ drawn_card = self.collection[index]
+ self.collection.remove(drawn_card)
+ return drawn_card
+
+ def draw_from_top(self):
+ return self.draw(len(self.collection) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ 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):
+ return self.collection.index(card)
+
+
+RANKS = {rank.__name__: rank for rank in Rank.__subclasses__()}
+
+
+SUITS = {suit.__name__: suit for suit in Suit.__subclasses__()}
+
+
+def StandardDeck():
+ standart_deck_collection = [
+ Card(RANKS['King'], SUITS['Diamonds']),
+ Card(RANKS['Queen'], SUITS['Diamonds']),
+ Card(RANKS['Jack'], SUITS['Diamonds']),
+ Card(RANKS['Ten'], SUITS['Diamonds']),
+ Card(RANKS['Nine'], SUITS['Diamonds']),
+ Card(RANKS['Eight'], SUITS['Diamonds']),
+ Card(RANKS['Seven'], SUITS['Diamonds']),
+ Card(RANKS['Six'], SUITS['Diamonds']),
+ Card(RANKS['Five'], SUITS['Diamonds']),
+ Card(RANKS['Four'], SUITS['Diamonds']),
+ Card(RANKS['Three'], SUITS['Diamonds']),
+ Card(RANKS['Two'], SUITS['Diamonds']),
+ Card(RANKS['Ace'], SUITS['Diamonds']),
+ Card(RANKS['King'], SUITS['Clubs']),
+ Card(RANKS['Queen'], SUITS['Clubs']),
+ Card(RANKS['Jack'], SUITS['Clubs']),
+ Card(RANKS['Ten'], SUITS['Clubs']),
+ Card(RANKS['Nine'], SUITS['Clubs']),
+ Card(RANKS['Eight'], SUITS['Clubs']),
+ Card(RANKS['Seven'], SUITS['Clubs']),
+ Card(RANKS['Six'], SUITS['Clubs']),
+ Card(RANKS['Five'], SUITS['Clubs']),
+ Card(RANKS['Four'], SUITS['Clubs']),
+ Card(RANKS['Three'], SUITS['Clubs']),
+ Card(RANKS['Two'], SUITS['Clubs']),
+ Card(RANKS['Ace'], SUITS['Clubs']),
+ Card(RANKS['King'], SUITS['Hearts']),
+ Card(RANKS['Queen'], SUITS['Hearts']),
+ Card(RANKS['Jack'], SUITS['Hearts']),
+ Card(RANKS['Ten'], SUITS['Hearts']),
+ Card(RANKS['Nine'], SUITS['Hearts']),
+ Card(RANKS['Eight'], SUITS['Hearts']),
+ Card(RANKS['Seven'], SUITS['Hearts']),
+ Card(RANKS['Six'], SUITS['Hearts']),
+ Card(RANKS['Five'], SUITS['Hearts']),
+ Card(RANKS['Four'], SUITS['Hearts']),
+ Card(RANKS['Three'], SUITS['Hearts']),
+ Card(RANKS['Two'], SUITS['Hearts']),
+ Card(RANKS['Ace'], SUITS['Hearts']),
+ Card(RANKS['King'], SUITS['Spades']),
+ Card(RANKS['Queen'], SUITS['Spades']),
+ Card(RANKS['Jack'], SUITS['Spades']),
+ Card(RANKS['Ten'], SUITS['Spades']),
+ Card(RANKS['Nine'], SUITS['Spades']),
+ Card(RANKS['Eight'], SUITS['Spades']),
+ Card(RANKS['Seven'], SUITS['Spades']),
+ Card(RANKS['Six'], SUITS['Spades']),
+ Card(RANKS['Five'], SUITS['Spades']),
+ Card(RANKS['Four'], SUITS['Spades']),
+ Card(RANKS['Three'], SUITS['Spades']),
+ Card(RANKS['Two'], SUITS['Spades']),
+ Card(RANKS['Ace'], SUITS['Spades'])
+ ]
+ return CardCollection(standart_deck_collection)
+
+
+def BeloteDeck():
+ return CardCollection(x for x in StandardDeck()
+ if x.rank.symbol not in '23456')
+
+
+def SixtySixDeck():
+ return CardCollection(x for x in StandardDeck()
+ if x.rank.symbol not in '2345678')
  • Възползвай се от това, че наследяваш клас и не дефинирай __init__ във всеки наследник
  • StandardDeck, BeloteDeck и SixtySixDeck трябва да са класове, не функции
  • Обмисли как да направиш стандартно тесте, използвайки константите, които сме ви поискали

Благодаря за коментара, но не разбрах 2-рата точка. В условито е написано, че се очакват функции.

Очакваме да видим и 3 функции които генерират стандартните тестета за Покер, Белот и Сантасе: StandardDeck() BeloteDeck() SixtySixDeck()

Ако трябва да са класове, предполагам отново при извикването на StandartDeck(), трябва да се върне колекцията от карти?

HOLY CODE! Буквално се задавих като видях че си изписал 52 РЕДА с всички карти една под друга в StandardDeck.

Сериозно, в програмирането е OK да си мързелив!. Използвай цикъл! List comprehension! Разтрепериха ми се ръцете :D

Иначе Кирил очевидно не е чел условието. StandardDeck и приятелите му са функции, те просто трябва да връщат колекцията от карти.

Деян обнови решението на 25.03.2014 01:10 (преди над 10 години)

-class Rank:
- def __init__(self, symbol):
- self.symbol = symbol
+from itertools import product
- def __str__(self):
- return type(self).__name__
- def __eq__(self, other):
- return type(self) == type(other)
+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", "Rank": ""
+ }
-class Suit:
- def __init__(self, color):
- self.color = color
+COLORS = {
+ "Diamonds": "red", "Hearts": "red", "Clubs": "black", "Spades": "black",
+ "Suit": ""
+ }
- def __str__(self):
- return type(self).__name__
- def __eq__(self, other):
- return type(self) == type(other)
+ORDERED_RANKS = [
+ "King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
+ "Six", "Five", "Four", "Three", "Two", "Ace"
+ ]
-class Two(Rank):
- def __init__(self):
- self.symbol = '2'
+ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
-class Three(Rank):
+class Rank:
def __init__(self):
- self.symbol = '3'
+ self.symbol = SYMBOLS[self.__class__.__name__]
+ def __str__(self):
+ return type(self).__name__
-class Four(Rank):
- def __init__(self):
- self.symbol = '4'
+ def __eq__(self, other):
+ return type(self) == type(other)
-class Five(Rank):
+class Suit:
def __init__(self):
- self.symbol = '6'
+ self.color = COLORS[self.__class__.__name__]
+ def __str__(self):
+ return type(self).__name__
-class Six(Rank):
- def __init__(self):
- self.symbol = '6'
+ def __eq__(self, other):
+ return type(self) == type(other)
-class Seven(Rank):
- def __init__(self):
- self.symbol = '7'
+for rank in ORDERED_RANKS:
+ exec("class %s(Rank): pass" % rank)
-class Eight(Rank):
- def __init__(self):
- self.symbol = '8'
+for suit in ORDERED_SUITS:
+ exec("class %s(Suit): pass" % suit)
-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 Clubs(Suit):
- def __init__(self):
- self.color = 'black'
-
-
-class Diamonds(Suit):
- def __init__(self):
- self.color = 'red'
-
-
-class Hearts(Suit):
- def __init__(self):
- self.color = 'red'
-
-
-class Spades(Suit):
- def __init__(self):
- self.color = 'black'
-
-
class Card:
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 __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return "<" + str(self) + ">"
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = [element for element in collection]
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def __str__(self):
return str([str(x) for x in self.collection])
def __repr__(self):
return repr([repr(x) for x in self.collection])
def draw(self, index):
drawn_card = self.collection[index]
self.collection.remove(drawn_card)
return drawn_card
def draw_from_top(self):
return self.draw(len(self.collection) - 1)
def draw_from_bottom(self):
return self.draw(0)
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):
return self.collection.index(card)
RANKS = {rank.__name__: rank for rank in Rank.__subclasses__()}
SUITS = {suit.__name__: suit for suit in Suit.__subclasses__()}
def StandardDeck():
- standart_deck_collection = [
- Card(RANKS['King'], SUITS['Diamonds']),
- Card(RANKS['Queen'], SUITS['Diamonds']),
- Card(RANKS['Jack'], SUITS['Diamonds']),
- Card(RANKS['Ten'], SUITS['Diamonds']),
- Card(RANKS['Nine'], SUITS['Diamonds']),
- Card(RANKS['Eight'], SUITS['Diamonds']),
- Card(RANKS['Seven'], SUITS['Diamonds']),
- Card(RANKS['Six'], SUITS['Diamonds']),
- Card(RANKS['Five'], SUITS['Diamonds']),
- Card(RANKS['Four'], SUITS['Diamonds']),
- Card(RANKS['Three'], SUITS['Diamonds']),
- Card(RANKS['Two'], SUITS['Diamonds']),
- Card(RANKS['Ace'], SUITS['Diamonds']),
- Card(RANKS['King'], SUITS['Clubs']),
- Card(RANKS['Queen'], SUITS['Clubs']),
- Card(RANKS['Jack'], SUITS['Clubs']),
- Card(RANKS['Ten'], SUITS['Clubs']),
- Card(RANKS['Nine'], SUITS['Clubs']),
- Card(RANKS['Eight'], SUITS['Clubs']),
- Card(RANKS['Seven'], SUITS['Clubs']),
- Card(RANKS['Six'], SUITS['Clubs']),
- Card(RANKS['Five'], SUITS['Clubs']),
- Card(RANKS['Four'], SUITS['Clubs']),
- Card(RANKS['Three'], SUITS['Clubs']),
- Card(RANKS['Two'], SUITS['Clubs']),
- Card(RANKS['Ace'], SUITS['Clubs']),
- Card(RANKS['King'], SUITS['Hearts']),
- Card(RANKS['Queen'], SUITS['Hearts']),
- Card(RANKS['Jack'], SUITS['Hearts']),
- Card(RANKS['Ten'], SUITS['Hearts']),
- Card(RANKS['Nine'], SUITS['Hearts']),
- Card(RANKS['Eight'], SUITS['Hearts']),
- Card(RANKS['Seven'], SUITS['Hearts']),
- Card(RANKS['Six'], SUITS['Hearts']),
- Card(RANKS['Five'], SUITS['Hearts']),
- Card(RANKS['Four'], SUITS['Hearts']),
- Card(RANKS['Three'], SUITS['Hearts']),
- Card(RANKS['Two'], SUITS['Hearts']),
- Card(RANKS['Ace'], SUITS['Hearts']),
- Card(RANKS['King'], SUITS['Spades']),
- Card(RANKS['Queen'], SUITS['Spades']),
- Card(RANKS['Jack'], SUITS['Spades']),
- Card(RANKS['Ten'], SUITS['Spades']),
- Card(RANKS['Nine'], SUITS['Spades']),
- Card(RANKS['Eight'], SUITS['Spades']),
- Card(RANKS['Seven'], SUITS['Spades']),
- Card(RANKS['Six'], SUITS['Spades']),
- Card(RANKS['Five'], SUITS['Spades']),
- Card(RANKS['Four'], SUITS['Spades']),
- Card(RANKS['Three'], SUITS['Spades']),
- Card(RANKS['Two'], SUITS['Spades']),
- Card(RANKS['Ace'], SUITS['Spades'])
- ]
- return CardCollection(standart_deck_collection)
+ standard_deck = []
+ for x in product(ORDERED_SUITS, ORDERED_RANKS):
+ standard_deck.append(Card(RANKS[x[1]], SUITS[x[0]]))
+ return CardCollection(standard_deck)
def BeloteDeck():
return CardCollection(x for x in StandardDeck()
if x.rank.symbol not in '23456')
def SixtySixDeck():
return CardCollection(x for x in StandardDeck()
if x.rank.symbol not in '2345678')

Деян обнови решението на 26.03.2014 02:32 (преди над 10 години)

from itertools import product
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", "Rank": ""
}
COLORS = {
"Diamonds": "red", "Hearts": "red", "Clubs": "black", "Spades": "black",
"Suit": ""
}
ORDERED_RANKS = [
"King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
"Six", "Five", "Four", "Three", "Two", "Ace"
]
ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank:
def __init__(self):
self.symbol = SYMBOLS[self.__class__.__name__]
def __str__(self):
return type(self).__name__
def __eq__(self, other):
return type(self) == type(other)
class Suit:
def __init__(self):
self.color = COLORS[self.__class__.__name__]
def __str__(self):
return type(self).__name__
def __eq__(self, other):
return type(self) == type(other)
for rank in ORDERED_RANKS:
exec("class %s(Rank): pass" % rank)
for suit in ORDERED_SUITS:
exec("class %s(Suit): pass" % suit)
class Card:
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 __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return "<" + str(self) + ">"
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = [element for element in collection]
def __getitem__(self, i):
return self.collection[i]
def __len__(self):
return len(self.collection)
def __str__(self):
- return str([str(x) for x in self.collection])
+ return str([str(element) for element in self.collection])
def __repr__(self):
- return repr([repr(x) for x in self.collection])
+ return repr([repr(element) for element in self.collection])
def draw(self, index):
drawn_card = self.collection[index]
- self.collection.remove(drawn_card)
+ del self.collection[index]
return drawn_card
def draw_from_top(self):
- return self.draw(len(self.collection) - 1)
+ return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
- return self.collection[len(self.collection) - 1]
+ 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)
RANKS = {rank.__name__: rank for rank in Rank.__subclasses__()}
SUITS = {suit.__name__: suit for suit in Suit.__subclasses__()}
def StandardDeck():
standard_deck = []
- for x in product(ORDERED_SUITS, ORDERED_RANKS):
- standard_deck.append(Card(RANKS[x[1]], SUITS[x[0]]))
+ for suit, rank in product(ORDERED_SUITS, ORDERED_RANKS):
+ standard_deck.append(Card(RANKS[rank], SUITS[suit]))
return CardCollection(standard_deck)
def BeloteDeck():
- return CardCollection(x for x in StandardDeck()
- if x.rank.symbol not in '23456')
+ return CardCollection(card for card in StandardDeck()
+ if card.rank.symbol not in '23456')
def SixtySixDeck():
- return CardCollection(x for x in StandardDeck()
- if x.rank.symbol not in '2345678')
+ return CardCollection(card for card in StandardDeck()
+ if card.rank.symbol not in '2345678')