Решение на От ливадите до Лас Вегас (и назад) от Стоян Михайлов

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

Към профила на Стоян Михайлов

Резултати

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

Код

import random
DEAL_LEFT_TO_RIGHT = "ltr"
DEAL_RIGHT_TO_LEFT = "rtl"
class Card:
def __init__(self, suit, face):
self.suit = suit.upper()
self.face = face.upper()
def get_suit(self):
return self.suit
def get_face(self):
return self.face
class Deck:
def __init__(self, face_filter=None):
if face_filter is None:
self.face_filter = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
else:
self.face_filter = face_filter
self.cards = [Card(suit, face) for face in self.face_filter
for suit in ["DIAMONDS", "CLUBS", "SPADES", "HEARTS"]]
def cut(self):
if len(self.cards) < 2:
return
cut_point_index = random.randrange(0, len(self.cards) - 1)
for cut_counter in range(0, cut_point_index):
self.cards.append(self.cards.pop(0))
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def deal(self):
return self.cards.pop(0) if self.cards else None
def add_to_deck(self, card):
self.cards.append(card)
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
def return_cards(self):
cards_in_hand = self.cards.copy()
self.cards.clear()
return cards_in_hand
def receive_card(self, card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.deck = None
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = [Player() for _ in range(0, number_of_players)]
def initialise_deck(self, face_filter=None):
self.deck = Deck(face_filter)
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
player_hand_cards = player.return_cards()
for card in player_hand_cards:
self.deck.add_to_deck(card)
self.deck.shuffle()
self.deck.cut()
def deal(self, first_player):
self.__deal_left_to_right(first_player) if self.dealing_direction == DEAL_LEFT_TO_RIGHT else \
self.__deal_right_to_left(first_player)
def __deal_left_to_right(self, first_player):
self.__sort_players(first_player)
for cards_per_deal in self.dealing_instructions:
for player in self.players:
for _ in range(cards_per_deal):
player.receive_card(self.deck.deal())
def __deal_right_to_left(self, first_player):
self.__sort_players(first_player)
for cards_per_deal in self.dealing_instructions:
for player in reversed(self.players):
for _ in range(cards_per_deal):
player.receive_card(self.deck.deal())
def __sort_players(self, first_player):
first_player_index = self.players.index(first_player)
for _ in range(0, first_player_index):
self.players.append(self.players.pop(0))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, DEAL_LEFT_TO_RIGHT, (2, 3, 3))
super().initialise_deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self):
super().__init__(9, DEAL_RIGHT_TO_LEFT, (1, 1, 1, 1, 1))
super().initialise_deck()

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

F..F.FF.EEE.EFF
======================================================================
ERROR: 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
AttributeError: 'NoneType' object has no attribute 'shuffle'

======================================================================
ERROR: test_dealing_ltr (test.TestGame)
Test dealing the cards left to right.
----------------------------------------------------------------------
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
AttributeError: 'NoneType' object has no attribute 'get_cards'

======================================================================
ERROR: test_dealing_rtl (test.TestGame)
Test dealing the cards right to left.
----------------------------------------------------------------------
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
AttributeError: 'NoneType' object has no attribute 'get_cards'

======================================================================
ERROR: test_prepare_deck (test.TestGame)
Test preparing the deck for dealing.
----------------------------------------------------------------------
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
AttributeError: 'NoneType' object has no attribute 'shuffle'

======================================================================
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:
('clubs', 'K')
('clubs', 'A')
('hearts', '9')
('diamonds', '10')
('hearts', '7')
('clubs', '7')
('clubs', '9')
('spades', '8')
('hearts', 'J')
('clubs', 'J')
('clubs', 'Q')
('diamonds', '8')
('hearts', '10')
('clubs', '10')
('spades', 'Q')
('spades', 'K')
('spades', 'A')
('hearts', '8')
('clubs', '8')
('diamonds', 'Q')
('diamonds', 'K')
('diamonds', 'A')
('spades', '9')
('spades', '7')
('diamonds', '9')
('spades', 'J')
('diamonds', '7')
('diamonds', 'J')
('hearts', 'Q')
('hearts', 'K')
('hearts', 'A')
('spades', '10')
Items in the second set but not the first:
('SPADES', 'J')
('DIAMONDS', '10')
('HEARTS', 'Q')
('HEARTS', 'K')
('CLUBS', '10')
('HEARTS', 'A')
('CLUBS', 'J')
('DIAMONDS', '8')
('SPADES', '10')
('HEARTS', '9')
('HEARTS', '7')
('CLUBS', '8')
('HEARTS', 'J')
('SPADES', '8')
('HEARTS', '10')
('DIAMONDS', 'Q')
('DIAMONDS', 'K')
('DIAMONDS', 'A')
('CLUBS', 'Q')
('DIAMONDS', '9')
('CLUBS', 'A')
('HEARTS', '8')
('SPADES', 'Q')
('DIAMONDS', '7')
('CLUBS', 'K')
('SPADES', 'K')
('SPADES', 'A')
('CLUBS', '9')
('DIAMONDS', 'J')
('CLUBS', '7')
('SPADES', '9')
('SPADES', '7')

