Решение на Тесте карти от Ралица Цанова

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

Към профила на Ралица Цанова

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 12 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class Rank:
symbol = ''
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
color = ''
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return self.__class__.__name__
class Ace(Rank):
def __init__(self):
self.symbol = 'A'
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 = '5'
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 Hearts(Suit):
def __init__(self):
self.color = 'red'
class Clubs(Suit):
def __init__(self):
self.color = 'red'
class Spades(Suit):
def __init__(self):
self.color = 'black'
class Diamonds(Suit):
def __init__(self):
self.color = 'black'
RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
'Jack': Jack, 'Queen': Queen, 'King': King}
SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
'Diamonds': Diamonds}
SUITS_ = [Diamonds, Hearts, Spades, Clubs]
RANKS_ = [King, Queen, Jack, Ten, Nine, Eight, Seven,
Six, Five, Four, Three, Two, Ace]
class Card:
rank = Rank('')
suit = Suit('')
def __init__(self, rank, suit):
super(Card, self).__setattr__("rank", rank())
super(Card, self).__setattr__("suit", suit())
def __setattr__(self, name, value):
raise AttributeError("can't set attribute")
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __repr__(self):
return "<Card {} of {}>".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
def __init__(self, collection=[]):
self.collection = list(collection)
def __len__(self):
return len(self.collection)
def __getitem__(self, index):
return self.collection[index]
def __repr__(self):
return str(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):
for i in range(0, len(self.collection)):
if(self.collection(i) == card):
return i
raise ValueError("Card" + str(card) + "is not in list")
def CreateDeck(suits, ranks):
deck = CardCollection()
for suit in suits:
for rank in ranks:
deck.add(Card(rank, suit))
return deck
def StandardDeck():
return CreateDeck(SUITS_, RANKS_)
def BeloteDeck():
return CreateDeck(SUITS_, RANKS_[:7] + [RANKS_[12]])
def SixtySixDeck():
return CreateDeck(SUITS_, RANKS_[:5] + [RANKS_[12]])

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

F..E..FF........
======================================================================
ERROR: 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-1h80he4/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
  File "/tmp/d20140407-19315-1h80he4/solution.py", line 182, in index
    if(self.collection(i) == card):
TypeError: 'list' object is not callable

======================================================================
FAIL: test_belote_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-1h80he4/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 8:
King of Clubs
King of Hearts

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

======================================================================
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-1h80he4/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 6:
King of Clubs
King of Hearts

Diff is 687 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-1h80he4/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 13:
King of Clubs
King of Hearts

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

----------------------------------------------------------------------
Ran 16 tests in 0.042s

FAILED (failures=3, errors=1)

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

Ралица обнови решението на 24.03.2014 21:48 (преди над 10 години)

