Решение на Тесте карти от Йончо Йончев

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

Към профила на Йончо Йончев

Резултати

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

Код

class Rank:
'''
The class holds the the representation of the rank of the cards aka their power
'''
def __init__(self, symbol=None):
self.symbol = symbol
class Ace(Rank):
def __init__(self, symbol='A'):
self.symbol = symbol
def __str__(self):
return Ace.__name__
class Two(Rank):
def __init__(self, symbol='2'):
self.symbol = symbol
def __str__(self):
return Two.__name__
class Three(Rank):
def __init__(self, symbol='3'):
self.symbol = symbol
def __str__(self):
return Three.__name__
class Four(Rank):
def __init__(self, symbol='4'):
self.symbol = symbol
def __str__(self):
return Four.__name__
class Five(Rank):
def __init__(self, symbol='5'):
self.symbol = symbol
def __str__(self):
return Five.__name__
class Six(Rank):
def __init__(self, symbol='6'):
self.symbol = symbol
def __str__(self):
return Six.__name__
class Seven(Rank):
def __init__(self, symbol='7'):
self.symbol = symbol
def __str__(self):
return Seven.__name__
class Eight(Rank):
def __init__(self, symbol='8'):
self.symbol = symbol
def __str__(self):
return Eight.__name__
class Nine(Rank):
def __init__(self, symbol='9'):
self.symbol = symbol
def __str__(self):
return Nine.__name__
class Ten(Rank):
def __init__(self, symbol='10'):
self.symbol = symbol
def __str__(self):
return Ten.__name__
class Jack(Rank):
def __init__(self, symbol='J'):
self.symbol = symbol
def __str__(self):
return Jack.__name__
class Queen(Rank):
def __init__(self, symbol='Q'):
self.symbol = symbol
def __str__(self):
return Queen.__name__
class King(Rank):
def __init__(self, symbol='K'):
self.symbol = symbol
def __str__(self):
return King.__name__
class Suit:
'''
The class holds the the representation of the suit of the card aka their color and ect
'''
def __init__(self, color=None):
self.color = colorcolor
class Diamonds(Suit):
def __init__(self, color='red'):
self.color = color
def __str__(self):
return Diamonds.__name__
class Hearts(Suit):
def __init__(self, color='red'):
self.color = color
def __str__(self):
return Hearts.__name__
class Spades(Suit):
def __init__(self, color='black'):
self.color = color
def __str__(self):
return Spades.__name__
class Clubs(Suit):
def __init__(self, color='black'):
self.color = color
def __str__(self):
return Clubs.__name__
RANKS = {
'King': King(), 'Six': Six(), 'Jack': Jack(), 'Five': Five(), 'Queen': Queen(),
'Ten': Ten(), 'Ace': Ace(), 'Three': Three(), 'Eight': Eight(), 'Four': Four(),
'Two': Two(), 'Seven': Seven(), 'Nine': Nine()}
SUITS = {'Diamonds': Diamonds(), 'Hearts':
Hearts(), 'Spades': Spades(), 'Clubs': Clubs()}
class Card(object):
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
def __init__(self, Rank, Suit):
self.rank = Rank
self.suit = Suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
deck: list of Card objects- they are itterable'''
def __init__(self, deck=[]):
self.deck = [Card(rank, suit)
for suit in SUITS.values() for rank in RANKS.values()]
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
def __iter__(self):
for card in deck:
yield card
def __len__(self):
return len(self.deck)
def __getattr__(self, key):
if key == 'rank':
return self.rank
elif key == 'suit':
return self.suit
raise AttributeError(key)
def draw(self, index):
return deck[index]
def draw_from_top(self):
return self.deck.pop(0)
def draw_from_bottom(self):
return self.deck.pop(len(deck) - 1)
def top_card(self):
return self.deck.pop(0)
def bottom_card(self):
return self.deck.pop(len(deck) - 1)
def add(self, card):
self.deck.append(card)
def index(self, card):
i = self.deck.index(card)
return i
def standard_deck(self):
return self.deck
def belote_deck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
return self.deck
def sixty_six_deck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
return self.deck

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

EEFFEFEEEFE.FEE.
======================================================================
ERROR: 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-kkg20g/test.py", line 174, in test_belote_deck
    cards = [str(card) for card in solution.BeloteDeck()]
AttributeError: 'module' object has no attribute 'BeloteDeck'

======================================================================
ERROR: 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-kkg20g/test.py", line 121, in test_deck_add
    self.assertEqual(deck[0], card1)
TypeError: 'CardCollection' object does not support indexing

======================================================================
ERROR: test_deck_iteration (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-kkg20g/test.py", line 166, in test_deck_iteration
    for card in deck:
  File "/tmp/d20140407-19315-kkg20g/solution.py", line 220, in __iter__
    for card in deck:
NameError: global name 'deck' is not defined

======================================================================
ERROR: 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-kkg20g/test.py", line 178, in test_sixtysix_deck
    cards = [str(card) for card in solution.SixtySixDeck()]
AttributeError: 'module' object has no attribute 'SixtySixDeck'

======================================================================
ERROR: 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-kkg20g/test.py", line 170, in test_standard_deck
    cards = [str(card) for card in solution.StandardDeck()]
AttributeError: 'module' object has no attribute 'StandardDeck'

======================================================================
ERROR: test_all_card_instances (test.CardTest)
----------------------------------------------------------------------
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-kkg20g/test.py", line 67, in test_all_card_instances
    self.assertIsInstance(card.rank, rank)
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 1062, in assertIsInstance
    if not isinstance(obj, cls):
TypeError: isinstance() arg 2 must be a type or tuple of types

======================================================================
ERROR: test_all_suits_ranks_equal (test.CardTest)
----------------------------------------------------------------------
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-kkg20g/test.py", line 92, in test_all_suits_ranks_equal
    self.assertEqual(card.rank, rank())
TypeError: 'Nine' object is not callable

======================================================================
ERROR: test_card_instance (test.CardTest)
----------------------------------------------------------------------
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-kkg20g/test.py", line 60, in test_card_instance
    self.assertIsInstance(aos.rank, solution.RANKS["Ace"])
  File "/opt/python3.3/lib/python3.3/unittest/case.py", line 1062, in assertIsInstance
    if not isinstance(obj, cls):
TypeError: isinstance() arg 2 must be a type or tuple of types

======================================================================
ERROR: test_suit_rank_equals (test.CardTest)
----------------------------------------------------------------------
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-kkg20g/test.py", line 85, in test_suit_rank_equals
    self.assertEqual(aos.rank, solution.RANKS["Ace"]())
TypeError: 'Ace' object is not callable

======================================================================
FAIL: test_deck_draw (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-kkg20g/test.py", line 141, in test_deck_draw
    self.assertEqual(card, deck.draw_from_top())
AssertionError: <solution.Card object at 0xb785e72c> != <solution.Card object at 0xb785e76c>

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

======================================================================
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-kkg20g/test.py", line 133, in test_deck_order
    self.assertEqual(deck.top_card(), card3)
AssertionError: <solution.Card object at 0xb78730ec> != <solution.Card object at 0xb7873ecc>

======================================================================
FAIL: test_all_cards_equal (test.CardTest)
----------------------------------------------------------------------
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-kkg20g/test.py", line 81, in test_all_cards_equal
    self.assertEqual(card1, card2)
AssertionError: <solution.Card object at 0xb787758c> != <solution.Card object at 0xb78775cc>

======================================================================
FAIL: test_card_equals (test.CardTest)
----------------------------------------------------------------------
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-kkg20g/test.py", line 73, in test_card_equals
    self.assertEqual(aos1, aos2)
AssertionError: <solution.Card object at 0xb7877dac> != <solution.Card object at 0xb7877dec>

----------------------------------------------------------------------
Ran 16 tests in 0.021s

FAILED (failures=5, errors=9)

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

Йончо обнови решението на 25.03.2014 23:22 (преди около 10 години)

+class Rank:
+
+ '''
+ The class holds the the type card representation
+ '''
+
+ symbol = None # символът на картата (A, 2, Q, K)
+
+ def __init__(self, symbol=None):
+ self.symbol = suit
+
+ def __str__(self):
+ """Returns a human-readable string representation."""
+ return Rank.__name__
+
+
+class Ace(Rank):
+
+ def __init__(self, symbol='A'):
+ self.symbol = suit
+
+
+class Two(Rank):
+
+ def __init__(self, symbol='2'):
+ self.symbol = suit
+
+
+class Three(Rank):
+
+ def __init__(self, symbol='3'):
+ self.symbol = suit
+
+
+class Four(Rank):
+
+ def __init__(self, symbol='4'):
+ self.symbol = suit
+
+
+class Five(Rank):
+
+ def __init__(self, symbol='5'):
+ self.symbol = suit
+
+
+class Six(Rank):
+
+ def __init__(self, symbol='6'):
+ self.symbol = suit
+
+
+class Seven(Rank):
+
+ def __init__(self, symbol='7'):
+ self.symbol = suit
+
+
+class Eight(Rank):
+
+ def __init__(self, symbol='8'):
+ self.symbol = suit
+
+
+class Nine(Rank):
+
+ def __init__(self, symbol='9'):
+ self.symbol = suit
+
+
+class Ten(Rank):
+
+ def __init__(self, symbol='10'):
+ self.symbol = suit
+
+
+class Jack(Rank):
+
+ def __init__(self, symbol='J'):
+ self.symbol = suit
+
+
+class Queen(Rank):
+
+ def __init__(self, symbol='Q'):
+ self.symbol = suit
+
+
+class King(Rank):
+
+ def __init__(self, symbol='Q'):
+ self.symbol = suit
+
+
+class Suit:
+
+ '''
+ The class holds the the color card representation
+ '''
+ color = None # цветът на боята ('red', 'black')
+
+ def __init__(self, color=None):
+ self.color = colorcolor
+
+ def __str__(self):
+ """Returns a human-readable string representation."""
+ return Rank.__name__
+ 'Diamonds', '', '', ''
+
+
+class Diamonds(Suit):
+
+ def __init__(self, color='red'):
+ self.color = suit
+
+
+class Hearts(Suit):
+
+ def __init__(self, color='red'):
+ self.color = suit
+
+
+class Spades(Suit):
+
+ def __init__(self, color='black'):
+ self.color = suit
+
+
+class Clubs(Suit):
+
+ def __init__(self, color='black'):
+ self.color = suit
+
+RANKS = {
+ 'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
+ 'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
+ 'Two': Two, 'Seven': Seven, 'Nine': Nine}
+
+SUITS = {'Diamonds': Diamonds, 'Hearts':
+ Hearts, 'Spades': Spades, 'Clubs': Clubs}
+
+
+class Card(object):
+
+ '''Represents a standard playing card.
+ Attributes:
+ suits-dictionary of suits
+ ranks-dictionary of ranks
+ '''
+
+ def __init__(self, rank, suit):
+ self.rank = RANKS[rank]
+ self.suit = SUITS[suit]
+
+ def __str__(self):
+ return '%s of %s' % (str(RANKS[rank]),
+ str(SUITS[suit]))
+
+ def __cmp__(self, other):
+ '''simple function
+ '''
+
+
+class CardCollection:
+
+ '''Represents a deck of cards.
+
+ Attributes:
+ cards: list of Card objects.
+ '''
+ deck = []
+
+ def __init__(self):
+ self.deck = []
+ for suit in SUITS.keys():
+ for rank in RANKS.keys():
+ card = Card(SUITS[suit], RANKS[rank])
+ self.deck.append(card)
+
+ def __str__(self):
+ res = []
+ for card in self.deck:
+ res.append(str(card))
+ return '\n'.join(res)
+
+ def draw(self, index):
+ return deck[index]
+
+ def draw_from_top(self):
+ return deck[0]
+
+ def draw_from_bottom(self):
+ return deck[len(deck) - 1]
+
+ def top_card(self):
+ return deck[0]
+
+ def bottom_card(self):
+ return deck[len(deck) - 1]
+
+ def add(self, card):
+ deck.add(card)
+
+ def index(self, card):
+ return deck[index]
+
+ def StandardDeck(self):
+ return deck
+
+ def BeloteDeck(self):
+ RANKS.remove('Two')
+ RANKS.remove('Three')
+ RANKS.remove('Four')
+ RANKS.remove('Five')
+ RANKS.remove('Six')
+ deck = CardCollection()
+
+ def SixtySixDeck(self):
+ RANKS.remove('Two')
+ RANKS.remove('Three')
+ RANKS.remove('Four')
+ RANKS.remove('Five')
+ RANKS.remove('Six')
+ RANKS.remove('Seven')
+ RANKS.remove('Eight')
+ deck = CardCollection()

Йончо обнови решението на 26.03.2014 10:22 (преди около 10 години)

class Rank:
'''
- The class holds the the type card representation
+ The class holds the the representation of the rank of the cards aka their power
'''
symbol = None # символът на картата (A, 2, Q, K)
def __init__(self, symbol=None):
self.symbol = suit
def __str__(self):
- """Returns a human-readable string representation."""
- return Rank.__name__
+ '''Returns a human-readable string representation of the rank of the card'''
+ return self.__name__
class Ace(Rank):
def __init__(self, symbol='A'):
self.symbol = suit
class Two(Rank):
def __init__(self, symbol='2'):
self.symbol = suit
class Three(Rank):
def __init__(self, symbol='3'):
self.symbol = suit
class Four(Rank):
def __init__(self, symbol='4'):
self.symbol = suit
class Five(Rank):
def __init__(self, symbol='5'):
self.symbol = suit
class Six(Rank):
def __init__(self, symbol='6'):
self.symbol = suit
class Seven(Rank):
def __init__(self, symbol='7'):
self.symbol = suit
class Eight(Rank):
def __init__(self, symbol='8'):
self.symbol = suit
class Nine(Rank):
def __init__(self, symbol='9'):
self.symbol = suit
class Ten(Rank):
def __init__(self, symbol='10'):
self.symbol = suit
class Jack(Rank):
def __init__(self, symbol='J'):
self.symbol = suit
class Queen(Rank):
def __init__(self, symbol='Q'):
self.symbol = suit
class King(Rank):
- def __init__(self, symbol='Q'):
+ def __init__(self, symbol='K'):
self.symbol = suit
class Suit:
'''
- The class holds the the color card representation
+ The class holds the the representation of the suit of the card aka their color and ect
'''
color = None # цветът на боята ('red', 'black')
def __init__(self, color=None):
self.color = colorcolor
def __str__(self):
- """Returns a human-readable string representation."""
- return Rank.__name__
- 'Diamonds', '', '', ''
+ '''Returns a human-readable string representation of the suit of the card'''
+ return self.__name__
class Diamonds(Suit):
def __init__(self, color='red'):
self.color = suit
class Hearts(Suit):
def __init__(self, color='red'):
self.color = suit
class Spades(Suit):
def __init__(self, color='black'):
self.color = suit
class Clubs(Suit):
def __init__(self, color='black'):
self.color = suit
RANKS = {
'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
'Two': Two, 'Seven': Seven, 'Nine': Nine}
SUITS = {'Diamonds': Diamonds, 'Hearts':
Hearts, 'Spades': Spades, 'Clubs': Clubs}
class Card(object):
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
def __init__(self, rank, suit):
self.rank = RANKS[rank]
self.suit = SUITS[suit]
def __str__(self):
return '%s of %s' % (str(RANKS[rank]),
str(SUITS[suit]))
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
- cards: list of Card objects.
+ deck: list of Card objects- they are itterable
'''
deck = []
def __init__(self):
self.deck = []
for suit in SUITS.keys():
for rank in RANKS.keys():
card = Card(SUITS[suit], RANKS[rank])
self.deck.append(card)
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
def draw(self, index):
return deck[index]
def draw_from_top(self):
- return deck[0]
+ return deck.remove(0)
def draw_from_bottom(self):
- return deck[len(deck) - 1]
+ return deck.remove(len(deck) - 1)
def top_card(self):
return deck[0]
def bottom_card(self):
return deck[len(deck) - 1]
def add(self, card):
deck.add(card)
def index(self, card):
return deck[index]
def StandardDeck(self):
return deck
def BeloteDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
deck = CardCollection()
+ return deck
def SixtySixDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
- deck = CardCollection()
+ deck = CardCollection()
+ return deck
+
+
+import unittest
+import random
+
+from solution import Card, RANKS, SUITS, CardCollection
+
+
+Card.__hash__ = lambda self: 1
+
+
+def random_cards(count=1):
+ if count == 1:
+ return Card(random.choice(list(RANKS.values())),
+ random.choice(list(SUITS.values())))
+
+ cards = set()
+ while len(cards) < count:
+ cards.add(Card(random.choice(list(RANKS.values())),
+ random.choice(list(SUITS.values()))))
+
+ return cards
+
+
+class CardTest(unittest.TestCase):
+
+ def test_card_instance(self):
+ aos = Card(RANKS["Ace"], SUITS["Spades"])
+ self.assertIsInstance(aos.rank, RANKS["Ace"])
+ self.assertIsInstance(aos.suit, SUITS["Spades"])
+
+ def test_card_equals(self):
+ aos1 = Card(RANKS["Ace"], SUITS["Spades"])
+ aos2 = Card(RANKS["Ace"], SUITS["Spades"])
+ self.assertEqual(aos1, aos2)
+
+ def test_suit_rank_equals(self):
+ aos = Card(RANKS["Ace"], SUITS["Spades"])
+ self.assertEqual(aos.rank, RANKS["Ace"]())
+ self.assertEqual(aos.suit, SUITS["Spades"]())
+
+ def test_to_string(self):
+ aos = Card(RANKS["Ace"], SUITS["Spades"])
+ self.assertEqual(str(aos), "Ace of Spades")
+
+
+class CardCollectionTest(unittest.TestCase):
+
+ def setUp(self):
+ self.deck = [Card(rank, suit) for rank in RANKS.values()
+ for suit in SUITS.values()]
+
+ def test_standard_deck(self):
+ deck = CardCollection(self.deck)
+ self.assertEqual(len(deck), 52)
+
+ def test_deck_add(self):
+ deck = CardCollection()
+ card1, card2 = random_cards(2)
+
+ deck.add(card1)
+ self.assertEqual(deck[0], card1)
+
+ deck.add(card2)
+ self.assertEqual(deck[1], card2)
+
+ self.assertEqual(len(deck), 2)
+
+ def test_deck_draw(self):
+ deck = CardCollection(self.deck)
+ card = deck.top_card()
+
+ self.assertEqual(card, deck.draw_from_top())
+ self.assertEqual(len(deck), 51)
+
+ def test_deck_iteration(self):
+ deck = CardCollection(self.deck)
+ for card in deck:
+ self.assertIsInstance(card, Card)
+
+if __name__ == '__main__':
+ unittest.main()