======================================================================
FAIL: test_get_suit (test.TestCard)
Test the get_suit method.
----------------------------------------------------------------------
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: 'SPADES' != 'spades'
- SPADES
+ spades


======================================================================
FAIL: test_init_filtered (test.TestDeck)
Test initialized cards with 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:
('clubs', '2')
('clubs', 'A')
('diamonds', '5')
('diamonds', '10')
('hearts', 'J')
('clubs', 'J')
('hearts', '5')
('hearts', '10')
('clubs', '5')
('clubs', '10')
('spades', '2')
('spades', 'A')
('diamonds', '2')
('diamonds', 'A')
('spades', 'J')
('diamonds', 'J')
('hearts', 'A')
('spades', '5')
('spades', '10')
('hearts', '2')
Items in the second set but not the first:
('SPADES', 'J')
('DIAMONDS', '5')
('DIAMONDS', '10')
('CLUBS', '5')
('CLUBS', '10')
('HEARTS', '2')
('HEARTS', 'A')
('SPADES', '5')
('SPADES', '10')
('HEARTS', 'J')
('HEARTS', '5')
('HEARTS', '10')
('DIAMONDS', '2')
('DIAMONDS', 'A')
('CLUBS', 'A')
('CLUBS', '2')
('SPADES', '2')
('SPADES', 'A')
('DIAMONDS', 'J')
('CLUBS', 'J')

======================================================================
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:
('clubs', 'K')
('clubs', '2')
('clubs', 'A')
('hearts', '9')
('diamonds', '5')
('diamonds', '10')
('hearts', '6')
('hearts', '7')
('clubs', '6')
('clubs', '7')
('clubs', '9')
('spades', '8')
('hearts', 'J')
('hearts', '4')
('clubs', 'J')
('clubs', 'Q')
('clubs', '4')
('diamonds', '8')
('hearts', '5')
('hearts', '10')
('clubs', '5')
('clubs', '10')
('spades', '3')
('spades', 'Q')
('spades', '2')
('spades', 'K')
('spades', 'A')
('hearts', '8')
('clubs', '8')
('diamonds', '3')
('diamonds', 'Q')
('diamonds', '2')
('diamonds', 'K')
('diamonds', 'A')
('spades', '9')
('spades', '6')
('spades', '7')
('diamonds', '6')
('diamonds', '9')
('spades', 'J')
('diamonds', '7')
('spades', '4')
('hearts', '3')
('diamonds', 'J')
('hearts', 'Q')
('hearts', 'K')
('hearts', 'A')
('diamonds', '4')
('clubs', '3')
('spades', '10')
('hearts', '2')
('spades', '5')
Items in the second set but not the first:
('CLUBS', '4')
('SPADES', 'J')
('DIAMONDS', '5')
('DIAMONDS', '10')
('HEARTS', '3')
('HEARTS', 'Q')
('SPADES', '4')
('CLUBS', '5')
('CLUBS', '10')
('HEARTS', '2')
('HEARTS', 'K')
('HEARTS', 'A')
('SPADES', '5')
('SPADES', '10')
('DIAMONDS', '8')
('HEARTS', '6')
('HEARTS', '9')
('HEARTS', '7')
('CLUBS', '8')
('HEARTS', 'J')
('HEARTS', '4')
('SPADES', '8')
('SPADES', '7')
('DIAMONDS', '3')
('HEARTS', '5')
('DIAMONDS', '2')
('HEARTS', '10')
('DIAMONDS', 'Q')
('DIAMONDS', 'K')
('DIAMONDS', 'A')
('CLUBS', '3')
('CLUBS', 'Q')
('DIAMONDS', '9')
('CLUBS', 'A')
('HEARTS', '8')
('SPADES', 'Q')
('CLUBS', '2')
('DIAMONDS', '6')
('DIAMONDS', '7')
('CLUBS', 'K')
('SPADES', '3')
('SPADES', 'K')
('SPADES', '2')
('SPADES', 'A')
('CLUBS', '9')
('DIAMONDS', 'J')
('CLUBS', '6')
('CLUBS', '7')
('DIAMONDS', '4')
('SPADES', '6')
('SPADES', '9')
('CLUBS', 'J')

