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

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

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

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 11 успешни тест(а)
  • 5 неуспешни тест(а)

Код

from collections import OrderedDict
from functools import reduce
class Rank:
def __init__(self, symbol):
self.__symbol = symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__symbol == other.__symbol
RANKS_NAMES = OrderedDict([("A", "Ace"), ("2", "Two"), ("3", "Three"),
("4", "Four"), ("5", "Five"), ("6", "Six"),
("7", "Seven"), ("8", "Eight"), ("9", "Nine"),
("10", "Ten"), ("J", "Jack"), ("Q", "Queen"),
("K", "King")])
RANKS_ORD = OrderedDict()
for symbol in RANKS_NAMES.keys():
RANKS_ORD[RANKS_NAMES.get(symbol)] = type(
RANKS_NAMES.get(symbol), (Rank,),
dict(__init__=lambda self, symbol=symbol:
Rank.__init__(self, symbol)))
RANKS_ORD = OrderedDict(reversed(list(RANKS_ORD.items())))
RANKS = dict(RANKS_ORD)
class Suit:
def __init__(self, color):
self.__color = color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
SUITS_NAMES = OrderedDict(
[("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
("Spades", "black")])
SUITS_ORD = OrderedDict()
for name in SUITS_NAMES.keys():
SUITS_ORD[name] = type(name, (Suit,),
dict(__init__=lambda self,
color=SUITS_NAMES.get(name):
Suit.__init__(self, color)))
SUITS = dict(SUITS_ORD)
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
def __str__(self):
return str(self.__rank) + " of " + str(self.__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) + ">"
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
class CardCollection: # Представлява колекция (тесте) от карти
def __init__(self, collection=[]):
self.__collection = collection
def __len__(self):
return len(self.__collection)
def __getitem__(self, index):
return self.__collection[index]
def draw(self, index):
result = self.__collection[index]
del self.__collection[index]
return result
def draw_from_bottom(self):
return self.draw(0)
def draw_from_top(self):
return self.draw(-1)
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):
for index in range(len(self.__collection)):
if self.__collection[index] == card:
return index
def __repr__(self):
return reduce(lambda a, b: a + repr(b) +
", " * (not b == self.__collection[-1]),
[x for x in self.__collection], "[") + "]"
# str([repr(x) for x in self.__collection])
def GenerateDeck(ranks_exclude={}):
deck = CardCollection()
for suit in SUITS_ORD:
for rank in RANKS_ORD:
if not RANKS_ORD[str(rank)] in ranks_exclude:
deck.add(Card(RANKS_ORD[str(rank)],
SUITS_ORD[str(suit)]))
return deck
def StandardDeck():
return GenerateDeck({})
RANKS_EXCLUDE_BELOTE = {
RANKS["Two"], RANKS["Three"], RANKS["Four"], RANKS["Five"], RANKS["Six"]}
def BeloteDeck():
return GenerateDeck(RANKS_EXCLUDE_BELOTE)
def SixtySixDeck():
RANKS_EXCLUDE_SIXTY_SIX = {
RANKS["Two"], RANKS["Three"], RANKS[
"Four"], RANKS["Five"], RANKS["Six"],
RANKS["Seven"], RANKS["Eight"]}
return GenerateDeck(RANKS_EXCLUDE_SIXTY_SIX)

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

