Решение на Тесте карти от Димитър Чаушев

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

Към профила на Димитър Чаушев

Резултати

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

Код

import collections
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __str__(self):
return ("{0} of {1}").format(self.rank.__class__.__name__,\
self.suit.__class__.__name__)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
number_of_cards = 0
def __init__(self, collection = list()):
self.collection = collection
self.number_of_cards = len(collection)
def draw(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else:
raise ValueError("<Card {0}> is not in list".format(card))
def __str__(self):
if self.number_of_cards == 0:
return "No cards"
answer = "["
answer += "<Card {0}>".format(self.collection[0])
for i in range(1, self.number_of_cards):
answer += ", <Card {0}>".format(self.collection[i])
answer += "]"
return(answer)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
else:
return self.collection[index]
def __len__(self):
return self.number_of_cards
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')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
sixtySixUnnecessary = ['Seven', 'Eight']
RANKS = {"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, }
RANKS_ORDERED = (("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))
RANKS_ORDERED = collections.OrderedDict(RANKS_ORDERED)
SUITS = {"Diamonds": Diamonds, "Clubs":Clubs, "Hearts":Hearts, "Spades":Spades}
SUITS_ORDERED = (("Diamonds", Diamonds), ("Clubs", Clubs), ("Hearts", Hearts),\
("Spades", Spades))
SUITS_ORDERED = collections.OrderedDict(SUITS_ORDERED)
def StandardDeck():
l =[]
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
l.append(str(Card(RANKS[j], SUITS[i])))
return l
def BeloteDeck():
l = []
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
if j not in beloteUnnecessary:
l.append(str(Card(RANKS[j], SUITS[i])))
return l
def SixtySixDeck():
l = []
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
if j not in beloteUnnecessary and j not in sixtySixUnnecessary:
l.append(str(Card(RANKS[j], SUITS[i])))
return l

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

...F.F..........
======================================================================
FAIL: test_deck_index (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-1v19qwr/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-1v19qwr/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: <solution.Card object at 0xb7857c0c> != <solution.Card object at 0xb786ffac>

----------------------------------------------------------------------
Ran 16 tests in 0.022s

FAILED (failures=2)

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

Димитър обнови решението на 22.03.2014 18:33 (преди над 10 години)

+beloteExtra = ['Two', 'Three', 'Four', 'Five', 'Six']
+sixtySixExtra = ['Seven', 'Eight']
+
+class Rank:
+
+ def __eq__(self, other):
+ if self.__class__ == other.__class__:
+ return True
+ else: return False
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ if self.__class__ == other.__class__:
+ return True
+ else: return False
+
+
+class Card:
+ def __init__(self, rank, suit):
+ object.__setattr__(self, "rank", rank)
+ object.__setattr__(self, "suit", suit)
+
+ def __str__(self):
+ return "Card {0} of {1}".format((self.rank), (self.suit))
+
+ def __setattr__(self, *args):
+ return AttributeError("can't set attribute")
+
+
+class CardCollection:
+ collection = [0]*53
+ number_of_cards = 0
+
+ def __init__(self, collection):
+ for i in collection:
+ self.collection[self.number_of_cards] = i;
+ self.number_of_cards += 1
+
+ def draw(self, index):
+ if index >= self.number_of_cards:
+ return IndexError("list index out of range")
+ temporary = self.collection[index]
+ self.collection.remove(self.collection[index])
+ self.number_of_cards -= 1
+ return(temporary)
+
+ def draw_from_top(self):
+ index = self.number_of_cards - 1;
+ temporary = self.collection[index]
+ self.collection.remove(self.collection[index])
+ self.number_of_cards -= 1
+ return(temporary)
+
+ def draw_from_bottom(self):
+ temporary = self.collection[0]
+ self.collection.remove(self.collection[0])
+ self.number_of_cards -= 1
+ return(temporary)
+
+ def top_card(self):
+ index = self.number_of_cards - 1;
+ return(self.collection[index])
+
+ def bottom_card(self):
+ index = 0;
+ return(self.collection[index])
+
+ def add(self, card):
+ self.collection[self.number_of_cards] = card
+ self.number_of_cards += 1
+
+ def index(self, card):
+ if card in self.collection:
+ return self.collection.index(card)
+ else: return ValueError("{0} is not in list".format(card))
+
+
+
+
+class Two(Rank):
+ symbol = "2"
+
+class Three(Rank):
+ symbol = "3"
+
+class Four(Rank):
+ symbol = "4"
+
+class Five(Rank):
+ symbol = "5"
+
+class Six(Rank):
+ symbol = "6"
+
+class Seven(Rank):
+ symbol = "7"
+
+class Eight(Rank):
+ symbol = "8"
+
+class Nine(Rank):
+ symbol = "9"
+
+class Ten(Rank):
+ symbol = "10"
+
+class Jack(Rank):
+ symbol = "J"
+
+class Queen(Rank):
+ symbol = "K"
+
+class King(Rank):
+ symbol = "Q"
+
+class Ace(Rank):
+ symbol = "A"
+
+
+
+class Diamonds(Suit):
+ color = "Diamond"
+class Spades(Suit):
+ color = "Spades"
+class Hearts(Suit):
+ color = "Hearts"
+class Clubs(Suit):
+ color = "Clubs"
+
+RANKS = {}
+for cls in vars()['Rank'].__subclasses__():
+ RANKS[cls.__name__] = cls
+
+SUITS = {}
+for cls in vars()['Suit'].__subclasses__():
+ SUITS[cls.__name__] = cls
+
+
+
+def StandartDeck():
+ l = []
+
+ for i in RANKS.keys():
+ for j in SUITS.keys():
+ card = Card(i, j)
+ l.append(card)
+
+ return CardCollection(l)
+
+
+def BeloteDeck():
+ l = []
+
+ for i in RANKS.keys():
+ if i not in beloteExtra:
+ for j in SUITS.keys():
+ card = Card(i, j)
+ l.append(card)
+
+ return CardCollection(l)
+
+def SixtySixDeck():
+ l = []
+
+ for i in RANKS.keys():
+ if i not in beloteExtra and i not in sixtySixExtra:
+ for j in SUITS.keys():
+ card = Card(i, j)
+ l.append(card)
+
+ return CardCollection(l)
+
+
+# ako zakomentirame tazi 4ast:
+deck = SixtySixDeck()
+
+
+j = 1
+for i in range(0, deck.number_of_cards):
+ print("{0}: {1}".format(j, deck.collection[i]))
+ j+=1
+
+print(RANKS.keys())
+
+# do tuk
+# i razkomentirame tazi 4ast:
+
+##aos = Card(RANKS["Ace"], SUITS["Spades"])
+##kos = Card(RANKS["King"], SUITS["Spades"])
+##l = [aos, kos]
+##deck = CardCollection(l)
+##
+##print(deck.__dict__)
+##
+##deck.add(Card(RANKS["Two"], SUITS["Hearts"]))
+##print((deck.draw_from_bottom()))
+##print(deck.__dict__)
+##
+##print(deck.index(aos))
+##
+##
+##j = 0
+##for i in range(0, deck.number_of_cards):
+## print("{0}: {1}".format(j, deck.collection[i]))
+## j+=1
+
+# do tuk

Това решение не е финално, качих го, защото нещо не ми е ясно и се надявах, че може да ми помогнете.

Като тествам с тази част, която не е закоментирана, всичко е наред, но ако ползвам тази закоментираната, _ _ str _ _ нещо не работи.

И обратно, ако в клас Card сложа
def _ _ str _ _ (self):
    return "Card {0} of {1}".format(self.rank. _ _ name _ _ , self.suit. _ _ name_ _ )
(оставям празни места, защото не ми излизат долните черти)

Та, като ползвам това пък, закоментирата част работи без проблем, а другото казва, че няма такова нещо _ _ name _ _

Мен ми се струва, че е едно и също, но явно не е...
Ще се мъча да го оправя, но ако имате време и желание (знам, че сме 100+ човека), ще се радвам да хвърлите едно око :)

Първо, на ред 38 защо умножаваш по 53? В Питон не е нужно да заделяш памет. Освен това съвсем спокойно можеш да имаш и 104 карти, повтарящи се. И 1040. Не виждам смисъла. Питон е динамичен език който ти заделя сам колкото памет ти трябва, не се тревожи за това в аванс -- само ако някога напишеш питонска програма която заема твърде много памет.

Освен това на редове 137-143 не знам какво искаш да направиш, но там ти се чупи кода.

Решението ти е да напишеш речниците на ръка:

RANKS = {"Ace": Ace, "Two", Two, ... }

Ако не ти се пише пак целия масив с карти и бои пробвай да си правиш класовете с функцията type(). С нея можеш да си направиш цикъл който взима имената на картите и боите като низове от масиви и прави класове с тях :).

Димитър обнови решението на 22.03.2014 22:49 (преди над 10 години)

-beloteExtra = ['Two', 'Three', 'Four', 'Five', 'Six']
-sixtySixExtra = ['Seven', 'Eight']
+beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
+sixtySixUnnecessary = ['Seven', 'Eight']
class Rank:
def __eq__(self, other):
if self.__class__ == other.__class__:
return True
else: return False
def __str__(self):
return self.__class__.__name__
class Suit:
def __str__(self):
return self.__class__.__name__
def __eq__(self, other):
if self.__class__ == other.__class__:
return True
else: return False
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank)
object.__setattr__(self, "suit", suit)
def __str__(self):
- return "Card {0} of {1}".format((self.rank), (self.suit))
+ return "Card {0} of {1}".format(self.rank, self.suit)
def __setattr__(self, *args):
return AttributeError("can't set attribute")
-class CardCollection:
- collection = [0]*53
+class CardCollection():
+ collection = []
number_of_cards = 0
def __init__(self, collection):
- for i in collection:
- self.collection[self.number_of_cards] = i;
- self.number_of_cards += 1
+ self.collection = collection
+ self.number_of_cards = len(collection)
+
def draw(self, index):
if index >= self.number_of_cards:
return IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
- self.collection[self.number_of_cards] = card
+ self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else: return ValueError("{0} is not in list".format(card))
class Two(Rank):
symbol = "2"
class Three(Rank):
symbol = "3"
class Four(Rank):
symbol = "4"
class Five(Rank):
symbol = "5"
class Six(Rank):
symbol = "6"
class Seven(Rank):
symbol = "7"
class Eight(Rank):
symbol = "8"
class Nine(Rank):
symbol = "9"
class Ten(Rank):
symbol = "10"
class Jack(Rank):
symbol = "J"
class Queen(Rank):
symbol = "K"
class King(Rank):
symbol = "Q"
class Ace(Rank):
symbol = "A"
class Diamonds(Suit):
color = "Diamond"
class Spades(Suit):
color = "Spades"
class Hearts(Suit):
color = "Hearts"
class Clubs(Suit):
color = "Clubs"
-RANKS = {}
-for cls in vars()['Rank'].__subclasses__():
- RANKS[cls.__name__] = cls
+RANKS = {"Ace": Ace, "King": King, "Queen": Queen, "Jack": Jack, "Ten": Ten,\
+ "Nine": Nine, "Eight": Eight, "Seven": Seven, "Six": Six, "Five": Five \
+ , "Four": Four, "Three": Three, "Two": Two}
-SUITS = {}
-for cls in vars()['Suit'].__subclasses__():
- SUITS[cls.__name__] = cls
+SUITS = {"Diamonds": Diamonds, "Hearts":Hearts, "Clubs":Clubs, "Spades":Spades}
def StandartDeck():
l = []
for i in RANKS.keys():
for j in SUITS.keys():
card = Card(i, j)
l.append(card)
return CardCollection(l)
def BeloteDeck():
l = []
for i in RANKS.keys():
- if i not in beloteExtra:
+ if i not in beloteUnnecessary:
for j in SUITS.keys():
card = Card(i, j)
l.append(card)
return CardCollection(l)
def SixtySixDeck():
l = []
for i in RANKS.keys():
- if i not in beloteExtra and i not in sixtySixExtra:
+ if i not in beloteUnnecessary and i not in sixtySixUnnecessary:
for j in SUITS.keys():
card = Card(i, j)
l.append(card)
return CardCollection(l)
-# ako zakomentirame tazi 4ast:
-deck = SixtySixDeck()
+# test 1
-
+deck = SixtySixDeck()
+print(deck)
j = 1
for i in range(0, deck.number_of_cards):
print("{0}: {1}".format(j, deck.collection[i]))
j+=1
-print(RANKS.keys())
-# do tuk
-# i razkomentirame tazi 4ast:
+#-------
-##aos = Card(RANKS["Ace"], SUITS["Spades"])
-##kos = Card(RANKS["King"], SUITS["Spades"])
-##l = [aos, kos]
-##deck = CardCollection(l)
-##
-##print(deck.__dict__)
-##
-##deck.add(Card(RANKS["Two"], SUITS["Hearts"]))
-##print((deck.draw_from_bottom()))
-##print(deck.__dict__)
-##
-##print(deck.index(aos))
-##
-##
-##j = 0
-##for i in range(0, deck.number_of_cards):
-## print("{0}: {1}".format(j, deck.collection[i]))
-## j+=1
+print()
-# do tuk
+#------
+
+# test 2
+
+l =[]
+aos = Card(RANKS["Ace"], SUITS["Spades"])
+kos = Card(RANKS["King"], SUITS["Spades"])
+l.append(aos)
+l.append(kos)
+
+
+deck1 = CardCollection(l)
+
+deck1.add(Card(RANKS["Two"], SUITS["Hearts"]))
+
+print(deck1)
+
+j1 = 1
+for i in range(0, deck1.number_of_cards):
+ print("{0}: {1}".format(j1, deck1.collection[i]))
+ j1 += 1

Мерси за бързия отговор, но не е от това... Aз точно това правих, но все пак ги написах на ръка RANGE и SUITS. Проблема си остава - test 1 принтира картите добре, test 2 - не, а уж са еднакви неща... нз, утре ще го мисля повече.

Лека вечер! И мерси пак! :)

Засега забрави за __setattr__, пробвай да използваш @property. Освен това се пише StandardDeck. Аргумента на CardCollection не е задължителен. Можеш да имаш празна колекция empty = CardCollection(). Няма нужда да се занимаваш с number_of_cards. Можеш винаги да извикаш len(self.collection).

Като цяло ти препоръчвам да прочетеш пак условието и да започнеш отначало. И не включвай кода си за тестване от края на решението -- рискуваш да ни счупиш системата за оценяване и да не получиш никакви точки :).

