Решение на Тесте карти от Атанас Пройчев

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

Към профила на Атанас Пройчев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 16 успешни тест(а)
  • 0 неуспешни тест(а)

Код

from collections import deque
from collections import OrderedDict
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
def __str__(self):
return 'King'
class Queen(Rank):
def __init__(self):
Rank.__init__(self, 'Q')
def __str__(self):
return 'Queen'
class Jack(Rank):
def __init__(self):
Rank.__init__(self, 'J')
def __str__(self):
return 'Jack'
class Ten(Rank):
def __init__(self):
Rank.__init__(self, '10')
def __str__(self):
return 'Ten'
class Nine(Rank):
def __init__(self):
Rank.__init__(self, '9')
def __str__(self):
return 'Nine'
class Eight(Rank):
def __init__(self):
Rank.__init__(self, '8')
def __str__(self):
return 'Eight'
class Seven(Rank):
def __init__(self):
Rank.__init__(self, '7')
def __str__(self):
return 'Seven'
class Six(Rank):
def __init__(self):
Rank.__init__(self, '6')
def __str__(self):
return 'Six'
class Five(Rank):
def __init__(self):
Rank.__init__(self, '5')
def __str__(self):
return 'Five'
class Four(Rank):
def __init__(self):
Rank.__init__(self, '4')
def __str__(self):
return 'Four'
class Three(Rank):
def __init__(self):
Rank.__init__(self, '3')
def __str__(self):
return 'Three'
class Two(Rank):
def __init__(self):
Rank.__init__(self, '2')
def __str__(self):
return 'Two'
class Ace(Rank):
def __init__(self):
Rank.__init__(self, 'A')
def __str__(self):
return 'Ace'
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color and self.name == other.name
class Diamonds(Suit):
name = 'Diamonds'
def __init__(self):
Suit.__init__(self, 'red')
def __str__(self):
return 'Diamonds'
class Hearts(Suit):
name = 'Hearts'
def __init__(self):
Suit.__init__(self, 'red')
def __str__(self):
return 'Hearts'
class Spades(Suit):
name = 'Spades'
def __init__(self):
Suit.__init__(self, 'black')
def __str__(self):
return 'Spades'
class Clubs(Suit):
name = 'Clubs'
def __init__(self):
Suit.__init__(self, 'black')
def __str__(self):
return 'Clubs'
RANKS = OrderedDict({'King': King, 'Queen': Queen, 'Jack': Jack,
'Ten': Ten, 'Nine': Nine, 'Eight': Eight,
'Seven': Seven, 'Six': Six, 'Five': Five,
'Four': Four, 'Three': Three, 'Two': Two, 'Ace': Ace})
SUITS = OrderedDict({'Diamonds': Diamonds, 'Clubs': Clubs,
'Hearts': Hearts, 'Spades': Spades})
class Card:
def __init__(self, rank, suit):
self.rank = rank()
self.suit = suit()
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return "{0} of {1}".format(self.rank, self.suit)
class CardCollection:
def __init__(self, collection = []):
self.collection = deque(collection)
def add(self, card):
self.collection.append(card)
def __getitem__(self, index):
if index >= len(self.collection):
raise IndexError("list index out of range")
return self.collection[index]
def __iter__(self):
return self
def __next__(self):
if len(self.collection) > 0:
return self.collection.popleft()
raise StopIteration
def draw(self, index):
self.collection.rotate(-index)
item = self.collection.popleft()
self.collection.rotate(index)
return item
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.popleft()
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def index(self, searched_card):
for idx, card in enumerate(self.collection):
if card == searched_card:
return idx
raise ValueError("{0} is not in list".format(str(card)))
def __len__(self):
return len(self.collection)
def Deck(type):
if type == 'Standard':
start = 13
elif type == 'Belote':
start = 7
else:
start = 5
cards = []
suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
print(suits)
print(SUITS)
for suit in suits:
for index, rank in enumerate(ranks):
if index < start or index > 11:
cards.append(Card(RANKS[rank], SUITS[suit]))
# print(str(Card(rank, suit)))
return cards
def StandardDeck():
return CardCollection(Deck('Standard'))
def BeloteDeck():
return CardCollection(Deck('Belote'))
def SixtySixDeck():
return CardCollection(Deck('SixtySix'))

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.026s