======================================================================
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:
('clubs', 'K')
('clubs', '2')
('clubs', 'A')
('hearts', '9')
('diamonds', '5')
('diamonds', '10')
('hearts', '6')
('hearts', '7')
('clubs', '6')
('clubs', '7')
('clubs', '9')
('spades', '8')
('hearts', 'J')
('hearts', '4')
('clubs', 'J')
('clubs', 'Q')
('clubs', '4')
('diamonds', '8')
('hearts', '5')
('hearts', '10')
('clubs', '5')
('clubs', '10')
('spades', '3')
('spades', 'Q')
('spades', '2')
('spades', 'K')
('spades', 'A')
('hearts', '8')
('clubs', '8')
('diamonds', '3')
('diamonds', 'Q')
('diamonds', '2')
('diamonds', 'K')
('diamonds', 'A')
('spades', '9')
('spades', '6')
('spades', '7')
('diamonds', '6')
('diamonds', '9')
('spades', 'J')
('diamonds', '7')
('spades', '4')
('hearts', '3')
('diamonds', 'J')
('hearts', 'Q')
('hearts', 'K')
('hearts', 'A')
('diamonds', '4')
('clubs', '3')
('spades', '10')
('hearts', '2')
('spades', '5')
Items in the second set but not the first:
('CLUBS', '4')
('SPADES', 'J')
('DIAMONDS', '5')
('DIAMONDS', '10')
('HEARTS', '3')
('HEARTS', 'Q')
('SPADES', '4')
('CLUBS', '5')
('CLUBS', '10')
('HEARTS', '2')
('HEARTS', 'K')
('HEARTS', 'A')
('SPADES', '5')
('SPADES', '10')
('DIAMONDS', '8')
('HEARTS', '6')
('HEARTS', '9')
('HEARTS', '7')
('CLUBS', '8')
('HEARTS', 'J')
('HEARTS', '4')
('SPADES', '8')
('SPADES', '7')
('DIAMONDS', '3')
('HEARTS', '5')
('DIAMONDS', '2')
('HEARTS', '10')
('DIAMONDS', 'Q')
('DIAMONDS', 'K')
('DIAMONDS', 'A')
('CLUBS', '3')
('CLUBS', 'Q')
('DIAMONDS', '9')
('CLUBS', 'A')
('HEARTS', '8')
('SPADES', 'Q')
('CLUBS', '2')
('DIAMONDS', '6')
('DIAMONDS', '7')
('CLUBS', 'K')
('SPADES', '3')
('SPADES', 'K')
('SPADES', '2')
('SPADES', 'A')
('CLUBS', '9')
('DIAMONDS', 'J')
('CLUBS', '6')
('CLUBS', '7')
('DIAMONDS', '4')
('SPADES', '6')
('SPADES', '9')
('CLUBS', 'J')

======================================================================
FAIL: test_correct_direction_and_players_deal (test.TestPoker)
Test dealing with correct direction and players.
----------------------------------------------------------------------
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: {('HEARTS', '10'), ('DIAMONDS', 'K'), ('CLUBS', '6'), ('DIAMONDS', '4'), ('SPADES', '8')} not found in ({('CLUBS', '4'), ('DIAMONDS', '2'), ('DIAMONDS', 'J'), ('SPADES', '6'), ('HEARTS', '8')}, {('DIAMONDS', '8'), ('HEARTS', '5'), ('CLUBS', '10'), ('HEARTS', 'A'), ('SPADES', 'Q')})

----------------------------------------------------------------------
Ran 15 tests in 0.183s

FAILED (failures=6, errors=4)

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

Стоян обнови решението на 15.11.2022 16:14 (преди около 2 години)