Йончо обнови решението на 26.03.2014 10:36 (преди около 10 години)

class Rank:
'''
The class holds the the representation of the rank of the cards aka their power
'''
symbol = None # символът на картата (A, 2, Q, K)
def __init__(self, symbol=None):
self.symbol = suit
def __str__(self):
- '''Returns a human-readable string representation of the rank of the card'''
+ '''Returns string representation of the rank of the card'''
return self.__name__
class Ace(Rank):
def __init__(self, symbol='A'):
self.symbol = suit
class Two(Rank):
def __init__(self, symbol='2'):
self.symbol = suit
class Three(Rank):
def __init__(self, symbol='3'):
self.symbol = suit
class Four(Rank):
def __init__(self, symbol='4'):
self.symbol = suit
class Five(Rank):
def __init__(self, symbol='5'):
self.symbol = suit
class Six(Rank):
def __init__(self, symbol='6'):
self.symbol = suit
class Seven(Rank):
def __init__(self, symbol='7'):
self.symbol = suit
class Eight(Rank):
def __init__(self, symbol='8'):
self.symbol = suit
class Nine(Rank):
def __init__(self, symbol='9'):
self.symbol = suit
class Ten(Rank):
def __init__(self, symbol='10'):
self.symbol = suit
class Jack(Rank):
def __init__(self, symbol='J'):
self.symbol = suit
class Queen(Rank):
def __init__(self, symbol='Q'):
self.symbol = suit
class King(Rank):
def __init__(self, symbol='K'):
self.symbol = suit
class Suit:
'''
The class holds the the representation of the suit of the card aka their color and ect
'''
color = None # цветът на боята ('red', 'black')
def __init__(self, color=None):
self.color = colorcolor
def __str__(self):
- '''Returns a human-readable string representation of the suit of the card'''
+ '''Returns string representation of the suit of the card'''
return self.__name__
class Diamonds(Suit):
def __init__(self, color='red'):
self.color = suit
class Hearts(Suit):
def __init__(self, color='red'):
self.color = suit
class Spades(Suit):
def __init__(self, color='black'):
self.color = suit
class Clubs(Suit):
def __init__(self, color='black'):
self.color = suit
RANKS = {
'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
'Two': Two, 'Seven': Seven, 'Nine': Nine}
SUITS = {'Diamonds': Diamonds, 'Hearts':
Hearts, 'Spades': Spades, 'Clubs': Clubs}
class Card(object):
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
def __init__(self, rank, suit):
self.rank = RANKS[rank]
self.suit = SUITS[suit]
def __str__(self):
return '%s of %s' % (str(RANKS[rank]),
str(SUITS[suit]))
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
deck: list of Card objects- they are itterable
'''
deck = []
def __init__(self):
self.deck = []
for suit in SUITS.keys():
for rank in RANKS.keys():
card = Card(SUITS[suit], RANKS[rank])
self.deck.append(card)
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
def draw(self, index):
return deck[index]
def draw_from_top(self):
return deck.remove(0)
def draw_from_bottom(self):
return deck.remove(len(deck) - 1)
def top_card(self):
return deck[0]
def bottom_card(self):
return deck[len(deck) - 1]
def add(self, card):
deck.add(card)
def index(self, card):
return deck[index]
def StandardDeck(self):
return deck
def BeloteDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
deck = CardCollection()
return deck
def SixtySixDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
deck = CardCollection()
return deck
-
-
-import unittest
-import random
-
-from solution import Card, RANKS, SUITS, CardCollection
-
-
-Card.__hash__ = lambda self: 1
-
-
-def random_cards(count=1):
- if count == 1:
- return Card(random.choice(list(RANKS.values())),
- random.choice(list(SUITS.values())))
-
- cards = set()
- while len(cards) < count:
- cards.add(Card(random.choice(list(RANKS.values())),
- random.choice(list(SUITS.values()))))
-
- return cards
-
-
-class CardTest(unittest.TestCase):
-
- def test_card_instance(self):
- aos = Card(RANKS["Ace"], SUITS["Spades"])
- self.assertIsInstance(aos.rank, RANKS["Ace"])
- self.assertIsInstance(aos.suit, SUITS["Spades"])
-
- def test_card_equals(self):
- aos1 = Card(RANKS["Ace"], SUITS["Spades"])
- aos2 = Card(RANKS["Ace"], SUITS["Spades"])
- self.assertEqual(aos1, aos2)
-
- def test_suit_rank_equals(self):
- aos = Card(RANKS["Ace"], SUITS["Spades"])
- self.assertEqual(aos.rank, RANKS["Ace"]())
- self.assertEqual(aos.suit, SUITS["Spades"]())
-
- def test_to_string(self):
- aos = Card(RANKS["Ace"], SUITS["Spades"])
- self.assertEqual(str(aos), "Ace of Spades")
-
-
-class CardCollectionTest(unittest.TestCase):
-
- def setUp(self):
- self.deck = [Card(rank, suit) for rank in RANKS.values()
- for suit in SUITS.values()]
-
- def test_standard_deck(self):
- deck = CardCollection(self.deck)
- self.assertEqual(len(deck), 52)
-
- def test_deck_add(self):
- deck = CardCollection()
- card1, card2 = random_cards(2)
-
- deck.add(card1)
- self.assertEqual(deck[0], card1)
-
- deck.add(card2)
- self.assertEqual(deck[1], card2)
-
- self.assertEqual(len(deck), 2)
-
- def test_deck_draw(self):
- deck = CardCollection(self.deck)
- card = deck.top_card()
-
- self.assertEqual(card, deck.draw_from_top())
- self.assertEqual(len(deck), 51)
-
- def test_deck_iteration(self):
- deck = CardCollection(self.deck)
- for card in deck:
- self.assertIsInstance(card, Card)
-
-if __name__ == '__main__':
- unittest.main()

Йончо обнови решението на 26.03.2014 14:02 (преди около 10 години)

class Rank:
'''
The class holds the the representation of the rank of the cards aka their power
'''
symbol = None # символът на картата (A, 2, Q, K)
def __init__(self, symbol=None):
self.symbol = suit
def __str__(self):
'''Returns string representation of the rank of the card'''
- return self.__name__
+ return Rank.__name__
class Ace(Rank):
def __init__(self, symbol='A'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Ace.__name__
+
class Two(Rank):
def __init__(self, symbol='2'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Two.__name__
+
class Three(Rank):
def __init__(self, symbol='3'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Three.__name__
+
class Four(Rank):
def __init__(self, symbol='4'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Four.__name__
+
class Five(Rank):
def __init__(self, symbol='5'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Five.__name__
+
class Six(Rank):
def __init__(self, symbol='6'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Six.__name__
+
class Seven(Rank):
def __init__(self, symbol='7'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Seven.__name__
+
class Eight(Rank):
def __init__(self, symbol='8'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Eight.__name__
+
class Nine(Rank):
def __init__(self, symbol='9'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Nine.__name__
+
class Ten(Rank):
def __init__(self, symbol='10'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Ten.__name__
+
class Jack(Rank):
def __init__(self, symbol='J'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Jack.__name__
+
class Queen(Rank):
def __init__(self, symbol='Q'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return Queen.__name__
+
class King(Rank):
def __init__(self, symbol='K'):
- self.symbol = suit
+ self.symbol = symbol
+ def __str__(self):
+ '''Returns string representation of the rank of the card'''
+ return King.__name__
+
class Suit:
'''
The class holds the the representation of the suit of the card aka their color and ect
'''
color = None # цветът на боята ('red', 'black')
def __init__(self, color=None):
self.color = colorcolor
def __str__(self):
'''Returns string representation of the suit of the card'''
return self.__name__
class Diamonds(Suit):
def __init__(self, color='red'):
self.color = suit
class Hearts(Suit):
def __init__(self, color='red'):
self.color = suit
class Spades(Suit):
def __init__(self, color='black'):
self.color = suit
class Clubs(Suit):
def __init__(self, color='black'):
self.color = suit
RANKS = {
'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
'Two': Two, 'Seven': Seven, 'Nine': Nine}
SUITS = {'Diamonds': Diamonds, 'Hearts':
Hearts, 'Spades': Spades, 'Clubs': Clubs}
class Card(object):
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
def __init__(self, rank, suit):
self.rank = RANKS[rank]
self.suit = SUITS[suit]
def __str__(self):
return '%s of %s' % (str(RANKS[rank]),
str(SUITS[suit]))
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
deck: list of Card objects- they are itterable
'''
deck = []
def __init__(self):
- self.deck = []
for suit in SUITS.keys():
for rank in RANKS.keys():
card = Card(SUITS[suit], RANKS[rank])
- self.deck.append(card)
+ CardCollection.deck.append(card)
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
def draw(self, index):
return deck[index]
def draw_from_top(self):
return deck.remove(0)
def draw_from_bottom(self):
return deck.remove(len(deck) - 1)
def top_card(self):
return deck[0]
def bottom_card(self):
return deck[len(deck) - 1]
def add(self, card):
deck.add(card)
def index(self, card):
return deck[index]
def StandardDeck(self):
return deck
def BeloteDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
deck = CardCollection()
return deck
def SixtySixDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
deck = CardCollection()
return deck