+class Rank:
+ symbol = ''
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+
+ def __eq__(self, other):
+ return (self.symbol == other.symbol)
+
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ color = ''
+ def __init__(self, color):
+ self.color = color
+
+
+ def __eq__(self, other):
+ return (self.color == other.color)
+
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+ self.symbol = 'A'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+ self.symbol = '2'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+ self.symbol = '3'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+ self.symbol = '4'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+ self.symbol = '5'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+ self.symbol = '6'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+ self.symbol = '7'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+ self.symbol = '8'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+ self.symbol = '9'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+ self.symbol = '10'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+ self.symbol = 'J'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+ self.symbol = 'Q'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+ self.symbol = 'K'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.color = 'red'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.color = 'red'
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.color = 'black'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.color = 'black'
+
+
+class Card:
+ rank = Rank('')
+ suit = Suit('')
+ def __init__(self, rank, suit):
+ super(Card, self).__setattr__("rank", rank())
+ super(Card, self).__setattr__("suit", suit())
+
+
+ def __setattr__(self, name, value):
+ error = "can't set attribute"
+ raise AttributeError(error)
+
+
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
+
+
+ def __repr__(self):
+ return "<Card {} of {}>".format(self.rank, self.suit)
+
+
+ def __eq__(self, other):
+ return (self.rank == other.rank and self.suit == other.suit)
+
+
+class CardCollection:
+ def __init__(self, collection = []):
+ self.collection = list(collection)
+
+
+ def __len__(self):
+ return len(self.collection)
+
+
+ def __getitem__(self,index):
+ return self.collection[index]
+
+
+ def __repr__(self):
+ return str(self.collection)
+
+
+ def draw(self, index):
+ card = self.collection[index]
+ del(self.collection[index])
+ return card
+
+
+ def draw_from_top(self):
+ top_card = self.collection[-1]
+ del(self.collection[-1])
+ return top_card
+
+
+ def draw_from_bottom(self):
+ bottom_card = self.collection[0]
+ del(self.collection[0])
+ return bottom_card
+
+
+ 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 _ in range(0, len(self.collection)):
+ if(self.collection(_) == card):
+ return _
+ raise ValueError("Card" + str(card) + "is not in list")
+
+
+def StandardDeck():
+ standart_deck = CardCollection()
+ for _ in COLORS:
+ for __ in SYMBOLS:
+ standart_deck.add(Card(__,_))
+ return standart_deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ for _ in COLORS:
+ for __ in range(0,len(SYMBOLS)):
+ if(__ <= 6 or __ == 12):
+ belote_deck.add(Card(SYMBOLS[__],_))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ for _ in COLORS:
+ for __ in range(0,len(SYMBOLS)):
+ if(__ <= 4 or __ == 12):
+ sixty_six_deck.add(Card(SYMBOLS[__],_))
+ return sixty_six_deck
+
+
+COLORS = [Diamonds, Hearts, Spades, Clubs]
+
+
+SYMBOLS = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace]
+
+
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
+ 'Jack': Jack, 'Queen': Queen, 'King': King}
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
+ 'Diamonds': Diamonds}
  • Семантично е по-подходящо symbol и color, rank да ги направиш атрибути на класа вместо да ги подаваш през __init__-и и да ги правиш атрибути на инстанциите

  • между методите в един клас се оставя по един празен ред

  • скоби при return и del са противопоказни

    • прегледай и help(list.pop). Може да ти хрумне по-подходяща имплементация за draw_...
  • _, __... Не! смени ги с подходящи имена

    • COLORS не съдържа точно това, което би очаквал. Note: Suit е английската дума за боя на карта
  • StandardDeck, BeloteDeck, SixtySixDeck имат голямо количество повтарящ се код. Можеш ли да го преизползваш някак?

Ралица обнови решението на 25.03.2014 22:58 (преди над 10 години)

