Решение на Тесте карти от Петьо Чолаков

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

Към профила на Петьо Чолаков

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 14 успешни тест(а)
  • 2 неуспешни тест(а)

Код

import itertools
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return str(self.symbol)
# the 13 card ranks
class Two(Rank):
def __init__(self):
Rank.__init__(self, 'Two')
class Three(Rank):
def __init__(self):
Rank.__init__(self, 'Three')
class Four(Rank):
def __init__(self):
Rank.__init__(self, 'Four')
class Five(Rank):
def __init__(self):
Rank.__init__(self, 'Five')
class Six(Rank):
def __init__(self):
Rank.__init__(self, 'Six')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, 'Seven')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, 'Eight')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, 'Nine')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, 'Ten')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'Jack')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Queen')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'King')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'Ace')
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}
# The four card suits
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return str(self.color)
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'Diamonds')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'Hearts')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'Spades')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'Clubs')
SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
'Spades': Spades, 'Clubs': Clubs}
class Card():
def __init__(self, Rank, Suit):
object.__setattr__(self, 'rank', Rank())
object.__setattr__(self, 'suit', Suit())
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __delattr__(self, *args):
raise AttributeError("can't delete attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return '{} of {}'.format(self.rank, self.suit)
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
# check for duplicate cards?
def add(self, new_card):
self.collection.append(new_card)
def __getitem__(self, index):
return self.collection[index]
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 index(self, card):
return self.collection.index(card)
def __len__(self):
return len(self.collection)
def StandardDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
standard_deck = []
for card in itertools.product(ordered_suits, ordered_ranks):
standard_deck.append('{} of {}'.format(card[1], card[0]))
return standard_deck
def BeloteDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
belote_deck = []
for card in itertools.product(ordered_suits, ordered_ranks):
belote_deck.append('{} of {}'.format(card[1], card[0]))
return belote_deck
def SixtySixDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
sixty_six_deck = []
for card in itertools.product(ordered_suits, ordered_ranks):
sixty_six_deck.append('{} of {}'.format(card[1], card[0]))
return sixty_six_deck

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

...F.F..........
======================================================================
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-rauhni/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
AssertionError: 0 != 2

======================================================================
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-rauhni/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <solution.Card object at 0xb781ebcc> != <solution.Card object at 0xb7831f6c>

----------------------------------------------------------------------
Ran 16 tests in 0.023s

FAILED (failures=2)

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

Петьо обнови решението на 26.03.2014 16:40 (преди около 10 години)

+import itertools
+
+
+class Rank:
+
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return str(self.symbol)
+
+
+# the 13 card ranks
+class Two(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Two')
+
+
+class Three(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Three')
+
+
+class Four(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Four')
+
+
+class Five(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Five')
+
+
+class Six(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Six')
+
+
+class Seven(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Seven')
+
+
+class Eight(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Eight')
+
+
+class Nine(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Nine')
+
+
+class Ten(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Ten')
+
+
+class Jack(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Jack')
+
+
+class Queen(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Queen')
+
+
+class King(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'King')
+
+
+class Ace(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Ace')
+
+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}
+
+
+# The four card suits
+
+class Suit:
+
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return str(self.color)
+
+
+class Diamonds(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'Diamonds')
+
+
+class Hearts(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'Hearts')
+
+
+class Spades(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'Spades')
+
+
+class Clubs(Suit):
+
+ def __init__(self):
+ Suit.__init__(self, 'Clubs')
+
+SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
+ 'Spades': Spades, 'Clubs': Clubs}
+
+
+class Card():
+
+ def __init__(self, Rank, Suit):
+ object.__setattr__(self, 'rank', Rank())
+ object.__setattr__(self, 'suit', Suit())
+
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+ def __delattr__(self, *args):
+ raise AttributeError("can't delete attribute")
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return '{} of {}'.format(self.rank, self.suit)
+
+
+class CardCollection():
+
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ # check for duplicate cards?
+ def add(self, new_card):
+ self.collection.append(new_card)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ 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 index(self, card):
+ return self.collection.index(card)
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def StandartDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ standart_deck = []
+
+ for card in itertools.product(ordered_suits, ordered_ranks):
+ standart_deck.append('{} of {}'.format(card[1], card[0]))
+ return standart_deck
+
+
+def BeloteDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ belote_deck = []
+ for card in itertools.product(ordered_suits, ordered_ranks):
+ belote_deck.append('{} of {}'.format(card[1], card[0]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ sixty_six_deck = []
+ for card in itertools.product(ordered_suits, ordered_ranks):
+ sixty_six_deck.append('{} of {}'.format(card[1], card[0]))
+ return sixty_six_deck

Петьо обнови решението на 26.03.2014 16:45 (преди около 10 години)

import itertools
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return str(self.symbol)
# the 13 card ranks
class Two(Rank):
def __init__(self):
Rank.__init__(self, 'Two')
class Three(Rank):
def __init__(self):
Rank.__init__(self, 'Three')
class Four(Rank):
def __init__(self):
Rank.__init__(self, 'Four')
class Five(Rank):
def __init__(self):
Rank.__init__(self, 'Five')
class Six(Rank):
def __init__(self):
Rank.__init__(self, 'Six')
class Seven(Rank):
def __init__(self):
Rank.__init__(self, 'Seven')
class Eight(Rank):
def __init__(self):
Rank.__init__(self, 'Eight')
class Nine(Rank):
def __init__(self):
Rank.__init__(self, 'Nine')
class Ten(Rank):
def __init__(self):
Rank.__init__(self, 'Ten')
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'Jack')
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Queen')
class King(Rank):
def __init__(self):
Rank.__init__(self, 'King')
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'Ace')
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}
# The four card suits
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return str(self.color)
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'Diamonds')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'Hearts')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'Spades')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'Clubs')
SUITS = {'Diamonds': Diamonds, 'Hearts': Hearts,
'Spades': Spades, 'Clubs': Clubs}
class Card():
def __init__(self, Rank, Suit):
object.__setattr__(self, 'rank', Rank())
object.__setattr__(self, 'suit', Suit())
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __delattr__(self, *args):
raise AttributeError("can't delete attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return '{} of {}'.format(self.rank, self.suit)
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
# check for duplicate cards?
def add(self, new_card):
self.collection.append(new_card)
def __getitem__(self, index):
return self.collection[index]
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 index(self, card):
return self.collection.index(card)
def __len__(self):
return len(self.collection)
-def StandartDeck():
+def StandardDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
- standart_deck = []
+ standard_deck = []
for card in itertools.product(ordered_suits, ordered_ranks):
- standart_deck.append('{} of {}'.format(card[1], card[0]))
- return standart_deck
+ standard_deck.append('{} of {}'.format(card[1], card[0]))
+ return standard_deck
def BeloteDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
belote_deck = []
for card in itertools.product(ordered_suits, ordered_ranks):
belote_deck.append('{} of {}'.format(card[1], card[0]))
return belote_deck
def SixtySixDeck():
ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
sixty_six_deck = []
for card in itertools.product(ordered_suits, ordered_ranks):
sixty_six_deck.append('{} of {}'.format(card[1], card[0]))
return sixty_six_deck