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

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

Към профила на Никола Димитров

Резултати

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

Код

# Mixins and stuff
class KnowsItsName:
@property
def classname(self):
return self.__class__.__name__
def __str__(self):
return self.classname
def __eq__(self, other):
return self.classname == other.classname
class Suit(KnowsItsName):
def __init__(self):
self.color = self.__class__.color
class Rank(KnowsItsName):
def __init__(self):
self.symbol = self.__class__.symbol
# Sorted!
RANK_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace', ]
RANK_SYMBOLS = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
SUIT_NAMES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
SUIT_COLORS = [ "red", "black" ]
def create_rank(rank, symbol):
return type(rank, (Rank, ), { "symbol": symbol})
def create_suit(suit, color):
return type(suit, (Suit, ), { "color": color})
RANKS = {rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)])
for rank in RANK_NAMES}
SUITS = {suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2])
for suit in SUIT_NAMES}
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 __str__(self):
return "{0} of {1}".format(self.rank.classname, self.suit.classname)
def __repr__(self):
return "<Card {0} >".format(str(self))
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __int__(self):
return SUIT_NAMES.index(self.suit.classname) * len(RANKS) + \
RANK_NAMES.index(self.rank.classname)
class CardCollection:
def __init__(self, collection=[]):
self.deck = list(collection)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[len(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 __getitem__(self, i):
return self.deck[i]
def __len__(self):
return len(self.deck)
def __repr__(self):
return repr(self.deck)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values() for suit in SUITS.values()]
sorted_deck = sorted(deck, key=int)
return sorted_deck
def rank_greater_than(card, rank):
if card.rank == RANKS["Ace"]():
return True
return RANK_NAMES.index(card.rank.classname) < RANK_NAMES.index(rank)
def BeloteDeck():
return CardCollection([card for card in StandardDeck()
if rank_greater_than(card, "Six")])
def SixtySixDeck():
return CardCollection([card for card in StandardDeck()
if rank_greater_than(card, "Eight")])

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.024s

OK

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

Никола обнови решението на 20.03.2014 14:07 (преди над 10 години)

+class Suit:
+ def __str__(self):
+ return self.color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+class Rank:
+ def __str__(self):
+ return self.symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+# Sorted!
+RANK_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace', ]
+RANK_SYMBOLS = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
+SUIT_NAMES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUIT_COLORS = [ "red", "black" ]
+
+# Go functional
+def create_constructor(attribute_name, attribute_value):
+ def constructor(self):
+ setattr(self, attribute_name, attribute_value)
+
+ return constructor;
+
+def create_rank(rank, symbol):
+ return type(rank, (Rank, ), { "__init__": create_constructor("symbol", symbol)})
+
+def create_suit(suit, color):
+ return type(suit, (Suit, ), { "__init__": create_constructor("color", color)})
+
+RANKS = { rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)]) for rank in RANK_NAMES }
+SUITS = { suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2]) for suit in SUIT_NAMES }
+
+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 __str__(self):
+ return "{0} of {1}".format(self.rank.__class__.__name__, self.suit.__class__.__name__)
+
+ def __repr__(self):
+ return "<Card {0} >".format(str(self))
+
+ def __eq__(self, other):
+ return (self.rank, self.suit) == (other.rank, other.suit)
+
+ def __int__(self):
+ return SUIT_NAMES.index(self.suit.__class__.__name__) * len(RANKS) + \
+ RANK_NAMES.index(self.rank.__class__.__name__)
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.deck = list(collection)
+
+ def draw(self, index):
+ return self.deck.pop(index)
+
+ def draw_from_top(self):
+ return self.deck.pop()
+
+ def draw_from_bottom(self):
+ return self.deck.pop(0)
+
+ def top_card(self):
+ return self.deck[len(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 __getitem__(self, i):
+ return self.deck[i]
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __repr__(self):
+ return repr(self.deck)
+
+
+def StandardDeck():
+ deck = [Card(rank, suit) for rank in RANKS.values() for suit in SUITS.values()]
+ sorted_deck = sorted(deck, key=lambda x: int(x))
+ return sorted_deck
+
+def rank_less_than(card, rank):
+ if card.rank == RANKS["Ace"]():
+ return False
+ return RANK_NAMES.index(card.rank.__class__.__name__) < RANK_NAMES.index(rank)
+
+def BeloteDeck():
+ return CardCollection([card for card in StandardDeck()
+ if rank_less_than(card, "Seven")])
+
+def SixtySixDeck():
+ return CardCollection([card for card in StandardDeck()
+ if rank_less_than(card, "Nine")])
+
+

Стандартните ти тестета са подредени в различен ред всеки път когато рестартираш програмата си :)

Освен това не е нужно да имаш конструктури на всяка от видовете карти и бои, можеш да ги направиш просто да имат различен атрибут и всякакви методи да са в базовите класове Rank и Suit.

Никола обнови решението на 21.03.2014 00:49 (преди над 10 години)

-class Suit:
+# Mixins and stuff
+class KnowsItsName:
+ @property
+ def classname(self):
+ return self.__class__.__name__
+
+class Suit(KnowsItsName):
+ def __init__(self):
+ self.color = self.__class__.color
+
def __str__(self):
return self.color
def __eq__(self, other):
return self.color == other.color
-class Rank:
+class Rank(KnowsItsName):
+ def __init__(self):
+ self.symbol = self.__class__.symbol
+
def __str__(self):
return self.symbol
def __eq__(self, other):
return self.symbol == other.symbol
# Sorted!
RANK_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace', ]
RANK_SYMBOLS = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
SUIT_NAMES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
SUIT_COLORS = [ "red", "black" ]
-
-# Go functional
-def create_constructor(attribute_name, attribute_value):
- def constructor(self):
- setattr(self, attribute_name, attribute_value)
-
- return constructor;
def create_rank(rank, symbol):
- return type(rank, (Rank, ), { "__init__": create_constructor("symbol", symbol)})
+ return type(rank, (Rank, ), { "symbol": symbol})
def create_suit(suit, color):
- return type(suit, (Suit, ), { "__init__": create_constructor("color", color)})
+ return type(suit, (Suit, ), { "color": color})
-RANKS = { rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)]) for rank in RANK_NAMES }
-SUITS = { suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2]) for suit in SUIT_NAMES }
+RANKS = {rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)])
+ for rank in RANK_NAMES}
+SUITS = {suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2])
+ for suit in SUIT_NAMES}
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 __str__(self):
- return "{0} of {1}".format(self.rank.__class__.__name__, self.suit.__class__.__name__)
+ return "{0} of {1}".format(self.rank.classname, self.suit.classname)
def __repr__(self):
return "<Card {0} >".format(str(self))
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __int__(self):
- return SUIT_NAMES.index(self.suit.__class__.__name__) * len(RANKS) + \
- RANK_NAMES.index(self.rank.__class__.__name__)
+ return SUIT_NAMES.index(self.suit.classname) * len(RANKS) + \
+ RANK_NAMES.index(self.rank.classname)
class CardCollection:
def __init__(self, collection=[]):
self.deck = list(collection)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[len(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 __getitem__(self, i):
return self.deck[i]
def __len__(self):
return len(self.deck)
def __repr__(self):
return repr(self.deck)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values() for suit in SUITS.values()]
- sorted_deck = sorted(deck, key=lambda x: int(x))
+ sorted_deck = sorted(deck, key=int)
return sorted_deck
def rank_less_than(card, rank):
if card.rank == RANKS["Ace"]():
return False
- return RANK_NAMES.index(card.rank.__class__.__name__) < RANK_NAMES.index(rank)
+ return RANK_NAMES.index(card.rank.classname) < RANK_NAMES.index(rank)
def BeloteDeck():
return CardCollection([card for card in StandardDeck()
if rank_less_than(card, "Seven")])
def SixtySixDeck():
return CardCollection([card for card in StandardDeck()
if rank_less_than(card, "Nine")])

Никола обнови решението на 25.03.2014 08:04 (преди над 10 години)

# Mixins and stuff
class KnowsItsName:
@property
def classname(self):
return self.__class__.__name__
-class Suit(KnowsItsName):
- def __init__(self):
- self.color = self.__class__.color
-
def __str__(self):
- return self.color
+ return self.classname
def __eq__(self, other):
- return self.color == other.color
+ return self.classname == other.classname
+ def __repr__(self):
+ return str(self)
+
+class Suit(KnowsItsName):
+ def __init__(self):
+ self.color = self.__class__.color
+
class Rank(KnowsItsName):
def __init__(self):
- self.symbol = self.__class__.symbol
-
- def __str__(self):
- return self.symbol
+ self.symbol = self.__class__.symbol
- def __eq__(self, other):
- return self.symbol == other.symbol
-
# Sorted!
RANK_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace', ]
RANK_SYMBOLS = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
SUIT_NAMES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
SUIT_COLORS = [ "red", "black" ]
def create_rank(rank, symbol):
return type(rank, (Rank, ), { "symbol": symbol})
def create_suit(suit, color):
return type(suit, (Suit, ), { "color": color})
RANKS = {rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)])
for rank in RANK_NAMES}
SUITS = {suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2])
for suit in SUIT_NAMES}
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 __str__(self):
return "{0} of {1}".format(self.rank.classname, self.suit.classname)
def __repr__(self):
return "<Card {0} >".format(str(self))
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __int__(self):
return SUIT_NAMES.index(self.suit.classname) * len(RANKS) + \
RANK_NAMES.index(self.rank.classname)
class CardCollection:
def __init__(self, collection=[]):
self.deck = list(collection)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[len(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 __getitem__(self, i):
return self.deck[i]
def __len__(self):
return len(self.deck)
def __repr__(self):
return repr(self.deck)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values() for suit in SUITS.values()]
sorted_deck = sorted(deck, key=int)
return sorted_deck
-def rank_less_than(card, rank):
+def rank_greater_than(card, rank):
if card.rank == RANKS["Ace"]():
- return False
+ return True
return RANK_NAMES.index(card.rank.classname) < RANK_NAMES.index(rank)
def BeloteDeck():
return CardCollection([card for card in StandardDeck()
- if rank_less_than(card, "Seven")])
+ if rank_greater_than(card, "Six")])
def SixtySixDeck():
return CardCollection([card for card in StandardDeck()
- if rank_less_than(card, "Nine")])
+ if rank_greater_than(card, "Eight")])

