Решение на Тесте карти от Божидар Карааргиров

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

Към профила на Божидар Карааргиров

Резултати

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

Код

from collections import deque
ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6',
'Seven': '7', 'Eight': '8', 'Nine': '9', 'Ten': '10', 'Jack': 'J',
'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
suits = {'Hearts': 'red', 'Diamonds': 'red',
'Clubs': 'black', 'Spades': 'black'}
order_of_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
order_of_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
class Rank(object):
rank = ""
symbol = ""
def __eq__(self, other):
return self.rank == other.rank
def __str__(self):
return self.rank
class Suit(object):
suit = ""
color = ""
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.suit
class Card(object):
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return "{0} of {1}".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection(object):
def __init__(self, collection):
self.deck = deque(collection)
def draw(self, index):
if index > len(self.deck):
raise IndexError
counter_cards = deque()
for i in range(index + 1):
counter_cards.append(self.deck.pop())
while(len(counter_cards) != 1):
self.deck.append(counter_cards.popleft())
return counter_cards.pop()
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.popleft()
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
for i in range(len(self.deck)):
if self.deck[i] == card:
return i
raise ValueError
def __getitem__(self, i):
return self.deck.__getitem__(i)
def __iter__(self):
return self.deck.__iter__()
def _create_rank_class(ranks, symb):
return type(ranks, (Rank,), dict(rank=ranks, symbol=symb))
def _create_suit_class(suits, colors):
return type(suits, (Suit,), dict(suit=suits, color=colors))
def _create_card_class(rank, suit):
return Card(rank, suit)
RANKS = {rank: _create_rank_class(rank, ranks[rank]) for rank in ranks.keys()}
SUITS = {suit: _create_suit_class(suit, suits[suit]) for suit in suits.keys()}
def StandardDeck():
standard_deck = []
for suit in order_of_suits:
for rank in order_of_ranks:
standard_deck.append(_create_card_class(RANKS[rank], SUITS[suit]))
return standard_deck
def BeloteDeck():
return [_create_card_class(RANKS[rank], SUITS[suit])
for suit in order_of_suits for rank in order_of_ranks
if int(ranks[rank], 32) not in range(2, 7)]
def SixtySixDeck():
return [_create_card_class(RANKS[rank], SUITS[suit])
for suit in order_of_suits for rank in order_of_ranks
if int(ranks[rank], 32) not in range(2, 9)]

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

.EEE.E..........
======================================================================
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-1pf8cpy/test.py", line 117, in test_deck_add
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

======================================================================
ERROR: 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-1pf8cpy/test.py", line 142, in test_deck_draw
    self.assertEqual(len(deck), 51)
TypeError: object of type 'CardCollection' has no len()

======================================================================
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-1pf8cpy/test.py", line 154, in test_deck_index
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

======================================================================
ERROR: 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-1pf8cpy/test.py", line 129, in test_deck_order
    deck = solution.CardCollection()
TypeError: __init__() missing 1 required positional argument: 'collection'

----------------------------------------------------------------------
Ran 16 tests in 0.020s

FAILED (errors=4)

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

Божидар обнови решението на 26.03.2014 11:33 (преди над 10 години)