Димитър обнови решението на 24.03.2014 14:38 (преди над 10 години)

-beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
-sixtySixUnnecessary = ['Seven', 'Eight']
-
class Rank:
-
- def __eq__(self, other):
- if self.__class__ == other.__class__:
- return True
- else: return False
-
def __str__(self):
return self.__class__.__name__
class Suit:
def __str__(self):
return self.__class__.__name__
- def __eq__(self, other):
- if self.__class__ == other.__class__:
- return True
- else: return False
-
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank)
object.__setattr__(self, "suit", suit)
def __str__(self):
- return "Card {0} of {1}".format(self.rank, self.suit)
+ return ("{0} of {1}").format(self.rank.__name__, self.suit.__name__)
def __setattr__(self, *args):
- return AttributeError("can't set attribute")
+ raise AttributeError("can't set attribute")
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
-class CardCollection():
- collection = []
- number_of_cards = 0
+
+class CardCollection:
+ number_of_cards = 0
def __init__(self, collection):
self.collection = collection
self.number_of_cards = len(collection)
-
def draw(self, index):
if index >= self.number_of_cards:
- return IndexError("list index out of range")
+ raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
- else: return ValueError("{0} is not in list".format(card))
+ else: raise ValueError("<Card {0}> is not in list".format(card))
+ def __str__(self):
+ answer = "["
+ answer += "<Card {0}>".format(self.collection[0])
+ for i in range(1, self.number_of_cards):
+ answer += ", <Card {0}>".format(self.collection[i])
+ answer += "]"
+ return(answer)
+ def __iter__(self):
+ return iter(self.collection)
+ def __getitem__(self, index):
+ if index >= self.number_of_cards:
+ raise IndexError("list index out of range")
+ else:
+ return self.collection[index]
class Two(Rank):
symbol = "2"
class Three(Rank):
symbol = "3"
class Four(Rank):
symbol = "4"
class Five(Rank):
symbol = "5"
class Six(Rank):
symbol = "6"
class Seven(Rank):
symbol = "7"
class Eight(Rank):
symbol = "8"
class Nine(Rank):
symbol = "9"
class Ten(Rank):
symbol = "10"
class Jack(Rank):
symbol = "J"
class Queen(Rank):
symbol = "K"
class King(Rank):
symbol = "Q"
class Ace(Rank):
symbol = "A"
-
class Diamonds(Suit):
color = "Diamond"
class Spades(Suit):
color = "Spades"
class Hearts(Suit):
color = "Hearts"
class Clubs(Suit):
color = "Clubs"
+beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
+sixtySixUnnecessary = ['Seven', 'Eight']
+
RANKS = {"Ace": Ace, "King": King, "Queen": Queen, "Jack": Jack, "Ten": Ten,\
"Nine": Nine, "Eight": Eight, "Seven": Seven, "Six": Six, "Five": Five \
, "Four": Four, "Three": Three, "Two": Two}
SUITS = {"Diamonds": Diamonds, "Hearts":Hearts, "Clubs":Clubs, "Spades":Spades}
-
-def StandartDeck():
- l = []
-
+def StandardDeck():
+ l =[]
for i in RANKS.keys():
for j in SUITS.keys():
- card = Card(i, j)
- l.append(card)
+ l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
-
def BeloteDeck():
l = []
-
for i in RANKS.keys():
if i not in beloteUnnecessary:
for j in SUITS.keys():
- card = Card(i, j)
- l.append(card)
+ l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
def SixtySixDeck():
l = []
-
for i in RANKS.keys():
if i not in beloteUnnecessary and i not in sixtySixUnnecessary:
for j in SUITS.keys():
- card = Card(i, j)
- l.append(card)
+ l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
-
-
-# test 1
-
-deck = SixtySixDeck()
-print(deck)
-j = 1
-for i in range(0, deck.number_of_cards):
- print("{0}: {1}".format(j, deck.collection[i]))
- j+=1
-
-
-#-------
-
-print()
-
-#------
-
-# test 2
-
-l =[]
-aos = Card(RANKS["Ace"], SUITS["Spades"])
-kos = Card(RANKS["King"], SUITS["Spades"])
-l.append(aos)
-l.append(kos)
-
-
-deck1 = CardCollection(l)
-
-deck1.add(Card(RANKS["Two"], SUITS["Hearts"]))
-
-print(deck1)
-
-j1 = 1
-for i in range(0, deck1.number_of_cards):
- print("{0}: {1}".format(j1, deck1.collection[i]))
- j1 += 1

