Решение на Тесте карти от Васил Тодоров

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

Към профила на Васил Тодоров

Резултати

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

Код

from functools import cmp_to_key
class Rank:
def __init__(self, symbol):
self._symbol = symbol
def symbol(self):
return self._symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Two(Rank):
def __init__(self):
Rank.__init__(self, "2")
class Three(Rank):
def __init__(self):
Rank.__init__(self, "3")
class Four(Rank):
def __init__(self):
Rank.__init__(self, "4")
class Five(Rank):
def __init__(self):
Rank.__init__(self, "5")
class Six(Rank):
def __init__(self):
Rank.__init__(self, "6")
class Seven(Rank):
def __init__(self):
Rank.__init__(self, "7")
class Eight(Rank):
def __init__(self):
Rank.__init__(self, "8")
class Nine(Rank):
def __init__(self):
Rank.__init__(self, "9")
class Ten(Rank):
def __init__(self):
Rank.__init__(self, "10")
class Jack(Rank):
def __init__(self):
Rank.__init__(self, "J")
class Queen(Rank):
def __init__(self):
Rank.__init__(self, "Q")
class King(Rank):
def __init__(self):
Rank.__init__(self, "K")
class Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
RANKS = {
"Two": Two().__class__, "Three": Three().__class__,
"Four": Four().__class__, "Five": Five().__class__,
"Six": Six().__class__, "Seven": Seven().__class__,
"Eight": Eight().__class__, "Nine": Nine().__class__,
"Ten": Ten().__class__, "Jack": Jack().__class__,
"Queen": Queen().__class__, "King": King().__class__,
"Ace": Ace().__class__
}
class Suit:
def __init__(self, color):
self._color = color
def color(self):
return self._color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, "red")
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
SUITS = {
"Hearts": Hearts().__class__, "Diamonds": Diamonds().__class__,
"Spades": Spades().__class__, "Clubs": Clubs().__class__
}
class Card:
def __init__(self, rank, suit):
self.__rank = rank()
self.__suit = suit()
@property
def rank(self):
return self.__rank
@property
def suit(self):
return self.__suit
def __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return str(self) == str(other)
def __repr__(self):
return "<Card " + str(self) + ">"
class CardCollection:
def __init__(self, collection=[]):
self._collection = collection
def draw(self, index):
card_on_index = self._collection[index]
self._collection.pop(index)
return card_on_index
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self._collection[-1]
def bottom_card(self):
return self._collection[1]
def add(self, card):
self._collection.append(card)
def index(self, card):
for i in range(0, len(self._collection)):
if(card == self._collection[i]):
return i
raise ValueError("<Card {0}> is not in list".format(str(card)))
def __getitem__(self, key):
return self._collection[key]
def __iter__(self):
return iter(self._collection)
def __str__(self):
return str(self._collection)
def __repr__(self):
return repr(self._collection)
def __len__(self):
return len(self._collection)
def card_comparer(card_one, card_two):
rank_priorities = {
RANKS["Ace"]: 1, RANKS["Two"]: 2, RANKS["Three"]: 3,
RANKS["Four"]: 4, RANKS["Five"]: 5, RANKS["Six"]: 6,
RANKS["Seven"]: 7, RANKS["Eight"]: 8, RANKS["Nine"]: 9,
RANKS["Ten"]: 10, RANKS["Jack"]: 11, RANKS["Queen"]: 12,
RANKS["King"]: 13
}
suit_priorities = {
SUITS["Diamonds"]: 4, SUITS["Clubs"]: 3,
SUITS["Hearts"]: 2, SUITS["Spades"]: 1
}
card_one_rank_value = rank_priorities[card_one.rank.__class__]
card_two_rank_value = rank_priorities[card_two.rank.__class__]
card_one_suit_value = suit_priorities[card_one.suit.__class__]
card_two_suit_value = suit_priorities[card_two.suit.__class__]
if card_one_suit_value < card_two_suit_value:
return 1
elif card_one_suit_value > card_two_suit_value:
return -1
if card_one_rank_value < card_two_rank_value:
return 1
elif card_one_rank_value > card_two_rank_value:
return -1
else:
return 0
def StandardDeck():
standard_deck = CardCollection([])
for rank in RANKS:
for suit in SUITS:
standard_deck.add(Card(RANKS[rank], SUITS[suit]))
standard_deck = sorted(standard_deck, key=cmp_to_key(card_comparer))
return standard_deck
def BeloteDeck():
cards_not_in_belote = {
"Two", "Three", "Four", "Five", "Six"
}
belote_deck = CardCollection([])
for rank in RANKS:
if rank not in cards_not_in_belote:
for suit in SUITS:
belote_deck.add(Card(RANKS[rank], SUITS[suit]))
belote_deck = sorted(belote_deck, key=cmp_to_key(card_comparer))
return belote_deck
def SixtySixDeck():
cards_not_in_sixty_six = {
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
}
sixty_six_deck = CardCollection([])
for rank in RANKS:
if rank not in cards_not_in_sixty_six:
for suit in SUITS:
sixty_six_deck.add(Card(RANKS[rank], SUITS[suit]))
sixty_six_deck = sorted(sixty_six_deck, key=cmp_to_key(card_comparer))
return sixty_six_deck

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

