Решение на От ливадите до Лас Вегас (и назад) от Гергана Гочева

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

Към профила на Гергана Гочева

Резултати

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

Код

import random
suits = ['clubs', 'diamonds', 'hearts', 'spades']
faces = ('2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A')
class Card:
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
class Deck:
def __init__(self, face_filter=faces):
self.deck = []
for face in face_filter:
self.deck += [Card(suit, face) for suit in suits]
def cut(self):
index_to_cut = random.randrange(len(self.deck))
for _ in range(index_to_cut):
self.deck.append(self.deck.pop(0))
def shuffle(self):
random.shuffle(self.deck)
def get_cards(self):
return [card for card in self.deck]
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = [Player() for _ in range(number_of_players)]
self.deck = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.deck += player.get_cards()
player.card = []
self.deck.shuffle()
self.deck.cut()
def deal(self, first_player):
first_player_index = self.players.index(first_player)
direction = 1
if self.dealing_direction == 'rtl':
direction = -1
current_index = first_player_index
for number in self.dealing_instructions:
while 0 <= current_index < self.number_of_players:
self.players[current_index].cards += [self.deck.deck.pop(0) for _ in range(number)]
current_index += direction
current_index -= self.number_of_players * direction
while current_index != first_player_index:
self.players[current_index].cards += [self.deck.deck.pop(0) for _ in range(number)]
current_index += direction
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.game_deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

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

F.....F.F....F.
======================================================================
FAIL: test_correct_deck_init (test.TestBelot)
Test initialization with correct deck.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Items in the first set but not the second:
('spades', '10')
('clubs', '10')
('diamonds', '10')
('hearts', '10')
Items in the second set but not the first:
('clubs', '3')
('hearts', '5')
('hearts', '6')
('hearts', '4')
('diamonds', '2')
('diamonds', '3')
('hearts', '2')
('spades', '5')
('hearts', '3')
('spades', '6')
('spades', '4')
('clubs', '5')
('spades', '2')
('spades', '3')
('clubs', '4')
('clubs', '6')
('diamonds', '6')
('diamonds', '5')
('clubs', '2')
('diamonds', '4')

======================================================================
FAIL: test_init_regular (test.TestDeck)
Test initialized cards without filter.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Items in the first set but not the second:
('spades', '10')
('clubs', '10')
('diamonds', '10')
('hearts', '10')

======================================================================
FAIL: test_collecting_cards_before_dealing (test.TestGame)
Test collecting the cards before a new deal.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Lists differ: [<solution.Card object at 0xffff8b53b970>,[79 chars]e90>] != []

First list contains 3 additional elements.
First extra element 0:
<solution.Card object at 0xffff8b53b970>

+ []
- [<solution.Card object at 0xffff8b53b970>,
-  <solution.Card object at 0xffff8b53bb50>,
-  <solution.Card object at 0xffff8b53ae90>]

======================================================================
FAIL: test_correct_deck_init (test.TestPoker)
Test initialization with correct deck.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Items in the first set but not the second:
('spades', '10')
('clubs', '10')
('diamonds', '10')
('hearts', '10')

----------------------------------------------------------------------
Ran 15 tests in 0.161s

FAILED (failures=4)

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

Гергана обнови решението на 14.11.2022 08:19 (преди около 2 години)

+import random
+
+suits = ['clubs', 'diamonds', 'hearts', 'spades']
+faces = ['2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A']
+
+
+class Card:
+ def __init__(self, suit, face):
+ self.suit = suit
+ self.face = face
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+
+class Deck:
+ def __init__(self, *face_filter):

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

+ self.deck = []
+ if len(face_filter) == 0:
+ face_filter = faces
+ self.face_filter = face_filter[0]
+ for face in face_filter[0]:
+ self.deck += [Card(suit, face) for suit in suits]
+
+ def cut(self):
+ index_to_cut = random.randrange(len(self.deck))
+ for i in range(index_to_cut + 1):

Забравяш нулата. Да, ако index_to_cut е 19, range ще приключи при 18, но започвайки от нула, това все пак са 19 итерации.
Ако го направиш така, както го правиш сега, добавяйки едно, при index_to_cut == 19, ще се получат 20 итерации, което значи, че ще преместиш всяка една карта и ще получиш същото тесте, все едно въобще не си цепила, което от гледна точка на тестовете е проблем.