Димитър обнови решението на 24.03.2014 16:06 (преди над 10 години)

class Rank:
def __str__(self):
return self.__class__.__name__
class Suit:
def __str__(self):
return self.__class__.__name__
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank)
object.__setattr__(self, "suit", suit)
def __str__(self):
- return ("{0} of {1}").format(self.rank.__name__, self.suit.__name__)
+ return ("{0} of {1}").format(self.rank.__name__, self.suit.__name__)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
+ def __getattr__(self, rank):
+ return 53
+
class CardCollection:
number_of_cards = 0
- def __init__(self, collection):
+
+ def __init__(self, collection = list()):
self.collection = collection
self.number_of_cards = len(collection)
def draw(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else: raise ValueError("<Card {0}> is not in list".format(card))
def __str__(self):
answer = "["
answer += "<Card {0}>".format(self.collection[0])
for i in range(1, self.number_of_cards):
answer += ", <Card {0}>".format(self.collection[i])
answer += "]"
return(answer)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
else:
return self.collection[index]
+ def __len__(self):
+ return self.number_of_cards
+
+
class Two(Rank):
symbol = "2"
class Three(Rank):
symbol = "3"
class Four(Rank):
symbol = "4"
class Five(Rank):
symbol = "5"
class Six(Rank):
symbol = "6"
class Seven(Rank):
symbol = "7"
class Eight(Rank):
symbol = "8"
class Nine(Rank):
symbol = "9"
class Ten(Rank):
symbol = "10"
class Jack(Rank):
symbol = "J"
class Queen(Rank):
symbol = "K"
class King(Rank):
symbol = "Q"
class Ace(Rank):
symbol = "A"
class Diamonds(Suit):
color = "Diamond"
class Spades(Suit):
color = "Spades"
class Hearts(Suit):
color = "Hearts"
class Clubs(Suit):
color = "Clubs"
beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
sixtySixUnnecessary = ['Seven', 'Eight']
RANKS = {"Ace": Ace, "King": King, "Queen": Queen, "Jack": Jack, "Ten": Ten,\
"Nine": Nine, "Eight": Eight, "Seven": Seven, "Six": Six, "Five": Five \
, "Four": Four, "Three": Three, "Two": Two}
SUITS = {"Diamonds": Diamonds, "Hearts":Hearts, "Clubs":Clubs, "Spades":Spades}
def StandardDeck():
l =[]
for i in RANKS.keys():
for j in SUITS.keys():
l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
def BeloteDeck():
l = []
for i in RANKS.keys():
if i not in beloteUnnecessary:
for j in SUITS.keys():
l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
def SixtySixDeck():
l = []
for i in RANKS.keys():
if i not in beloteUnnecessary and i not in sixtySixUnnecessary:
for j in SUITS.keys():
l.append(Card(RANKS[i], SUITS[j]))
- return CardCollection(l)
+ return CardCollection(l)

Предполагам за това става дума: "Конструктура на Card приема клас rank и suit (не тяхна инстанция)."
Но не ми е много ясно...
От примерният тест не минават само

  • test_suit_rank_equals
  • test_card_instance

Предполагам, че има общо с това, което ми казвате, но не знам как да го направя. Видях, че още някой във форума има същия проблем. По-късно ще го мисля.

Мерси за бързите коментари! :)