..FF.F..........
======================================================================
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-1b63z9g/test.py", line 145, in test_deck_draw
    self.assertEqual(card, deck.draw_from_bottom())
AssertionError: <Card Ace of Spades> != <Card Ace of Diamonds>

======================================================================
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-1b63z9g/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-1b63z9g/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <Card Four of Clubs> != <Card Two of Hearts>

----------------------------------------------------------------------
Ran 16 tests in 0.030s

FAILED (failures=3)

История (3 версии и 4 коментара)

Васил обнови решението на 23.03.2014 01:34 (преди над 10 години)

+from functools import cmp_to_key
+
+
+class Rank:
+ def __init__(self, symbol):
+ self._symbol = symbol
+
+ def symbol(self):
+ return self._symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, "2")
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, "3")
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, "4")
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, "5")
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, "6")
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, "7")
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, "8")
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, "9")
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, "10")
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, "J")
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Q")
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, "K")
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, "A")
+
+
+RANKS = {
+ "Two": Two().__class__, "Three": Three().__class__,
+ "Four": Four().__class__, "Five": Five().__class__,
+ "Six": Six().__class__, "Seven": Seven().__class__,
+ "Eight": Eight().__class__, "Nine": Nine().__class__,
+ "Ten": Ten().__class__, "Jack": Jack().__class__,
+ "Queen": Queen().__class__, "King": King().__class__,
+ "Ace": Ace().__class__
+}
+
+
+class Suit:
+ def __init__(self, color):
+ self._color = color
+
+ def color(self):
+ return self._color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+
+#Pika
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+#Spatia
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+SUITS = {
+ "Hearts": Hearts().__class__, "Diamonds": Diamonds().__class__,
+ "Spades": Spades().__class__, "Clubs": Clubs().__class__
+}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.__rank__ = rank
+ self.__suit__ = suit
+
+ @property
+ def rank(self):
+ return self.__rank__
+
+ @property
+ def suit(self):
+ return self.__suit__
+
+ def __str__(self):
+ return "{0} of {1}".format(str(self.rank()), str(self.suit()))
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __repr__(self):
+ return str(self)
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self._collection = collection
+
+ def draw(self, index):
+ card_on_index = self._collection[index]
+ self._collection.pop(index)
+ return card_on_index
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self._collection[-1]
+
+ def bottom_card(self):
+ return self._collection[1]
+
+ def add(self, card):
+ self._collection.append(card)
+
+ def index(self, card):
+ for i in range(0, len(self._collection)):
+ if(card == self._collection[i]):
+ return i
+ raise ValueError("<Card {0}> is not in list".format(str(card)))
+
+ def __getitem__(self, key):
+ return self._collection[key]
+
+ def __iter__(self):
+ return iter(self._collection)
+
+ def __str__(self):
+ return str(self._collection)
+
+ def __repr__(self):
+ return repr(self._collection)
+
+ def __len__(self):
+ return len(self._collection)
+
+
+def card_comparer(card_one, card_two):
+ rank_priorities = {
+ RANKS["Ace"]: 1, RANKS["Two"]: 2, RANKS["Three"]: 3,
+ RANKS["Four"]: 4, RANKS["Five"]: 5, RANKS["Six"]: 6,
+ RANKS["Seven"]: 7, RANKS["Eight"]: 8, RANKS["Nine"]: 9,
+ RANKS["Ten"]: 10, RANKS["Jack"]: 11, RANKS["Queen"]: 12,
+ RANKS["King"]: 13
+ }
+ suit_priorities = {
+ SUITS["Diamonds"]: 4, SUITS["Clubs"]: 3,
+ SUITS["Hearts"]: 2, SUITS["Spades"]: 1
+ }
+ if suit_priorities[card_one.suit] < suit_priorities[card_two.suit]:
+ return 1
+ elif suit_priorities[card_one.suit] > suit_priorities[card_two.suit]:
+ return -1
+ if rank_priorities[card_one.rank] < rank_priorities[card_two.rank]:
+ return 1
+ elif rank_priorities[card_one.rank] > rank_priorities[card_two.rank]:
+ return -1
+ else:
+ return 0
+
+
+def StandardDeck():
+ standard_deck = CardCollection([])
+ for rank in RANKS:
+ for suit in SUITS:
+ standard_deck.add(Card(RANKS[rank], SUITS[suit]))
+ standard_deck = sorted(standard_deck, key=cmp_to_key(card_comparer))
+ return standard_deck
+
+
+def BeloteDeck():
+ cards_not_in_belote = {
+ "Two", "Three", "Four", "Five", "Six"
+ }
+ belote_deck = CardCollection([])
+ for rank in RANKS:
+ if rank not in cards_not_in_belote:
+ for suit in SUITS:
+ belote_deck.add(Card(RANKS[rank], SUITS[suit]))
+ belote_deck = sorted(belote_deck, key=cmp_to_key(card_comparer))
+ return belote_deck
+
+
+def SixtySixDeck():
+ cards_not_in_sixty_six = {
+ "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
+ }
+ sixty_six_deck = CardCollection([])
+ for rank in RANKS:
+ if rank not in cards_not_in_sixty_six:
+ for suit in SUITS:
+ sixty_six_deck.add(Card(RANKS[rank], SUITS[suit]))
+ sixty_six_deck = sorted(sixty_six_deck, key=cmp_to_key(card_comparer))
+ return sixty_six_deck