import random
deck = list(range(20))
index_to_cut = random.randrange(len(deck))
index_to_cut = 19 # Нека го насилим да е последният индекс

for i in range(index_to_cut + 1):
    deck.append(deck.pop(0))

print(deck) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
+ self.deck.append(self.deck.pop(0))
+
+ def shuffle(self):
+ random.shuffle(self.deck)
+
+ def get_cards(self):
+ return [card for card in self.deck]
+
+
+class Player:
+ def __init__(self):
+
+ def get_cards(self):
+ return self.card
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.players = [Player() for _ in range(number_of_players)]
+ self.game_deck = Deck()
+
+ def get_players(self):
+ first_player_index = random.randrange(self.number_of_players)
+ for i in range(first_player_index + 1):
+ self.players.append(self.players.pop(0))
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ self.game_deck.deck += player.get_cards()
+ player.card = []
+ self.game_deck.shuffle()
+ self.game_deck.cut()
+
+ def deal(self, first_player):
+ first_player_index = self.players.index(first_player)
+ direction = 1
+ if self.dealing_direction == 'rtl':
+ direction = -1
+ current_index = first_player_index
+ for number in self.dealing_instructions:
+ while 0 <= current_index < self.number_of_players:
+ self.players[current_index].card += [self.game_deck.deck.pop(0) for _ in range(number)]
+ current_index += direction
+ current_index -= self.number_of_players * direction
+ while current_index != first_player_index:
+ self.players[current_index].card += [self.game_deck.deck.pop(0) for _ in range(number)]
+ current_index += direction
+
+ def get_deck(self):
+ return self.game_deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self.game_deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Гергана обнови решението на 14.11.2022 23:25 (преди около 2 години)

import random
suits = ['clubs', 'diamonds', 'hearts', 'spades']
-faces = ['2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A']
+faces = ('2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A')
class Card:
def __init__(self, suit, face):
- self.suit = suit
- self.face = face
+ self._suit = suit
+ self._face = face
def get_suit(self):
- return self.suit
+ return self._suit
def get_face(self):
- return self.face
+ return self._face
class Deck:
- def __init__(self, *face_filter):
+ def __init__(self, face_filter=faces):
self.deck = []
- if len(face_filter) == 0:
- face_filter = faces
- self.face_filter = face_filter[0]
- for face in face_filter[0]:
+ for face in face_filter:
self.deck += [Card(suit, face) for suit in suits]
def cut(self):
index_to_cut = random.randrange(len(self.deck))
- for i in range(index_to_cut + 1):
+ for _ in range(index_to_cut):
self.deck.append(self.deck.pop(0))
def shuffle(self):
random.shuffle(self.deck)
def get_cards(self):
return [card for card in self.deck]
class Player:
def __init__(self):
- self.card = []
+ self.cards = []
def get_cards(self):
- return self.card
+ return self.cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = [Player() for _ in range(number_of_players)]
- self.game_deck = Deck()
+ self.deck = Deck()
def get_players(self):
- first_player_index = random.randrange(self.number_of_players)
- for i in range(first_player_index + 1):
- self.players.append(self.players.pop(0))
return self.players
def prepare_deck(self):
for player in self.players:
- self.game_deck.deck += player.get_cards()
+ self.deck.deck += player.get_cards()
player.card = []
- self.game_deck.shuffle()
- self.game_deck.cut()
+ self.deck.shuffle()
+ self.deck.cut()
def deal(self, first_player):
first_player_index = self.players.index(first_player)
direction = 1
if self.dealing_direction == 'rtl':
direction = -1
current_index = first_player_index
for number in self.dealing_instructions:
while 0 <= current_index < self.number_of_players:
- self.players[current_index].card += [self.game_deck.deck.pop(0) for _ in range(number)]
+ self.players[current_index].cards += [self.deck.deck.pop(0) for _ in range(number)]
current_index += direction
current_index -= self.number_of_players * direction
while current_index != first_player_index:
- self.players[current_index].card += [self.game_deck.deck.pop(0) for _ in range(number)]
+ self.players[current_index].cards += [self.deck.deck.pop(0) for _ in range(number)]
current_index += direction
def get_deck(self):
- return self.game_deck
+ return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.game_deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))