Димитър обнови решението на 25.03.2014 19:14 (преди над 10 години)

class Rank:
- def __str__(self):
- return self.__class__.__name__
+ def __init__(self, symbol):
+ self.symbol = symbol
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
class Suit:
- def __str__(self):
- return self.__class__.__name__
+ def __init__(self, color):
+ self.color = color
+ def __eq__(self, other):
+ return self.color == other.color
+
class Card:
+
def __init__(self, rank, suit):
- object.__setattr__(self, "rank", rank)
- object.__setattr__(self, "suit", suit)
+ object.__setattr__(self, "rank", rank())
+ object.__setattr__(self, "suit", suit())
def __str__(self):
- return ("{0} of {1}").format(self.rank.__name__, self.suit.__name__)
+ return ("{0} of {1}").format(self.rank.__class__.__name__,\
+ self.suit.__class__.__name__)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __getattr__(self, rank):
return 53
-
class CardCollection:
number_of_cards = 0
def __init__(self, collection = list()):
self.collection = collection
self.number_of_cards = len(collection)
def draw(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
- else: raise ValueError("<Card {0}> is not in list".format(card))
+ else:
+ raise ValueError("<Card {0}> is not in list".format(card))
def __str__(self):
answer = "["
answer += "<Card {0}>".format(self.collection[0])
for i in range(1, self.number_of_cards):
answer += ", <Card {0}>".format(self.collection[i])
answer += "]"
return(answer)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
else:
return self.collection[index]
def __len__(self):
return self.number_of_cards
+
+
class Two(Rank):
- symbol = "2"
+ def __init__(self):
+ Rank.__init__(self, '2')
class Three(Rank):
- symbol = "3"
+ def __init__(self):
+ Rank.__init__(self, '3')
class Four(Rank):
- symbol = "4"
+ def __init__(self):
+ Rank.__init__(self, '4')
class Five(Rank):
- symbol = "5"
+ def __init__(self):
+ Rank.__init__(self, '5')
class Six(Rank):
- symbol = "6"
+ def __init__(self):
+ Rank.__init__(self, '6')
class Seven(Rank):
- symbol = "7"
+ def __init__(self):
+ Rank.__init__(self, '7')
class Eight(Rank):
- symbol = "8"
+ def __init__(self):
+ Rank.__init__(self, '8')
class Nine(Rank):
- symbol = "9"
+ def __init__(self):
+ Rank.__init__(self, '9')
class Ten(Rank):
- symbol = "10"
+ def __init__(self):
+ Rank.__init__(self, '10')
class Jack(Rank):
- symbol = "J"
+ def __init__(self):
+ Rank.__init__(self, 'J')
class Queen(Rank):
- symbol = "K"
+ def __init__(self):
+ Rank.__init__(self, 'Q')
class King(Rank):
- symbol = "Q"
+ def __init__(self):
+ Rank.__init__(self, 'K')
class Ace(Rank):
- symbol = "A"
+ def __init__(self):
+ Rank.__init__(self, 'A')
class Diamonds(Suit):
- color = "Diamond"
+ def __init__(self):
+ Suit.__init__(self, 'red')
class Spades(Suit):
- color = "Spades"
+ def __init__(self):
+ Suit.__init__(self, 'black')
class Hearts(Suit):
- color = "Hearts"
+ def __init__(self):
+ Suit.__init__(self, 'red')
class Clubs(Suit):
- color = "Clubs"
+ def __init__(self):
+ Suit.__init__(self, 'black')
beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
sixtySixUnnecessary = ['Seven', 'Eight']
-RANKS = {"Ace": Ace, "King": King, "Queen": Queen, "Jack": Jack, "Ten": Ten,\
+RANKS = {"King": King, "Queen": Queen, "Jack": Jack, "Ten": Ten,\
"Nine": Nine, "Eight": Eight, "Seven": Seven, "Six": Six, "Five": Five \
- , "Four": Four, "Three": Three, "Two": Two}
+ , "Four": Four, "Three": Three, "Two": Two, "Ace": Ace, }
-SUITS = {"Diamonds": Diamonds, "Hearts":Hearts, "Clubs":Clubs, "Spades":Spades}
+SUITS = {"Diamonds": Diamonds, "Clubs":Clubs, "Hearts":Hearts, "Spades":Spades}
def StandardDeck():
l =[]
for i in RANKS.keys():
for j in SUITS.keys():
l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
def BeloteDeck():
l = []
for i in RANKS.keys():
if i not in beloteUnnecessary:
for j in SUITS.keys():
l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)
def SixtySixDeck():
l = []
for i in RANKS.keys():
if i not in beloteUnnecessary and i not in sixtySixUnnecessary:
for j in SUITS.keys():
l.append(Card(RANKS[i], SUITS[j]))
return CardCollection(l)