Васил обнови решението на 23.03.2014 14:41 (преди над 10 години)

from functools import cmp_to_key
class Rank:
def __init__(self, symbol):
self._symbol = symbol
def symbol(self):
return self._symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Two(Rank):
def __init__(self):
Rank.__init__(self, "2")
class Three(Rank):
def __init__(self):
Rank.__init__(self, "3")
class Four(Rank):
def __init__(self):
Rank.__init__(self, "4")
class Five(Rank):
def __init__(self):
Rank.__init__(self, "5")
class Six(Rank):
def __init__(self):
Rank.__init__(self, "6")
class Seven(Rank):
def __init__(self):
Rank.__init__(self, "7")
class Eight(Rank):
def __init__(self):
Rank.__init__(self, "8")
class Nine(Rank):
def __init__(self):
Rank.__init__(self, "9")
class Ten(Rank):
def __init__(self):
Rank.__init__(self, "10")
class Jack(Rank):
def __init__(self):
Rank.__init__(self, "J")
class Queen(Rank):
def __init__(self):
Rank.__init__(self, "Q")
class King(Rank):
def __init__(self):
Rank.__init__(self, "K")
class Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
RANKS = {
"Two": Two().__class__, "Three": Three().__class__,
"Four": Four().__class__, "Five": Five().__class__,
"Six": Six().__class__, "Seven": Seven().__class__,
"Eight": Eight().__class__, "Nine": Nine().__class__,
"Ten": Ten().__class__, "Jack": Jack().__class__,
"Queen": Queen().__class__, "King": King().__class__,
"Ace": Ace().__class__
}
class Suit:
def __init__(self, color):
self._color = color
def color(self):
return self._color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, "red")
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
#Pika
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
#Spatia
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
SUITS = {
"Hearts": Hearts().__class__, "Diamonds": Diamonds().__class__,
"Spades": Spades().__class__, "Clubs": Clubs().__class__
}
class Card:
def __init__(self, rank, suit):
- self.__rank__ = rank
- self.__suit__ = suit
+ self.__rank__ = rank()
+ self.__suit__ = suit()
@property
def rank(self):
return self.__rank__
@property
def suit(self):
return self.__suit__
def __str__(self):
- return "{0} of {1}".format(str(self.rank()), str(self.suit()))
+ return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return str(self) == str(other)
def __repr__(self):
- return str(self)
+ return "<Card " + str(self) + ">"
class CardCollection:
def __init__(self, collection=[]):
self._collection = collection
def draw(self, index):
card_on_index = self._collection[index]
self._collection.pop(index)
return card_on_index
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self._collection[-1]
def bottom_card(self):
return self._collection[1]
def add(self, card):
self._collection.append(card)
def index(self, card):
for i in range(0, len(self._collection)):
if(card == self._collection[i]):
return i
raise ValueError("<Card {0}> is not in list".format(str(card)))
def __getitem__(self, key):
return self._collection[key]
def __iter__(self):
return iter(self._collection)
def __str__(self):
return str(self._collection)
def __repr__(self):
return repr(self._collection)
def __len__(self):
return len(self._collection)
def card_comparer(card_one, card_two):
rank_priorities = {
RANKS["Ace"]: 1, RANKS["Two"]: 2, RANKS["Three"]: 3,
RANKS["Four"]: 4, RANKS["Five"]: 5, RANKS["Six"]: 6,
RANKS["Seven"]: 7, RANKS["Eight"]: 8, RANKS["Nine"]: 9,
RANKS["Ten"]: 10, RANKS["Jack"]: 11, RANKS["Queen"]: 12,
RANKS["King"]: 13
}
suit_priorities = {
SUITS["Diamonds"]: 4, SUITS["Clubs"]: 3,
SUITS["Hearts"]: 2, SUITS["Spades"]: 1
}
- if suit_priorities[card_one.suit] < suit_priorities[card_two.suit]:
+ card_one_rank_value = rank_priorities[card_one.rank.__class__]
+ card_two_rank_value = rank_priorities[card_two.rank.__class__]
+ card_one_suit_value = suit_priorities[card_one.suit.__class__]
+ card_two_suit_value = suit_priorities[card_two.suit.__class__]
+ if card_one_suit_value < card_two_suit_value:
return 1
- elif suit_priorities[card_one.suit] > suit_priorities[card_two.suit]:
+ elif card_one_suit_value > card_two_suit_value:
return -1
- if rank_priorities[card_one.rank] < rank_priorities[card_two.rank]:
+ if card_one_rank_value < card_two_rank_value:
return 1
- elif rank_priorities[card_one.rank] > rank_priorities[card_two.rank]:
+ elif card_one_rank_value > card_two_rank_value:
return -1
else:
return 0
def StandardDeck():
standard_deck = CardCollection([])
for rank in RANKS:
for suit in SUITS:
standard_deck.add(Card(RANKS[rank], SUITS[suit]))
standard_deck = sorted(standard_deck, key=cmp_to_key(card_comparer))
return standard_deck
def BeloteDeck():
cards_not_in_belote = {
"Two", "Three", "Four", "Five", "Six"
}
belote_deck = CardCollection([])
for rank in RANKS:
if rank not in cards_not_in_belote:
for suit in SUITS:
belote_deck.add(Card(RANKS[rank], SUITS[suit]))
belote_deck = sorted(belote_deck, key=cmp_to_key(card_comparer))
return belote_deck
def SixtySixDeck():
cards_not_in_sixty_six = {
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
}
sixty_six_deck = CardCollection([])
for rank in RANKS:
if rank not in cards_not_in_sixty_six:
for suit in SUITS:
sixty_six_deck.add(Card(RANKS[rank], SUITS[suit]))
sixty_six_deck = sorted(sixty_six_deck, key=cmp_to_key(card_comparer))
return sixty_six_deck