Никола обнови решението на 25.03.2014 08:17 (преди над 10 години)

# Mixins and stuff
class KnowsItsName:
@property
def classname(self):
return self.__class__.__name__
def __str__(self):
return self.classname
def __eq__(self, other):
return self.classname == other.classname
- def __repr__(self):
- return str(self)
class Suit(KnowsItsName):
def __init__(self):
self.color = self.__class__.color
class Rank(KnowsItsName):
def __init__(self):
self.symbol = self.__class__.symbol
# Sorted!
RANK_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace', ]
RANK_SYMBOLS = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
SUIT_NAMES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
SUIT_COLORS = [ "red", "black" ]
def create_rank(rank, symbol):
return type(rank, (Rank, ), { "symbol": symbol})
def create_suit(suit, color):
return type(suit, (Suit, ), { "color": color})
RANKS = {rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)])
for rank in RANK_NAMES}
SUITS = {suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2])
for suit in SUIT_NAMES}
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 __str__(self):
return "{0} of {1}".format(self.rank.classname, self.suit.classname)
def __repr__(self):
return "<Card {0} >".format(str(self))
def __eq__(self, other):
return (self.rank, self.suit) == (other.rank, other.suit)
def __int__(self):
return SUIT_NAMES.index(self.suit.classname) * len(RANKS) + \
RANK_NAMES.index(self.rank.classname)
class CardCollection:
def __init__(self, collection=[]):
self.deck = list(collection)
def draw(self, index):
return self.deck.pop(index)
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.pop(0)
def top_card(self):
return self.deck[len(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 __getitem__(self, i):
return self.deck[i]
def __len__(self):
return len(self.deck)
def __repr__(self):
return repr(self.deck)
def StandardDeck():
deck = [Card(rank, suit) for rank in RANKS.values() for suit in SUITS.values()]
sorted_deck = sorted(deck, key=int)
return sorted_deck
def rank_greater_than(card, rank):
if card.rank == RANKS["Ace"]():
return True
return RANK_NAMES.index(card.rank.classname) < RANK_NAMES.index(rank)
def BeloteDeck():
return CardCollection([card for card in StandardDeck()
if rank_greater_than(card, "Six")])
def SixtySixDeck():
return CardCollection([card for card in StandardDeck()
if rank_greater_than(card, "Eight")])