+import random
+
+
+class Card:
+ def __init__(self, suit, face):
+ self.suit = suit.upper()
+ self.face = face.upper()
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ if face_filter is None:
+ self.face_filter = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+ else:
+ self.face_filter = face_filter
+
+ self.cards = [Card(suit, face) for face in self.face_filter
+ for suit in ["DIAMONDS", "CLUBS", "SPADES", "HEARTS"]]
+
+ def cut(self):
+ cut_point_index = random.randrange(0, len(self.cards) - 1)
+
+ for cut_counter in range(0, cut_point_index):
+ self.cards.append(self.cards.pop(0))
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def deal(self):
+ return self.cards.pop(0)
+
+ def add_to_deck(self, card):
+ self.cards.append(card)
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+ def return_cards(self):
+ cards_in_hand = self.cards.copy()
+ self.cards.clear()
+ return cards_in_hand
+
+ def receive_card(self, card):
+ self.cards.append(card)
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.deck = None
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.players = [Player() for _ in range(0, number_of_players)]
+
+ def initialise_deck(self, face_filter=None):
+ self.deck = Deck(face_filter)
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ player_hand_cards = player.return_cards()
+ for card in player_hand_cards:
+ self.deck.add_to_deck(card)
+
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, first_player):
+ if self.dealing_direction == "ltr":
+ self.__deal_left_to_right(first_player)
+ else:
+ self.__deal_right_to_left(first_player)
+
+ def __deal_left_to_right(self, first_player):
+ self.__sort_players(first_player)
+
+ for cards_per_deal in self.dealing_instructions:
+ for player in self.players:
+ for _ in range(cards_per_deal):
+ player.receive_card(self.deck.deal())
+
+ def __deal_right_to_left(self, first_player):
+ self.__sort_players(first_player)
+
+ for cards_per_deal in self.dealing_instructions:
+ for player in reversed(self.players):
+ for _ in range(cards_per_deal):
+ player.receive_card(self.deck.deal())
+
+ def __sort_players(self, first_player):
+ first_player_index = self.players.index(first_player)
+ for _ in range(0, first_player_index):
+ self.players.append(self.players.pop(0))
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ super().initialise_deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))
+ super().initialise_deck()

Стоян обнови решението на 15.11.2022 17:30 (преди около 2 години)

import random
+DEAL_LEFT_TO_RIGHT = "ltr"
+DEAL_RIGHT_TO_LEFT = "rtl"
+
class Card:
def __init__(self, suit, face):
self.suit = suit.upper()
self.face = face.upper()
def get_suit(self):
return self.suit
def get_face(self):
return self.face
class Deck:
def __init__(self, face_filter=None):
if face_filter is None:
self.face_filter = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
else:
self.face_filter = face_filter
self.cards = [Card(suit, face) for face in self.face_filter
for suit in ["DIAMONDS", "CLUBS", "SPADES", "HEARTS"]]
def cut(self):
- cut_point_index = random.randrange(0, len(self.cards) - 1)
+ if len(self.cards) < 2:
+ return
+ cut_point_index = random.randrange(0, len(self.cards) - 1)
for cut_counter in range(0, cut_point_index):
self.cards.append(self.cards.pop(0))
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def deal(self):
- return self.cards.pop(0)
+ return self.cards.pop(0) if self.cards else None
def add_to_deck(self, card):
self.cards.append(card)
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
def return_cards(self):
cards_in_hand = self.cards.copy()
self.cards.clear()
return cards_in_hand
def receive_card(self, card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.deck = None
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = [Player() for _ in range(0, number_of_players)]
def initialise_deck(self, face_filter=None):
self.deck = Deck(face_filter)
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
player_hand_cards = player.return_cards()
for card in player_hand_cards:
self.deck.add_to_deck(card)
self.deck.shuffle()
self.deck.cut()
def deal(self, first_player):
- if self.dealing_direction == "ltr":
- self.__deal_left_to_right(first_player)
- else:
+ self.__deal_left_to_right(first_player) if self.dealing_direction == DEAL_LEFT_TO_RIGHT else \
self.__deal_right_to_left(first_player)
def __deal_left_to_right(self, first_player):
self.__sort_players(first_player)
for cards_per_deal in self.dealing_instructions:
for player in self.players:
for _ in range(cards_per_deal):
player.receive_card(self.deck.deal())
def __deal_right_to_left(self, first_player):
self.__sort_players(first_player)
for cards_per_deal in self.dealing_instructions:
for player in reversed(self.players):
for _ in range(cards_per_deal):
player.receive_card(self.deck.deal())
def __sort_players(self, first_player):
first_player_index = self.players.index(first_player)
for _ in range(0, first_player_index):
self.players.append(self.players.pop(0))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
- super().__init__(4, "ltr", (2, 3, 3))
+ super().__init__(4, DEAL_LEFT_TO_RIGHT, (2, 3, 3))
super().initialise_deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self):
- super().__init__(9, "rtl", (1, 1, 1, 1, 1))
+ super().__init__(9, DEAL_RIGHT_TO_LEFT, (1, 1, 1, 1, 1))
super().initialise_deck()