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

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

Към профила на Васил Бунарджиев

Резултати

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

Код

import itertools
CLASSES = [
'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
BELOTE = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
SIXTYSIX = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
SYMBOLS = ['K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2', 'Ace']
SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
COLORS = ['red', 'black', 'red', 'black']
class Rank:
symbol = ""
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
return self.__class__.__name__
class Suit:
color = ""
def __eq__(self, other):
return self.color == other.color
def __str__(self):
return self.__class__.__name__
RANKS = {
class_name: type(class_name, (Rank, ), dict(symbol='')) for
class_name in CLASSES}
SUITS = {
class_name: type(class_name, (Suit, ), dict(color=''))
for class_name in SUITS}
def symbol_assigment():
counter_for_ranks = 0
counter_for_suits = 0
for names in CLASSES:
RANKS[names].symbol = SYMBOLS[counter_for_ranks]
counter_for_ranks += 1
for names in SUITS:
SUITS[names].color = COLORS[counter_for_suits]
counter_for_suits += 1
symbol_assigment()
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 __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return str(self)
class CardCollection:
def __init__(self, collection=[]):
self.collect = list(collection)
def add(self, card):
self.collect.append(card)
def __iter__(self):
for element in self.collect:
yield element
def __getitem__(self, index):
if index > len(self.collect) - 1:
raise IndexError("list index out of range")
else:
return next(itertools.islice(self.collect, index, index+1))
def bottom_card(self):
return self.collect[0]
def top_card(self):
return self.collect[len(self.collect)-1]
def draw(self, index):
return self.collect.pop(index)
def draw_from_bottom(self):
return self.collect.pop(0)
def draw_from_top(self):
return self.collect.pop(len(self.collect)-1)
def __len__(self):
return len(self.collect)
def index(self, card):
for card_index in range(len(self.collect)-1):
if self.collect[card_index] == card:
return card_index
raise ValueError("{0} is not in list".format(card))
def StandardDeck():
return
[Card(RANKS[rank], SUITS[suit]) for
rank in CLASSES for suit in SUITS]
def BeloteDeck():
return
[Card(RANKS[rank], SUITS[suit]) for
rank in BELOTE for suit in SUITS]
def SixtySixDeck():
return
[Card(RANKS[rank], SUITS[suit]) for
rank in SIXTYSIX for suit in SUITS]

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

E..E..EE........
======================================================================
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-kl1y19/test.py", line 174, in test_belote_deck
    cards = [str(card) for card in solution.BeloteDeck()]
TypeError: 'NoneType' object is not iterable

======================================================================
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-kl1y19/test.py", line 161, in test_deck_index
    self.assertEqual(1, deck.index(card2))
  File "/tmp/d20140407-19315-kl1y19/solution.py", line 116, in index
    raise ValueError("{0} is not in list".format(card))
ValueError: King of Diamonds is not in list

======================================================================
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-kl1y19/test.py", line 178, in test_sixtysix_deck
    cards = [str(card) for card in solution.SixtySixDeck()]
TypeError: 'NoneType' object is not iterable

======================================================================
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-kl1y19/test.py", line 170, in test_standard_deck
    cards = [str(card) for card in solution.StandardDeck()]
TypeError: 'NoneType' object is not iterable

----------------------------------------------------------------------
Ran 16 tests in 0.019s

FAILED (errors=4)

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

Васил обнови решението на 21.03.2014 20:39 (преди около 10 години)

+class Rank:
+ symbol = ""
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+ def __str__(self):
+ return self.symbol
+
+class Suit:
+ color = ""
+ def __eq__(self,other):
+ return self.color == other.color
+ def __str__(self):
+ return self.color
+
+class Diamonds(Suit):
+ color = 'red'
+
+class Spades(Suit):
+ color = 'black'
+
+class Clubs(Suit):
+ color = 'black'
+
+class Hearts(Suit):
+ color = 'red'
+
+class Ace(Rank):
+ symbol = 'Ace'
+
+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 = 'Q'
+
+class King(Rank):
+ symbol = 'K'
+
+
+RANKS = {'Ace':Ace,'Two':Two,'Three':Three,'Four':Four,'Five':Five,'Six':Six,
+ 'Seven':Seven,'Eight':Eight,'Nine':Nine,'Ten':Ten,'Jack':Jack,'Queen'
+ :Queen,'King':King}
+RANKS
+
+SUITS = {'Diamonds':Diamonds,'Hearts':Hearts,'Clubs':Clubs,'Spades':Spades}
+SUITS
+
+str(RANKS["Ace"]())
+
+str(SUITS["Spades"]())
+
+
+#Можете ли да ми дадете някакви препоръки за начина по който съм
+#тръгнал да решавам домашното

В Python може динамично да дефинираш клас. За целта можеш да използваш type.

Използвай тестовете, качени в GitHub за да си проверяваш решението. PEP8 не е напълно спазен.

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

Васил обнови решението на 25.03.2014 20:03 (преди около 10 години)

+import itertools
+CLASSES = ['Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King']
+SYMBOLS = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
+SUITS = ['Diamonds','Spades','Clubs','Hearts']
+COLORS = ['red','black','black','red']
+
class Rank:
symbol = ""
def __eq__(self, other):
return self.symbol == other.symbol
def __str__(self):
- return self.symbol
+ return self.__class__.__name__
class Suit:
color = ""
def __eq__(self,other):
return self.color == other.color
def __str__(self):
- return self.color
+ return self.__class__.__name__
-class Diamonds(Suit):
- color = 'red'
-class Spades(Suit):
- color = 'black'
+RANKS = {class_name:type(class_name,(Rank,),dict(symbol='')) for class_name in CLASSES}
+SUITS = {class_name:type(class_name,(Suit,),dict(color = '')) for class_name in SUITS}
-class Clubs(Suit):
- color = 'black'
-class Hearts(Suit):
- color = 'red'
-class Ace(Rank):
- symbol = 'Ace'
-class Two(Rank):
- symbol = '2'
-class Three(Rank):
- symbol = '3'
+def symbol_assigment():
+ counter_for_ranks=0
+ counter_for_suits=0
+ for names in CLASSES:
+ RANKS[names].symbol=SYMBOLS[counter_for_ranks]
+ counter_for_ranks += 1
+ for names in SUITS:
+ SUITS[names].color=COLORS[counter_for_suits]
+ counter_for_suits+=1
-class Four(Rank):
- symbol = '4'
-class Five(Rank):
- symbol = '5'
+symbol_assigment()
-class Six(Rank):
- symbol = '6'
-class Seven(Rank):
- symbol = '7'
+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 __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+ def __str__(self):
+ return str(self.rank) + " of " + str(self.suit)
-class Eight(Rank):
- symbol = '8'
+class CardCollection:
+ def __init__(self,collection):
+ self.collect = list(collection)
+ def add(self,card):
+ self.collect.append(card)
+ def __iter__(self):
+ for element in self.collect:
+ yield element
+ def __getitem__(self,index):
+ if index>len(self.collect)-1:
+ raise IndexError("list index out of range")
+ else:
+ return next(itertools.islice(self.collect,index,index+1))
+ def bottom_card(self):
+ return self.collect[0]
+ def top_card(self):
+ return self.collect[len(self.collect)-1]
-class Nine(Rank):
- symbol = '9'
+#Все още не съм обърнал внимание на PEP8
-
-class Ten(Rank):
- symbol = '10'
-
-class Jack(Rank):
- symbol = 'J'
-
-class Queen(Rank):
- symbol = 'Q'
-
-class King(Rank):
- symbol = 'K'
-
-
-RANKS = {'Ace':Ace,'Two':Two,'Three':Three,'Four':Four,'Five':Five,'Six':Six,
- 'Seven':Seven,'Eight':Eight,'Nine':Nine,'Ten':Ten,'Jack':Jack,'Queen'
- :Queen,'King':King}
-RANKS
-
-SUITS = {'Diamonds':Diamonds,'Hearts':Hearts,'Clubs':Clubs,'Spades':Spades}
-SUITS
-
-str(RANKS["Ace"]())
-
-str(SUITS["Spades"]())
-
-
-#Можете ли да ми дадете някакви препоръки за начина по който съм
-#тръгнал да решавам домашното

Васил обнови решението на 26.03.2014 04:57 (преди около 10 години)

import itertools
-CLASSES = ['Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King']
-SYMBOLS = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
-SUITS = ['Diamonds','Spades','Clubs','Hearts']
-COLORS = ['red','black','black','red']
+CLASSES = [
+ 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+BELOTE = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+SIXTYSIX = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+SYMBOLS = ['K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2', 'Ace']
+SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+COLORS = ['red', 'black', 'red', 'black']
+
class Rank:
symbol = ""
+
def __eq__(self, other):
return self.symbol == other.symbol
+
def __str__(self):
return self.__class__.__name__
+
class Suit:
color = ""
- def __eq__(self,other):
- return self.color == other.color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
def __str__(self):
return self.__class__.__name__
-RANKS = {class_name:type(class_name,(Rank,),dict(symbol='')) for class_name in CLASSES}
-SUITS = {class_name:type(class_name,(Suit,),dict(color = '')) for class_name in SUITS}
+RANKS = {
+ class_name: type(class_name, (Rank, ), dict(symbol='')) for
+ class_name in CLASSES}
+SUITS = {
+ class_name: type(class_name, (Suit, ), dict(color=''))
+ for class_name in SUITS}
-
-
-
def symbol_assigment():
- counter_for_ranks=0
- counter_for_suits=0
+ counter_for_ranks = 0
+ counter_for_suits = 0
for names in CLASSES:
- RANKS[names].symbol=SYMBOLS[counter_for_ranks]
+ RANKS[names].symbol = SYMBOLS[counter_for_ranks]
counter_for_ranks += 1
for names in SUITS:
- SUITS[names].color=COLORS[counter_for_suits]
- counter_for_suits+=1
+ SUITS[names].color = COLORS[counter_for_suits]
+ counter_for_suits += 1
symbol_assigment()
class Card:
- def __init__(self,Rank,Suit):
+ 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 __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
+
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
+ def __repr__(self):
+ return str(self)
+
+
class CardCollection:
- def __init__(self,collection):
+ def __init__(self, collection=[]):
self.collect = list(collection)
- def add(self,card):
+
+ def add(self, card):
self.collect.append(card)
+
def __iter__(self):
for element in self.collect:
yield element
- def __getitem__(self,index):
- if index>len(self.collect)-1:
+
+ def __getitem__(self, index):
+ if index > len(self.collect) - 1:
raise IndexError("list index out of range")
else:
- return next(itertools.islice(self.collect,index,index+1))
+ return next(itertools.islice(self.collect, index, index+1))
+
def bottom_card(self):
return self.collect[0]
+
def top_card(self):
return self.collect[len(self.collect)-1]
-#Все още не съм обърнал внимание на PEP8
+ def draw(self, index):
+ return self.collect.pop(index)
+
+ def draw_from_bottom(self):
+ return self.collect.pop(0)
+
+ def draw_from_top(self):
+ return self.collect.pop(len(self.collect)-1)
+
+ def __len__(self):
+ return len(self.collect)
+
+ def index(self, card):
+ for card_index in range(len(self.collect)-1):
+ if self.collect[card_index] == card:
+ return card_index
+ raise ValueError("{0} is not in list".format(card))
+
+
+def StandardDeck():
+ return
+ [Card(RANKS[rank], SUITS[suit]) for
+ rank in CLASSES for suit in SUITS]
+
+
+def BeloteDeck():
+ return
+ [Card(RANKS[rank], SUITS[suit]) for
+ rank in BELOTE for suit in SUITS]
+
+
+def SixtySixDeck():
+ return
+ [Card(RANKS[rank], SUITS[suit]) for
+ rank in SIXTYSIX for suit in SUITS]