Не, за мен за immutable клас е винаги е по-добре да използваш @property. Не е нужно да пречиш на хипотетичен лош програмист да ти бърника в класа :) (може и да не е толкоз лош пък и какво те интересува).

Васил обнови решението на 24.03.2014 23:15 (преди над 10 години)

from functools import cmp_to_key
class Rank:
def __init__(self, symbol):
self._symbol = symbol
def symbol(self):
return self._symbol
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Two(Rank):
def __init__(self):
Rank.__init__(self, "2")
class Three(Rank):
def __init__(self):
Rank.__init__(self, "3")
class Four(Rank):
def __init__(self):
Rank.__init__(self, "4")
class Five(Rank):
def __init__(self):
Rank.__init__(self, "5")
class Six(Rank):
def __init__(self):
Rank.__init__(self, "6")
class Seven(Rank):
def __init__(self):
Rank.__init__(self, "7")
class Eight(Rank):
def __init__(self):
Rank.__init__(self, "8")
class Nine(Rank):
def __init__(self):
Rank.__init__(self, "9")
class Ten(Rank):
def __init__(self):
Rank.__init__(self, "10")
class Jack(Rank):
def __init__(self):
Rank.__init__(self, "J")
class Queen(Rank):
def __init__(self):
Rank.__init__(self, "Q")
class King(Rank):
def __init__(self):
Rank.__init__(self, "K")
class Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
RANKS = {
"Two": Two().__class__, "Three": Three().__class__,
"Four": Four().__class__, "Five": Five().__class__,
"Six": Six().__class__, "Seven": Seven().__class__,
"Eight": Eight().__class__, "Nine": Nine().__class__,
"Ten": Ten().__class__, "Jack": Jack().__class__,
"Queen": Queen().__class__, "King": King().__class__,
"Ace": Ace().__class__
}
class Suit:
def __init__(self, color):
self._color = color
def color(self):
return self._color
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, "red")
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
-#Pika
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
-#Spatia
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
SUITS = {
"Hearts": Hearts().__class__, "Diamonds": Diamonds().__class__,
"Spades": Spades().__class__, "Clubs": Clubs().__class__
}
class Card:
def __init__(self, rank, suit):
- self.__rank__ = rank()
- self.__suit__ = suit()
+ self.__rank = rank()
+ self.__suit = suit()
@property
def rank(self):
- return self.__rank__
+ return self.__rank
@property
def suit(self):
- return self.__suit__
+ return self.__suit
def __str__(self):
return "{0} of {1}".format(str(self.rank), str(self.suit))
def __eq__(self, other):
return str(self) == str(other)
def __repr__(self):
return "<Card " + str(self) + ">"
class CardCollection:
def __init__(self, collection=[]):
self._collection = collection
def draw(self, index):
card_on_index = self._collection[index]
self._collection.pop(index)
return card_on_index
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
def top_card(self):
return self._collection[-1]
def bottom_card(self):
return self._collection[1]
def add(self, card):
self._collection.append(card)
def index(self, card):
for i in range(0, len(self._collection)):
if(card == self._collection[i]):
return i
raise ValueError("<Card {0}> is not in list".format(str(card)))
def __getitem__(self, key):
return self._collection[key]
def __iter__(self):
return iter(self._collection)
def __str__(self):
return str(self._collection)
def __repr__(self):
return repr(self._collection)
def __len__(self):
return len(self._collection)
def card_comparer(card_one, card_two):
rank_priorities = {
RANKS["Ace"]: 1, RANKS["Two"]: 2, RANKS["Three"]: 3,
RANKS["Four"]: 4, RANKS["Five"]: 5, RANKS["Six"]: 6,
RANKS["Seven"]: 7, RANKS["Eight"]: 8, RANKS["Nine"]: 9,
RANKS["Ten"]: 10, RANKS["Jack"]: 11, RANKS["Queen"]: 12,
RANKS["King"]: 13
}
suit_priorities = {
SUITS["Diamonds"]: 4, SUITS["Clubs"]: 3,
SUITS["Hearts"]: 2, SUITS["Spades"]: 1
}
card_one_rank_value = rank_priorities[card_one.rank.__class__]
card_two_rank_value = rank_priorities[card_two.rank.__class__]
card_one_suit_value = suit_priorities[card_one.suit.__class__]
card_two_suit_value = suit_priorities[card_two.suit.__class__]
if card_one_suit_value < card_two_suit_value:
return 1
elif card_one_suit_value > card_two_suit_value:
return -1
if card_one_rank_value < card_two_rank_value:
return 1
elif card_one_rank_value > card_two_rank_value:
return -1
else:
return 0
def StandardDeck():
standard_deck = CardCollection([])
for rank in RANKS:
for suit in SUITS:
standard_deck.add(Card(RANKS[rank], SUITS[suit]))
standard_deck = sorted(standard_deck, key=cmp_to_key(card_comparer))
return standard_deck
def BeloteDeck():
cards_not_in_belote = {
"Two", "Three", "Four", "Five", "Six"
}
belote_deck = CardCollection([])
for rank in RANKS:
if rank not in cards_not_in_belote:
for suit in SUITS:
belote_deck.add(Card(RANKS[rank], SUITS[suit]))
belote_deck = sorted(belote_deck, key=cmp_to_key(card_comparer))
return belote_deck
def SixtySixDeck():
cards_not_in_sixty_six = {
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
}
sixty_six_deck = CardCollection([])
for rank in RANKS:
if rank not in cards_not_in_sixty_six:
for suit in SUITS:
sixty_six_deck.add(Card(RANKS[rank], SUITS[suit]))
sixty_six_deck = sorted(sixty_six_deck, key=cmp_to_key(card_comparer))
return sixty_six_deck