Йончо обнови решението на 26.03.2014 16:33 (преди около 10 години)

class Rank:
'''
The class holds the the representation of the rank of the cards aka their power
'''
- symbol = None # символът на картата (A, 2, Q, K)
-
def __init__(self, symbol=None):
- self.symbol = suit
+ self.symbol = symbol
- def __str__(self):
- '''Returns string representation of the rank of the card'''
- return Rank.__name__
-
class Ace(Rank):
def __init__(self, symbol='A'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Ace.__name__
class Two(Rank):
def __init__(self, symbol='2'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Two.__name__
class Three(Rank):
def __init__(self, symbol='3'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Three.__name__
class Four(Rank):
def __init__(self, symbol='4'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Four.__name__
class Five(Rank):
def __init__(self, symbol='5'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Five.__name__
class Six(Rank):
def __init__(self, symbol='6'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Six.__name__
class Seven(Rank):
def __init__(self, symbol='7'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Seven.__name__
class Eight(Rank):
def __init__(self, symbol='8'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Eight.__name__
class Nine(Rank):
def __init__(self, symbol='9'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Nine.__name__
class Ten(Rank):
def __init__(self, symbol='10'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Ten.__name__
class Jack(Rank):
def __init__(self, symbol='J'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Jack.__name__
class Queen(Rank):
def __init__(self, symbol='Q'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return Queen.__name__
class King(Rank):
def __init__(self, symbol='K'):
self.symbol = symbol
def __str__(self):
- '''Returns string representation of the rank of the card'''
return King.__name__
class Suit:
'''
The class holds the the representation of the suit of the card aka their color and ect
'''
- color = None # цветът на боята ('red', 'black')
def __init__(self, color=None):
self.color = colorcolor
- def __str__(self):
- '''Returns string representation of the suit of the card'''
- return self.__name__
-
class Diamonds(Suit):
def __init__(self, color='red'):
- self.color = suit
+ self.color = color
+ def __str__(self):
+ return Diamonds.__name__
+
class Hearts(Suit):
def __init__(self, color='red'):
- self.color = suit
+ self.color = color
+ def __str__(self):
+ return Hearts.__name__
+
class Spades(Suit):
def __init__(self, color='black'):
- self.color = suit
+ self.color = color
+ def __str__(self):
+ return Spades.__name__
+
class Clubs(Suit):
def __init__(self, color='black'):
- self.color = suit
+ self.color = color
+ def __str__(self):
+ return Clubs.__name__
+
RANKS = {
- 'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
- 'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
- 'Two': Two, 'Seven': Seven, 'Nine': Nine}
+ 'King': King(), 'Six': Six(), 'Jack': Jack(), 'Five': Five(), 'Queen': Queen(),
+ 'Ten': Ten(), 'Ace': Ace(), 'Three': Three(), 'Eight': Eight(), 'Four': Four(),
+ 'Two': Two(), 'Seven': Seven(), 'Nine': Nine()}
-SUITS = {'Diamonds': Diamonds, 'Hearts':
- Hearts, 'Spades': Spades, 'Clubs': Clubs}
+SUITS = {'Diamonds': Diamonds(), 'Hearts':
+ Hearts(), 'Spades': Spades(), 'Clubs': Clubs()}
-class Card(object):
+class Card:
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
- def __init__(self, rank, suit):
+ def __init__(self, rank=None, suit=None):
self.rank = RANKS[rank]
self.suit = SUITS[suit]
def __str__(self):
- return '%s of %s' % (str(RANKS[rank]),
- str(SUITS[suit]))
+ return str(self.rank) + ' of ' + str(self.suit)
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
- deck: list of Card objects- they are itterable
- '''
- deck = []
+ deck: list of Card objects- they are itterable'''
- def __init__(self):
- for suit in SUITS.keys():
- for rank in RANKS.keys():
- card = Card(SUITS[suit], RANKS[rank])
- CardCollection.deck.append(card)
+ def __init__(self, deck=[]):
+ [SimpleClass(count) for count in xrange(4)]
+ self.deck = [Card(rank, suit)
+ for suit in SUITS.keys() for rank in RANKS.keys()]
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
def draw(self, index):
return deck[index]
def draw_from_top(self):
- return deck.remove(0)
+ top = self.deck[0]
+ self.deck.remove(0)
+ return top
def draw_from_bottom(self):
- return deck.remove(len(deck) - 1)
+ bottom = self.deck[len(deck) - 1]
+ self.deck.remove(len(deck) - 1)
+ return bottom
def top_card(self):
- return deck[0]
+ top = self.deck.index(0)
+ return top
def bottom_card(self):
- return deck[len(deck) - 1]
+ bottom = self.deck.index(len(deck) - 1)
+ return bottom
def add(self, card):
- deck.add(card)
+ self.deck.add(card)
def index(self, card):
- return deck[index]
+ i = self.deck.index(card)
+ return i
def StandardDeck(self):
- return deck
+ return self.deck
def BeloteDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
- deck = CardCollection()
- return deck
+ self.deck = CardCollection()
+ return self.deck
def SixtySixDeck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
- deck = CardCollection()
- return deck
+ self.deck = CardCollection()
+ return self.deck

Йончо обнови решението на 26.03.2014 16:47 (преди около 10 години)

class Rank:
'''
The class holds the the representation of the rank of the cards aka their power
'''
def __init__(self, symbol=None):
self.symbol = symbol
class Ace(Rank):
def __init__(self, symbol='A'):
self.symbol = symbol
def __str__(self):
return Ace.__name__
class Two(Rank):
def __init__(self, symbol='2'):
self.symbol = symbol
def __str__(self):
return Two.__name__
class Three(Rank):
def __init__(self, symbol='3'):
self.symbol = symbol
def __str__(self):
return Three.__name__
class Four(Rank):
def __init__(self, symbol='4'):
self.symbol = symbol
def __str__(self):
return Four.__name__
class Five(Rank):
def __init__(self, symbol='5'):
self.symbol = symbol
def __str__(self):
return Five.__name__
class Six(Rank):
def __init__(self, symbol='6'):
self.symbol = symbol
def __str__(self):
return Six.__name__
class Seven(Rank):
def __init__(self, symbol='7'):
self.symbol = symbol
def __str__(self):
return Seven.__name__
class Eight(Rank):
def __init__(self, symbol='8'):
self.symbol = symbol
def __str__(self):
return Eight.__name__
class Nine(Rank):
def __init__(self, symbol='9'):
self.symbol = symbol
def __str__(self):
return Nine.__name__
class Ten(Rank):
def __init__(self, symbol='10'):
self.symbol = symbol
def __str__(self):
return Ten.__name__
class Jack(Rank):
def __init__(self, symbol='J'):
self.symbol = symbol
def __str__(self):
return Jack.__name__
class Queen(Rank):
def __init__(self, symbol='Q'):
self.symbol = symbol
def __str__(self):
return Queen.__name__
class King(Rank):
def __init__(self, symbol='K'):
self.symbol = symbol
def __str__(self):
return King.__name__
class Suit:
'''
The class holds the the representation of the suit of the card aka their color and ect
'''
def __init__(self, color=None):
self.color = colorcolor
class Diamonds(Suit):
def __init__(self, color='red'):
self.color = color
def __str__(self):
return Diamonds.__name__
class Hearts(Suit):
def __init__(self, color='red'):
self.color = color
def __str__(self):
return Hearts.__name__
class Spades(Suit):
def __init__(self, color='black'):
self.color = color
def __str__(self):
return Spades.__name__
class Clubs(Suit):
def __init__(self, color='black'):
self.color = color
def __str__(self):
return Clubs.__name__
RANKS = {
'King': King(), 'Six': Six(), 'Jack': Jack(), 'Five': Five(), 'Queen': Queen(),
'Ten': Ten(), 'Ace': Ace(), 'Three': Three(), 'Eight': Eight(), 'Four': Four(),
'Two': Two(), 'Seven': Seven(), 'Nine': Nine()}
SUITS = {'Diamonds': Diamonds(), 'Hearts':
Hearts(), 'Spades': Spades(), 'Clubs': Clubs()}
-class Card:
+class Card(object):
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
- def __init__(self, rank=None, suit=None):
- self.rank = RANKS[rank]
- self.suit = SUITS[suit]
+ def __init__(self, Rank, Suit):
+ self.rank = Rank
+ self.suit = Suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
deck: list of Card objects- they are itterable'''
def __init__(self, deck=[]):
- [SimpleClass(count) for count in xrange(4)]
self.deck = [Card(rank, suit)
- for suit in SUITS.keys() for rank in RANKS.keys()]
+ for suit in SUITS.values() for rank in RANKS.values()]
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
def draw(self, index):
return deck[index]
def draw_from_top(self):
top = self.deck[0]
self.deck.remove(0)
return top
def draw_from_bottom(self):
bottom = self.deck[len(deck) - 1]
self.deck.remove(len(deck) - 1)
return bottom
def top_card(self):
top = self.deck.index(0)
return top
def bottom_card(self):
bottom = self.deck.index(len(deck) - 1)
return bottom
def add(self, card):
- self.deck.add(card)
+ self.deck.append(card)
def index(self, card):
i = self.deck.index(card)
return i
- def StandardDeck(self):
+ def standard_deck(self):
return self.deck
- def BeloteDeck(self):
+ def belote_deck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
- self.deck = CardCollection()
return self.deck
- def SixtySixDeck(self):
+ def sixty_six_deck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
- self.deck = CardCollection()
return self.deck

Йончо обнови решението на 26.03.2014 16:58 (преди около 10 години)

class Rank:
'''
The class holds the the representation of the rank of the cards aka their power
'''
def __init__(self, symbol=None):
self.symbol = symbol
class Ace(Rank):
def __init__(self, symbol='A'):
self.symbol = symbol
def __str__(self):
return Ace.__name__
class Two(Rank):
def __init__(self, symbol='2'):
self.symbol = symbol
def __str__(self):
return Two.__name__
class Three(Rank):
def __init__(self, symbol='3'):
self.symbol = symbol
def __str__(self):
return Three.__name__
class Four(Rank):
def __init__(self, symbol='4'):
self.symbol = symbol
def __str__(self):
return Four.__name__
class Five(Rank):
def __init__(self, symbol='5'):
self.symbol = symbol
def __str__(self):
return Five.__name__
class Six(Rank):
def __init__(self, symbol='6'):
self.symbol = symbol
def __str__(self):
return Six.__name__
class Seven(Rank):
def __init__(self, symbol='7'):
self.symbol = symbol
def __str__(self):
return Seven.__name__
class Eight(Rank):
def __init__(self, symbol='8'):
self.symbol = symbol
def __str__(self):
return Eight.__name__
class Nine(Rank):
def __init__(self, symbol='9'):
self.symbol = symbol
def __str__(self):
return Nine.__name__
class Ten(Rank):
def __init__(self, symbol='10'):
self.symbol = symbol
def __str__(self):
return Ten.__name__
class Jack(Rank):
def __init__(self, symbol='J'):
self.symbol = symbol
def __str__(self):
return Jack.__name__
class Queen(Rank):
def __init__(self, symbol='Q'):
self.symbol = symbol
def __str__(self):
return Queen.__name__
class King(Rank):
def __init__(self, symbol='K'):
self.symbol = symbol
def __str__(self):
return King.__name__
class Suit:
'''
The class holds the the representation of the suit of the card aka their color and ect
'''
def __init__(self, color=None):
self.color = colorcolor
class Diamonds(Suit):
def __init__(self, color='red'):
self.color = color
def __str__(self):
return Diamonds.__name__
class Hearts(Suit):
def __init__(self, color='red'):
self.color = color
def __str__(self):
return Hearts.__name__
class Spades(Suit):
def __init__(self, color='black'):
self.color = color
def __str__(self):
return Spades.__name__
class Clubs(Suit):
def __init__(self, color='black'):
self.color = color
def __str__(self):
return Clubs.__name__
RANKS = {
'King': King(), 'Six': Six(), 'Jack': Jack(), 'Five': Five(), 'Queen': Queen(),
'Ten': Ten(), 'Ace': Ace(), 'Three': Three(), 'Eight': Eight(), 'Four': Four(),
'Two': Two(), 'Seven': Seven(), 'Nine': Nine()}
SUITS = {'Diamonds': Diamonds(), 'Hearts':
Hearts(), 'Spades': Spades(), 'Clubs': Clubs()}
class Card(object):
'''Represents a standard playing card.
Attributes:
suits-dictionary of suits
ranks-dictionary of ranks
'''
def __init__(self, Rank, Suit):
self.rank = Rank
self.suit = Suit
def __str__(self):
return str(self.rank) + ' of ' + str(self.suit)
def __cmp__(self, other):
'''simple function
'''
class CardCollection:
'''Represents a deck of cards.
Attributes:
deck: list of Card objects- they are itterable'''
def __init__(self, deck=[]):
self.deck = [Card(rank, suit)
for suit in SUITS.values() for rank in RANKS.values()]
def __str__(self):
res = []
for card in self.deck:
res.append(str(card))
return '\n'.join(res)
+ def __iter__(self):
+ for card in deck:
+ yield card
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __getattr__(self, key):
+ if key == 'rank':
+ return self.rank
+ elif key == 'suit':
+ return self.suit
+ raise AttributeError(key)
+
def draw(self, index):
return deck[index]
def draw_from_top(self):
- top = self.deck[0]
- self.deck.remove(0)
- return top
+ return self.deck.pop(0)
def draw_from_bottom(self):
- bottom = self.deck[len(deck) - 1]
- self.deck.remove(len(deck) - 1)
- return bottom
+ return self.deck.pop(len(deck) - 1)
def top_card(self):
- top = self.deck.index(0)
- return top
+ return self.deck.pop(0)
def bottom_card(self):
- bottom = self.deck.index(len(deck) - 1)
- return bottom
+ return self.deck.pop(len(deck) - 1)
def add(self, card):
self.deck.append(card)
def index(self, card):
i = self.deck.index(card)
return i
def standard_deck(self):
return self.deck
def belote_deck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
return self.deck
def sixty_six_deck(self):
RANKS.remove('Two')
RANKS.remove('Three')
RANKS.remove('Four')
RANKS.remove('Five')
RANKS.remove('Six')
RANKS.remove('Seven')
RANKS.remove('Eight')
return self.deck