class Rank:
symbol = ''
+
def __init__(self, symbol):
self.symbol = symbol
-
def __eq__(self, other):
- return (self.symbol == other.symbol)
+ return self.symbol == other.symbol
-
def __str__(self):
return self.__class__.__name__
-
+
class Suit:
color = ''
+
def __init__(self, color):
self.color = color
-
def __eq__(self, other):
- return (self.color == other.color)
+ return self.color == other.color
-
def __str__(self):
return self.__class__.__name__
-
+
class Ace(Rank):
def __init__(self):
- Rank.__init__(self, 'A')
self.symbol = 'A'
class Two(Rank):
def __init__(self):
- Rank.__init__(self, '2')
self.symbol = '2'
class Three(Rank):
def __init__(self):
- Rank.__init__(self, '3')
self.symbol = '3'
class Four(Rank):
def __init__(self):
- Rank.__init__(self, '4')
self.symbol = '4'
class Five(Rank):
def __init__(self):
- Rank.__init__(self, '5')
self.symbol = '5'
class Six(Rank):
def __init__(self):
- Rank.__init__(self, '6')
self.symbol = '6'
class Seven(Rank):
def __init__(self):
- Rank.__init__(self, '7')
self.symbol = '7'
class Eight(Rank):
def __init__(self):
- Rank.__init__(self, '8')
self.symbol = '8'
class Nine(Rank):
def __init__(self):
- Rank.__init__(self, '9')
self.symbol = '9'
class Ten(Rank):
def __init__(self):
- Rank.__init__(self, '10')
self.symbol = '10'
class Jack(Rank):
def __init__(self):
- Rank.__init__(self, 'J')
self.symbol = 'J'
class Queen(Rank):
def __init__(self):
- Rank.__init__(self, 'Q')
self.symbol = 'Q'
-
+
class King(Rank):
def __init__(self):
- Rank.__init__(self, 'K')
self.symbol = 'K'
class Hearts(Suit):
def __init__(self):
- Suit.__init__(self, 'red')
self.color = 'red'
-
+
class Clubs(Suit):
def __init__(self):
- Suit.__init__(self, 'red')
self.color = 'red'
class Spades(Suit):
def __init__(self):
- Suit.__init__(self, 'black')
self.color = 'black'
class Diamonds(Suit):
def __init__(self):
- Suit.__init__(self, 'black')
self.color = 'black'
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
+ 'Jack': Jack, 'Queen': Queen, 'King': King}
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
+ 'Diamonds': Diamonds}
+
+
+SUITS_ = [Diamonds, Hearts, Spades, Clubs]
+
+
+RANKS_ = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace]
+
+
class Card:
rank = Rank('')
suit = Suit('')
+
def __init__(self, rank, suit):
super(Card, self).__setattr__("rank", rank())
super(Card, self).__setattr__("suit", suit())
-
def __setattr__(self, name, value):
- error = "can't set attribute"
- raise AttributeError(error)
+ raise AttributeError("can't set attribute")
-
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
-
def __repr__(self):
return "<Card {} of {}>".format(self.rank, self.suit)
-
def __eq__(self, other):
- return (self.rank == other.rank and self.suit == other.suit)
+ return self.rank == other.rank and self.suit == other.suit
class CardCollection:
- def __init__(self, collection = []):
+ def __init__(self, collection=[]):
self.collection = list(collection)
-
def __len__(self):
return len(self.collection)
-
- def __getitem__(self,index):
+ def __getitem__(self, index):
return self.collection[index]
-
def __repr__(self):
return str(self.collection)
-
def draw(self, index):
- card = self.collection[index]
- del(self.collection[index])
- return card
+ return self.collection.pop(index)
-
def draw_from_top(self):
- top_card = self.collection[-1]
- del(self.collection[-1])
- return top_card
+ return self.collection.pop(-1)
-
def draw_from_bottom(self):
- bottom_card = self.collection[0]
- del(self.collection[0])
- return bottom_card
+ 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):
- for _ in range(0, len(self.collection)):
- if(self.collection(_) == card):
- return _
+ for i in range(0, len(self.collection)):
+ if(self.collection(i) == card):
+ return i
raise ValueError("Card" + str(card) + "is not in list")
+def CreateDeck(suits, ranks):
+ deck = CardCollection()
+ for suit in suits:
+ for rank in ranks:
+ deck.add(Card(rank, suit))
+ return deck
+
+
def StandardDeck():
- standart_deck = CardCollection()
- for _ in COLORS:
- for __ in SYMBOLS:
- standart_deck.add(Card(__,_))
- return standart_deck
+ return CreateDeck(SUITS_, RANKS_)
def BeloteDeck():
- belote_deck = CardCollection()
- for _ in COLORS:
- for __ in range(0,len(SYMBOLS)):
- if(__ <= 6 or __ == 12):
- belote_deck.add(Card(SYMBOLS[__],_))
- return belote_deck
+ return CreateDeck(SUITS_, RANKS_[:7] + [RANKS_[12]])
def SixtySixDeck():
- sixty_six_deck = CardCollection()
- for _ in COLORS:
+ return CreateDeck(SUITS_, RANKS_[:5] + [RANKS_[12]])
- for __ in range(0,len(SYMBOLS)):
- if(__ <= 4 or __ == 12):
- sixty_six_deck.add(Card(SYMBOLS[__],_))
- return sixty_six_deck
-
-
-COLORS = [Diamonds, Hearts, Spades, Clubs]
-
-
-SYMBOLS = [King, Queen, Jack, Ten, Nine, Eight, Seven,
- Six, Five, Four, Three, Two, Ace]
-
-
-RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
- 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
- 'Jack': Jack, 'Queen': Queen, 'King': King}
-
-
-SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
- 'Diamonds': Diamonds}