OK

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

Атанас обнови решението на 26.03.2014 16:21 (преди над 10 години)

+from collections import deque
+from collections import OrderedDict
+
+
+class Rank:
+
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class King(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __str__(self):
+ return 'King'
+
+
+class Queen(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __str__(self):
+ return 'Queen'
+
+
+class Jack(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __str__(self):
+ return 'Jack'
+
+
+class Ten(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __str__(self):
+ return 'Ten'
+
+
+class Nine(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __str__(self):
+ return 'Nine'
+
+
+class Eight(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __str__(self):
+ return 'Eight'
+
+
+class Seven(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __str__(self):
+ return 'Seven'
+
+
+class Six(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __str__(self):
+ return 'Six'
+
+
+class Five(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __str__(self):
+ return 'Five'
+
+
+class Four(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __str__(self):
+ return 'Four'
+
+
+class Three(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __str__(self):
+ return 'Three'
+
+
+class Two(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __str__(self):
+ return 'Two'
+
+
+class Ace(Rank):
+
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __str__(self):
+ return 'Ace'
+
+
+class Suit:
+
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color and self.name == other.name
+
+
+class Diamonds(Suit):
+
+ name = 'Diamonds'
+
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __str__(self):
+ return 'Diamonds'
+
+
+class Hearts(Suit):
+
+ name = 'Hearts'
+
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __str__(self):
+ return 'Hearts'
+
+
+class Spades(Suit):
+
+ name = 'Spades'
+
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __str__(self):
+ return 'Spades'
+
+
+class Clubs(Suit):
+
+ name = 'Clubs'
+
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __str__(self):
+ return 'Clubs'
+
+RANKS = OrderedDict({'King': King, 'Queen': Queen, 'Jack': Jack,
+ 'Ten': Ten, 'Nine': Nine, 'Eight': Eight,
+ 'Seven': Seven, 'Six': Six, 'Five': Five,
+ 'Four': Four, 'Three': Three, 'Two': Two, 'Ace': Ace})
+SUITS = OrderedDict({'Diamonds': Diamonds, 'Clubs': Clubs,
+ 'Hearts': Hearts, 'Spades': Spades})
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return "{0} of {1}".format(self.rank, self.suit)
+
+
+class CardCollection:
+
+ def __init__(self, collection = []):
+ self.collection = deque(collection)
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def __getitem__(self, index):
+ if index >= len(self.collection):
+ raise IndexError("list index out of range")
+ return self.collection[index]
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if len(self.collection) > 0:
+ return self.collection.popleft()
+ raise StopIteration
+
+ def draw(self, index):
+ self.collection.rotate(-index)
+ item = self.collection.popleft()
+ self.collection.rotate(index)
+ return item
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.popleft()
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def index(self, searched_card):
+ for idx, card in enumerate(self.collection):
+ if card == searched_card:
+ return idx
+
+ raise ValueError("{0} is not in list".format(str(card)))
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def Deck(type):
+ if type == 'Standard':
+ start = 13
+ elif type == 'Belote':
+ start = 7
+ else:
+ start = 5
+ cards = []
+ suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+ print(suits)
+ print(SUITS)
+ for suit in suits:
+ for index, rank in enumerate(ranks):
+ if index < start or index > 11:
+ cards.append(Card(RANKS[rank], SUITS[suit]))
+ # print(str(Card(rank, suit)))
+
+ return cards
+
+
+def StandardDeck():
+ return CardCollection(Deck('Standard'))
+
+
+def BeloteDeck():
+ return CardCollection(Deck('Belote'))
+
+
+def SixtySixDeck():
+ return CardCollection(Deck('SixtySix'))