+from collections import deque
+
+ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6',
+ 'Seven': '7', 'Eight': '8', 'Nine': '9', 'Ten': '10', 'Jack': 'J',
+ 'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
+suits = {'Hearts': 'red', 'Diamonds': 'red',
+ 'Clubs': 'black', 'Spades': 'black'}
+order_of_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+order_of_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+class Rank(object):
+ rank = ""
+ symbol = ""
+
+ def __eq__(self, other):
+ return self.rank == other.rank
+
+ def __str__(self):
+ return self.rank
+
+
+class Suit(object):
+ suit = ""
+ color = ""
+
+ def __eq__(self, other):
+ return self.suit == other.suit
+
+ def __str__(self):
+ return self.suit
+
+
+class Card(object):
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return "{0} of {1}".format(self.rank, self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection(object):
+ def __init__(self, collection):
+ self.deck = deque(collection)
+
+ def draw(self, index):
+ if index > len(self.deck):
+ raise IndexError
+
+ counter_cards = deque()
+ for i in range(index + 1):
+ counter_cards.append(self.deck.pop())
+
+ while(len(counter_cards) != 1):
+ self.deck.append(counter_cards.popleft())
+ return counter_cards.pop()
+
+ def draw_from_top(self):
+ return self.deck.pop()
+
+ def draw_from_bottom(self):
+ return self.deck.popleft()
+
+ def top_card(self):
+ return self.deck[-1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ for i in range(len(self.deck)):
+ if self.deck[i] == card:
+ return i
+ raise ValueError
+
+ def __getitem__(self):
+ return self.deck.__getitem__()
+
+ def __iter__(self):
+ return self.deck.__iter__()
+
+
+def _create_rank_class(ranks, symb):
+ return type(ranks, (Rank,), dict(rank=ranks, symbol=symb))
+
+
+def _create_suit_class(suits, colors):
+ return type(suits, (Suit,), dict(suit=suits, color=colors))
+
+
+def _create_card_class(rank, suit):
+ return Card(rank, suit)
+
+
+RANKS = {rank: _create_rank_class(rank, ranks[rank]) for rank in ranks.keys()}
+SUITS = {suit: _create_suit_class(suit, suits[suit]) for suit in suits.keys()}
+
+
+def StandardDeck():
+ standard_deck = []
+ for suit in order_of_suits:
+ for rank in order_of_ranks:
+ standard_deck.append(_create_card_class(RANKS[rank], SUITS[suit]))
+
+ return standard_deck
+
+
+def BeloteDeck():
+ return [_create_card_class(RANKS[rank], SUITS[suit])
+ for suit in order_of_suits for rank in order_of_ranks
+ if int(ranks[rank], 32) not in range(2, 7)]
+
+
+def SixtySixDeck():
+ return [_create_card_class(RANKS[rank], SUITS[suit])
+ for suit in order_of_suits for rank in order_of_ranks
+ if int(ranks[rank], 32) not in range(2, 9)]

Божидар обнови решението на 26.03.2014 12:41 (преди над 10 години)

from collections import deque
ranks = {'Two': '2', 'Three': '3', 'Four': '4', 'Five': '5', 'Six': '6',
'Seven': '7', 'Eight': '8', 'Nine': '9', 'Ten': '10', 'Jack': 'J',
'Queen': 'Q', 'King': 'K', 'Ace': 'A'}
suits = {'Hearts': 'red', 'Diamonds': 'red',
'Clubs': 'black', 'Spades': 'black'}
order_of_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
order_of_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
class Rank(object):
rank = ""
symbol = ""
def __eq__(self, other):
return self.rank == other.rank
def __str__(self):
return self.rank
class Suit(object):
suit = ""
color = ""
def __eq__(self, other):
return self.suit == other.suit
def __str__(self):
return self.suit
class Card(object):
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __str__(self):
return "{0} of {1}".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection(object):
def __init__(self, collection):
self.deck = deque(collection)
def draw(self, index):
if index > len(self.deck):
raise IndexError
counter_cards = deque()
for i in range(index + 1):
counter_cards.append(self.deck.pop())
while(len(counter_cards) != 1):
self.deck.append(counter_cards.popleft())
return counter_cards.pop()
def draw_from_top(self):
return self.deck.pop()
def draw_from_bottom(self):
return self.deck.popleft()
def top_card(self):
return self.deck[-1]
def bottom_card(self):
return self.deck[0]
def add(self, card):
self.deck.append(card)
def index(self, card):
for i in range(len(self.deck)):
if self.deck[i] == card:
return i
raise ValueError
- def __getitem__(self):
- return self.deck.__getitem__()
+ def __getitem__(self, i):
+ return self.deck.__getitem__(i)
def __iter__(self):
return self.deck.__iter__()
def _create_rank_class(ranks, symb):
return type(ranks, (Rank,), dict(rank=ranks, symbol=symb))
def _create_suit_class(suits, colors):
return type(suits, (Suit,), dict(suit=suits, color=colors))
def _create_card_class(rank, suit):
return Card(rank, suit)
RANKS = {rank: _create_rank_class(rank, ranks[rank]) for rank in ranks.keys()}
SUITS = {suit: _create_suit_class(suit, suits[suit]) for suit in suits.keys()}
def StandardDeck():
standard_deck = []
for suit in order_of_suits:
for rank in order_of_ranks:
standard_deck.append(_create_card_class(RANKS[rank], SUITS[suit]))
return standard_deck
def BeloteDeck():
return [_create_card_class(RANKS[rank], SUITS[suit])
for suit in order_of_suits for rank in order_of_ranks
if int(ranks[rank], 32) not in range(2, 7)]
def SixtySixDeck():
return [_create_card_class(RANKS[rank], SUITS[suit])
for suit in order_of_suits for rank in order_of_ranks
if int(ranks[rank], 32) not in range(2, 9)]