.F.F.FFF........
======================================================================
FAIL: test_deck_add (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-hsgry3/test.py", line 121, in test_deck_add
    self.assertEqual(deck[0], card1)
AssertionError: <Card King of Diamonds> != <Card Three of Hearts>

======================================================================
FAIL: test_deck_index (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-hsgry3/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
AssertionError: 0 != 33

======================================================================
FAIL: test_deck_order (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-hsgry3/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <Card King of Diamonds> != <Card Six of Clubs>

======================================================================
FAIL: test_sixtysix_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-hsgry3/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 5:
Ace of Diamonds
Eight of Diamonds

Second list contains 38 additional elements.
First extra element 24:
King of Spades

Diff is 1313 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_standard_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-hsgry3/test.py", line 171, in test_standard_deck
    self.assertEqual(STANDARD_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 7:
Six of Diamonds
Ace of Diamonds

Second list contains 62 additional elements.
First extra element 52:
Jack of Hearts

Diff is 2418 characters long. Set self.maxDiff to None to see it.

----------------------------------------------------------------------
Ran 16 tests in 0.039s

FAILED (failures=5)

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

Марио обнови решението на 26.03.2014 02:20 (преди над 10 години)

+from collections import OrderedDict
+from functools import reduce
+
+
+class Rank:
+
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__symbol == other.__symbol
+
+# Attention : Ace of Hacks
+RANKS_NAMES = OrderedDict([("A", "Ace"), ("2", "Two"), ("3", "Three"),
+ ("4", "Four"), ("5", "Five"), ("6", "Six"),
+ ("7", "Seven"), ("8", "Eight"), ("9", "Nine"),
+ ("10", "Ten"), ("J", "Jack"), ("Q", "Queen"),
+ ("K", "King")])
+
+RANKS_ORD = OrderedDict()
+for symbol in RANKS_NAMES.keys():
+ RANKS_ORD[RANKS_NAMES.get(symbol)] = type(
+ RANKS_NAMES.get(symbol), (Rank,),
+ dict(__init__=lambda self, symbol=symbol:
+ Rank.__init__(self, symbol)))
+
+RANKS_ORD = OrderedDict(reversed(list(RANKS_ORD.items())))
+RANKS = dict(RANKS_ORD)
+
+
+class Suit:
+
+ def __init__(self, color):
+ self.__color = color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+SUITS_NAMES = OrderedDict(
+ [("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
+ ("Spades", "black")])
+SUITS_ORD = OrderedDict()
+for name in SUITS_NAMES.keys():
+ SUITS_ORD[name] = type(name, (Suit,),
+ dict(__init__=lambda self,
+ color=SUITS_NAMES.get(name):
+ Suit.__init__(self, color)))
+SUITS = dict(SUITS_ORD)
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
+
+ def __str__(self):
+ return str(self.__rank) + " of " + str(self.__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) + ">"
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+class CardCollection: # Представлява колекция (тесте) от карти
+
+ def __init__(self, collection=[]):
+ self.__collection = collection
+
+ def __len__(self):
+ return len(self.__collection)
+
+ def __getitem__(self, index):
+ return self.__collection[index]
+
+ def draw(self, index):
+ result = self.__collection[index]
+ del self.__collection[index]
+ return result
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ 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):
+ for index in self.__collection:
+ if self.__collection[index] == card:
+ return index
+
+ def __repr__(self):
+ return reduce(lambda a, b: a + repr(b) +
+ ", " * (not b == self.__collection[-1]),
+ [x for x in self.__collection], "[") + "]"
+# str([repr(x) for x in self.__collection])
+
+
+def GenerateDeck(ranks_exclude={}):
+ deck = CardCollection()
+ for suit in SUITS_ORD:
+ for rank in RANKS_ORD:
+ if not RANKS_ORD[str(rank)] in ranks_exclude:
+ deck.add(Card(RANKS_ORD[str(rank)],
+ SUITS_ORD[str(suit)]))
+ return deck
+
+
+def StandardDeck():
+ return GenerateDeck({})
+
+RANKS_EXCLUDE_BELOTE = {
+ RANKS["Two"], RANKS["Three"], RANKS["Four"], RANKS["Five"], RANKS["Six"]}
+
+
+def BeloteDeck():
+ return GenerateDeck(RANKS_EXCLUDE_BELOTE)
+
+
+def SixtySixDeck():
+ RANKS_EXCLUDE_SIXTY_SIX = RANKS_EXCLUDE_BELOTE + \
+ [RANKS["Seven"], RANKS["Eight"]]
+ return GenerateDeck(RANKS_EXCLUDE_SIXTY_SIX)

Марио обнови решението на 26.03.2014 11:54 (преди над 10 години)

from collections import OrderedDict
from functools import reduce
class Rank:
def __init__(self, symbol):
self.__symbol = symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__symbol == other.__symbol
-# Attention : Ace of Hacks
RANKS_NAMES = OrderedDict([("A", "Ace"), ("2", "Two"), ("3", "Three"),
("4", "Four"), ("5", "Five"), ("6", "Six"),
("7", "Seven"), ("8", "Eight"), ("9", "Nine"),
("10", "Ten"), ("J", "Jack"), ("Q", "Queen"),
("K", "King")])
RANKS_ORD = OrderedDict()
for symbol in RANKS_NAMES.keys():
RANKS_ORD[RANKS_NAMES.get(symbol)] = type(
RANKS_NAMES.get(symbol), (Rank,),
dict(__init__=lambda self, symbol=symbol:
Rank.__init__(self, symbol)))
RANKS_ORD = OrderedDict(reversed(list(RANKS_ORD.items())))
RANKS = dict(RANKS_ORD)
class Suit:
def __init__(self, color):
self.__color = color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
SUITS_NAMES = OrderedDict(
[("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
("Spades", "black")])
SUITS_ORD = OrderedDict()
for name in SUITS_NAMES.keys():
SUITS_ORD[name] = type(name, (Suit,),
dict(__init__=lambda self,
color=SUITS_NAMES.get(name):
Suit.__init__(self, color)))
SUITS = dict(SUITS_ORD)
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
def __str__(self):
return str(self.__rank) + " of " + str(self.__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) + ">"
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
class CardCollection: # Представлява колекция (тесте) от карти
def __init__(self, collection=[]):
self.__collection = collection
def __len__(self):
return len(self.__collection)
def __getitem__(self, index):
return self.__collection[index]
def draw(self, index):
result = self.__collection[index]
del self.__collection[index]
return result
def draw_from_bottom(self):
return self.draw(0)
def draw_from_top(self):
return self.draw(-1)
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):
- for index in self.__collection:
+ for index in range(len(self.__collection)):
if self.__collection[index] == card:
return index
def __repr__(self):
return reduce(lambda a, b: a + repr(b) +
", " * (not b == self.__collection[-1]),
[x for x in self.__collection], "[") + "]"
# str([repr(x) for x in self.__collection])
def GenerateDeck(ranks_exclude={}):
deck = CardCollection()
for suit in SUITS_ORD:
for rank in RANKS_ORD:
if not RANKS_ORD[str(rank)] in ranks_exclude:
deck.add(Card(RANKS_ORD[str(rank)],
SUITS_ORD[str(suit)]))
return deck
def StandardDeck():
return GenerateDeck({})
RANKS_EXCLUDE_BELOTE = {
RANKS["Two"], RANKS["Three"], RANKS["Four"], RANKS["Five"], RANKS["Six"]}
def BeloteDeck():
return GenerateDeck(RANKS_EXCLUDE_BELOTE)
def SixtySixDeck():
- RANKS_EXCLUDE_SIXTY_SIX = RANKS_EXCLUDE_BELOTE + \
- [RANKS["Seven"], RANKS["Eight"]]
+ RANKS_EXCLUDE_SIXTY_SIX = {
+ RANKS["Two"], RANKS["Three"], RANKS[
+ "Four"], RANKS["Five"], RANKS["Six"],
+ RANKS["Seven"], RANKS["Eight"]}
return GenerateDeck(RANKS_EXCLUDE_SIXTY_SIX)