Димитър обнови решението на 25.03.2014 20:24 (преди над 10 години)

+import collections
+
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return self.color == other.color
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __str__(self):
return ("{0} of {1}").format(self.rank.__class__.__name__,\
- self.suit.__class__.__name__)
+ self.suit.__class__.__name__)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __getattr__(self, rank):
return 53
class CardCollection:
number_of_cards = 0
def __init__(self, collection = list()):
self.collection = collection
self.number_of_cards = len(collection)
def draw(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else:
raise ValueError("<Card {0}> is not in list".format(card))
def __str__(self):
answer = "["
answer += "<Card {0}>".format(self.collection[0])
for i in range(1, self.number_of_cards):
answer += ", <Card {0}>".format(self.collection[i])
answer += "]"
return(answer)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
else:
return self.collection[index]
def __len__(self):
return self.number_of_cards
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')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
sixtySixUnnecessary = ['Seven', 'Eight']
RANKS = {"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, }
+RANKS_ORDERED = (("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))
+RANKS_ORDERED = collections.OrderedDict(RANKS_ORDERED)
+
SUITS = {"Diamonds": Diamonds, "Clubs":Clubs, "Hearts":Hearts, "Spades":Spades}
+SUITS_ORDERED = (("Diamonds", Diamonds), ("Clubs", Clubs), ("Hearts", Hearts), ("Spades", Spades))
+SUITS_ORDERED = collections.OrderedDict(SUITS_ORDERED)
-
def StandardDeck():
l =[]
- for i in RANKS.keys():
- for j in SUITS.keys():
- l.append(Card(RANKS[i], SUITS[j]))
+ for i in SUITS_ORDERED:
+ for j in RANKS_ORDERED:
+ l.append(Card(RANKS[j], SUITS[i]))
return CardCollection(l)
def BeloteDeck():
l = []
- for i in RANKS.keys():
- if i not in beloteUnnecessary:
- for j in SUITS.keys():
- l.append(Card(RANKS[i], SUITS[j]))
+ for i in SUITS_ORDERED:
+ for j in RANKS_ORDERED:
+ if j not in beloteUnnecessary:
+ l.append(Card(RANKS[j], SUITS[i]))
return CardCollection(l)
def SixtySixDeck():
l = []
- for i in RANKS.keys():
- if i not in beloteUnnecessary and i not in sixtySixUnnecessary:
- for j in SUITS.keys():
- l.append(Card(RANKS[i], SUITS[j]))
+ for i in SUITS_ORDERED:
+ for j in RANKS_ORDERED:
+ if j not in beloteUnnecessary and j not in sixtySixUnnecessary:
+ l.append(Card(RANKS[j], SUITS[i]))
return CardCollection(l)

Димитър обнови решението на 26.03.2014 12:51 (преди над 10 години)

import collections
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
+ def __str__(self):
+ return self.__class__.__name__
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
- return self.color == other.color
+ return str(self.color == other.color)
+ def __str__(self):
+ return self.__class__.__name__
+
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __str__(self):
return ("{0} of {1}").format(self.rank.__class__.__name__,\
- self.suit.__class__.__name__)
+ self.suit.__class__.__name__)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
- def __getattr__(self, rank):
- return 53
class CardCollection:
number_of_cards = 0
def __init__(self, collection = list()):
self.collection = collection
self.number_of_cards = len(collection)
def draw(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else:
raise ValueError("<Card {0}> is not in list".format(card))
def __str__(self):
+ if self.number_of_cards == 0:
+ return "No cards"
answer = "["
answer += "<Card {0}>".format(self.collection[0])
for i in range(1, self.number_of_cards):
answer += ", <Card {0}>".format(self.collection[i])
answer += "]"
return(answer)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
else:
return self.collection[index]
def __len__(self):
return self.number_of_cards
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')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
sixtySixUnnecessary = ['Seven', 'Eight']
RANKS = {"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, }
RANKS_ORDERED = (("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))
RANKS_ORDERED = collections.OrderedDict(RANKS_ORDERED)
SUITS = {"Diamonds": Diamonds, "Clubs":Clubs, "Hearts":Hearts, "Spades":Spades}
SUITS_ORDERED = (("Diamonds", Diamonds), ("Clubs", Clubs), ("Hearts", Hearts), ("Spades", Spades))
SUITS_ORDERED = collections.OrderedDict(SUITS_ORDERED)
def StandardDeck():
l =[]
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
l.append(Card(RANKS[j], SUITS[i]))
return CardCollection(l)
def BeloteDeck():
l = []
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
if j not in beloteUnnecessary:
l.append(Card(RANKS[j], SUITS[i]))
return CardCollection(l)
def SixtySixDeck():
l = []
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
if j not in beloteUnnecessary and j not in sixtySixUnnecessary:
l.append(Card(RANKS[j], SUITS[i]))
return CardCollection(l)

Димитър обнови решението на 26.03.2014 13:43 (преди над 10 години)

import collections
class Rank:
def __init__(self, symbol):
self.symbol = symbol
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
- return str(self.color == other.color)
+ return self.__class__.__name__ == other.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __str__(self):
return ("{0} of {1}").format(self.rank.__class__.__name__,\
self.suit.__class__.__name__)
def __setattr__(self, *args):
raise AttributeError("can't set attribute")
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
class CardCollection:
number_of_cards = 0
def __init__(self, collection = list()):
self.collection = collection
self.number_of_cards = len(collection)
def draw(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_top(self):
index = self.number_of_cards - 1;
temporary = self.collection[index]
self.collection.remove(self.collection[index])
self.number_of_cards -= 1
return(temporary)
def draw_from_bottom(self):
temporary = self.collection[0]
self.collection.remove(self.collection[0])
self.number_of_cards -= 1
return(temporary)
def top_card(self):
index = self.number_of_cards - 1;
return(self.collection[index])
def bottom_card(self):
index = 0;
return(self.collection[index])
def add(self, card):
self.collection.append(card)
self.number_of_cards += 1
def index(self, card):
if card in self.collection:
return self.collection.index(card)
else:
raise ValueError("<Card {0}> is not in list".format(card))
def __str__(self):
if self.number_of_cards == 0:
return "No cards"
answer = "["
answer += "<Card {0}>".format(self.collection[0])
for i in range(1, self.number_of_cards):
answer += ", <Card {0}>".format(self.collection[i])
answer += "]"
return(answer)
def __iter__(self):
return iter(self.collection)
def __getitem__(self, index):
if index >= self.number_of_cards:
raise IndexError("list index out of range")
else:
return self.collection[index]
def __len__(self):
return self.number_of_cards
-
-
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')
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, 'red')
+
+
class Spades(Suit):
def __init__(self):
Suit.__init__(self, 'black')
+
+
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, 'red')
+
+
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, 'black')
+
beloteUnnecessary = ['Two', 'Three', 'Four', 'Five', 'Six']
+
sixtySixUnnecessary = ['Seven', 'Eight']
RANKS = {"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, }
-RANKS_ORDERED = (("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))
-RANKS_ORDERED = collections.OrderedDict(RANKS_ORDERED)
+RANKS_ORDERED = (("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))
+RANKS_ORDERED = collections.OrderedDict(RANKS_ORDERED)
+
SUITS = {"Diamonds": Diamonds, "Clubs":Clubs, "Hearts":Hearts, "Spades":Spades}
-SUITS_ORDERED = (("Diamonds", Diamonds), ("Clubs", Clubs), ("Hearts", Hearts), ("Spades", Spades))
+
+SUITS_ORDERED = (("Diamonds", Diamonds), ("Clubs", Clubs), ("Hearts", Hearts),\
+("Spades", Spades))
+
SUITS_ORDERED = collections.OrderedDict(SUITS_ORDERED)
def StandardDeck():
l =[]
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
- l.append(Card(RANKS[j], SUITS[i]))
+ l.append(str(Card(RANKS[j], SUITS[i])))
+ return l
- return CardCollection(l)
-
def BeloteDeck():
l = []
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
if j not in beloteUnnecessary:
- l.append(Card(RANKS[j], SUITS[i]))
+ l.append(str(Card(RANKS[j], SUITS[i])))
+ return l
- return CardCollection(l)
-
def SixtySixDeck():
l = []
for i in SUITS_ORDERED:
for j in RANKS_ORDERED:
if j not in beloteUnnecessary and j not in sixtySixUnnecessary:
- l.append(Card(RANKS[j], SUITS[i]))
-
+ l.append(str(Card(RANKS[j], SUITS[i])))
- return CardCollection(l)
+ return l