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

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

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

Резултати

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

Код

import random
class CardHolderMixin:
"""Perform common actions for an object holding cards."""
def __init__(self):
"""Initializator."""
self._cards = []
def get_cards(self):
"""Return the list of cards."""
return self._cards
def give_cards(self, cards):
"""Extend the card pile with a set of cards."""
self._cards.extend(cards)
def take_cards(self, card_count=None):
"""Get cards from the top of the cards pile."""
if card_count is None:
# No card_count means taking all the cards
card_count = len(self._cards)
taken_cards = self._cards[:card_count]
self._cards = self._cards[card_count:]
return taken_cards
class Card:
"""Playing card."""
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
"""Get card's suit value."""
return self._suit
def get_face(self):
"""Get card's face value."""
return self._face
class Deck(CardHolderMixin):
"""Deck of playing cards."""
SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
def __init__(self, face_filter=None):
"""Initializator."""
super().__init__()
faces = [x for x in self.FACES if not face_filter or x in face_filter]
for suit in self.SUITS:
for face in faces:
self._cards.append(Card(suit, face))
def cut(self):
"""Cut the deck."""
# A valid cut contains at least one moved card, hence the one below
index = random.randrange(1, len(self._cards))
self._cards = self._cards[index:] + self._cards[:index]
def shuffle(self):
"""Shuffle the deck."""
random.shuffle(self._cards)
class Player(CardHolderMixin):
"""Card player."""
class Game:
"""A game played with cards."""
def __init__(self, number_of_players, dealing_direction,
dealing_instructions):
"""Initializator."""
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._deck = Deck()
self._players = []
for player in range(number_of_players):
self._players.append(Player())
def _get_players_dealing_flow(self, player):
"""Get players in order ready for dealing, starting with player."""
player_order_step = 1 if self._dealing_direction == 'ltr' else -1
ordered_players = self._players[::player_order_step]
index = ordered_players.index(player)
return ordered_players[index:] + ordered_players[:index]
def _collect_cards_from_players(self):
"""Collect the cards from all players."""
for player in self._players:
cards = player.take_cards()
self._deck.give_cards(cards)
def get_players(self):
"""Return the list of players, playing the game."""
return self._players
def prepare_deck(self):
"""Prepare the deck for dealing."""
self._collect_cards_from_players()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
"""Deal the cards to the players."""
player_flow = self._get_players_dealing_flow(player)
for card_count in self._dealing_instructions:
for player in player_flow:
cards = self._deck.take_cards(card_count)
player.give_cards(cards)
def get_deck(self):
"""Return the Deck used for the game."""
return self._deck
class Belot(Game):
"""Belot card game."""
def __init__(self):
"""Initializator."""
super().__init__(4, 'ltr', (2, 3, 3))
self._deck = Deck(face_filter=('7', '8', '9', '10',
'J', 'Q', 'K', 'A'))
class Poker(Game):
"""Poker card game."""
def __init__(self):
"""Initializator."""
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.157s

OK

История (5 версии и 0 коментара)

Георги обнови решението на 03.11.2022 15:51 (преди над 1 година)

+import random
+
+
+class Card:
+ """Playing card."""
+
+ def __init__(self, suit, face):
+ self._suit = suit
+ self._face = face
+
+ def get_suit(self):
+ """Get card's suit value."""
+ return self._suit
+
+ def get_face(self):
+ """Get card's face value."""
+ return self._face
+
+
+class Deck:
+ """Deck of playing cards."""
+
+ SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
+ FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
+
+ def __init__(self, face_filter=None):
+ """Initializator."""
+ self.cards = []
+ faces = [x for x in self.FACES if not face_filter or x in face_filter]
+ for suit in self.SUITS:
+ for face in faces:
+ self.cards.append(Card(suit, face))
+
+ def cut(self):
+ '''Cut the deck.'''
+ # A valid cut contains at least one moved card, hence the ones below
+ index = random.randrange(1, len(self.cards) - 1)
+ self.cards = self.cards[index:] + self.cards[:index]
+
+ def shuffle(self):
+ '''Shuffle the deck.'''
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ '''Return the list of cards.'''
+ return self.cards
+
+
+class Player:
+ """Card player."""
+
+ def __init__(self):
+ """Initializator."""
+ self.cards = []
+
+ def get_cards(self):
+ """Return the list of cards that the player holds."""
+ return self.cards
+
+
+class Game:
+ """A game played with cards."""
+
+ def __init__(self, number_of_players, dealing_direction,
+ dealing_instructions):
+ """Initializator."""
+ self._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
+ self._deck = Deck()
+ self._players = []
+ for player in range(number_of_players):
+ self._players.append(Player())
+
+ def _get_players_dealing_flow(self, player):
+ """Get players in order ready for dealing, starting with player."""
+ player_order_step = 1 if self._dealing_direction == 'ltr' else -1
+ ordered_players = self._players[::player_order_step]
+ index = ordered_players.index(player)
+ return ordered_players[index:] + ordered_players[:index]
+
+ def _collect_cards_from_players(self):
+ """Collect the cards from all players."""
+ for player in self._players:
+ self._deck.cards.extend(player.cards)
+ player.cards = []
+
+ def get_players(self):
+ """Return the list of players, playing the game."""
+ return self._players
+
+ def prepare_deck(self):
+ """Prepare the deck for dealing."""
+ self._collect_cards_from_players()
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, player):
+ """Deal the cards to the players."""
+ player_flow = self._get_players_dealing_flow(player)
+ for card_count in self._dealing_instructions:
+ for player in player_flow:
+ player.cards.extend(self._deck.cards[:card_count])
+ self._deck.cards = self._deck.cards[card_count:]
+
+ def get_deck(self):
+ """Return the Deck used for the game."""
+ return self._deck
+
+
+class Belot(Game):
+ """Belot card game."""
+
+ def __init__(self):
+ """Initializator."""
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self._deck = Deck(face_filter=('7', '8', '9', '10',
+ 'J', 'Q', 'K', 'A'))
+
+
+class Poker(Game):
+ """Poker card game."""
+
+ def __init__(self):
+ """Initializator."""
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Георги обнови решението на 03.11.2022 15:52 (преди над 1 година)

import random
class Card:
"""Playing card."""
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
"""Get card's suit value."""
return self._suit
def get_face(self):
"""Get card's face value."""
return self._face
class Deck:
"""Deck of playing cards."""
SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
def __init__(self, face_filter=None):
"""Initializator."""
self.cards = []
faces = [x for x in self.FACES if not face_filter or x in face_filter]
for suit in self.SUITS:
for face in faces:
self.cards.append(Card(suit, face))
def cut(self):
- '''Cut the deck.'''
+ """Cut the deck."""
# A valid cut contains at least one moved card, hence the ones below
index = random.randrange(1, len(self.cards) - 1)
self.cards = self.cards[index:] + self.cards[:index]
def shuffle(self):
- '''Shuffle the deck.'''
+ """Shuffle the deck."""
random.shuffle(self.cards)
def get_cards(self):
- '''Return the list of cards.'''
+ """Return the list of cards."""
return self.cards
class Player:
"""Card player."""
def __init__(self):
"""Initializator."""
self.cards = []
def get_cards(self):
"""Return the list of cards that the player holds."""
return self.cards
class Game:
"""A game played with cards."""
def __init__(self, number_of_players, dealing_direction,
dealing_instructions):
"""Initializator."""
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._deck = Deck()
self._players = []
for player in range(number_of_players):
self._players.append(Player())
def _get_players_dealing_flow(self, player):
"""Get players in order ready for dealing, starting with player."""
player_order_step = 1 if self._dealing_direction == 'ltr' else -1
ordered_players = self._players[::player_order_step]
index = ordered_players.index(player)
return ordered_players[index:] + ordered_players[:index]
def _collect_cards_from_players(self):
"""Collect the cards from all players."""
for player in self._players:
self._deck.cards.extend(player.cards)
player.cards = []
def get_players(self):
"""Return the list of players, playing the game."""
return self._players
def prepare_deck(self):
"""Prepare the deck for dealing."""
self._collect_cards_from_players()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
"""Deal the cards to the players."""
player_flow = self._get_players_dealing_flow(player)
for card_count in self._dealing_instructions:
for player in player_flow:
player.cards.extend(self._deck.cards[:card_count])
self._deck.cards = self._deck.cards[card_count:]
def get_deck(self):
"""Return the Deck used for the game."""
return self._deck
class Belot(Game):
"""Belot card game."""
def __init__(self):
"""Initializator."""
super().__init__(4, 'ltr', (2, 3, 3))
self._deck = Deck(face_filter=('7', '8', '9', '10',
'J', 'Q', 'K', 'A'))
class Poker(Game):
"""Poker card game."""
def __init__(self):
"""Initializator."""
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Георги обнови решението на 11.11.2022 10:20 (преди над 1 година)

import random
+class CardHolderMixin:
+ """Perform common actions for an object holding cards."""
+
+ def __init__(self):
+ """Initializator."""
+ self._cards = []
+
+ def get_cards(self):
+ """Return the list of cards."""
+ return self._cards
+
+ def give_cards(self, cards):
+ """Extend the card pile with a set of cards."""
+ self._cards.extend(cards)
+
+ def take_cards(self, card_count=None):
+ """Get cards from the top of the cards pile."""
+ if card_count is None:
+ # No card_count means taking all the cards
+ card_count = len(self._cards)
+ taken_cards = self._cards[:card_count]
+ self._cards = self._cards[card_count:]
+ return taken_cards
+
+
class Card:
"""Playing card."""
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
"""Get card's suit value."""
return self._suit
def get_face(self):
"""Get card's face value."""
return self._face
-class Deck:
+class Deck(CardHolderMixin):
"""Deck of playing cards."""
SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
def __init__(self, face_filter=None):
"""Initializator."""
- self.cards = []
+ super().__init__()
faces = [x for x in self.FACES if not face_filter or x in face_filter]
for suit in self.SUITS:
for face in faces:
- self.cards.append(Card(suit, face))
+ self._cards.append(Card(suit, face))
def cut(self):
"""Cut the deck."""
# A valid cut contains at least one moved card, hence the ones below
- index = random.randrange(1, len(self.cards) - 1)
- self.cards = self.cards[index:] + self.cards[:index]
+ index = random.randrange(1, len(self._cards) - 1)
+ self._cards = self._cards[index:] + self._cards[:index]
def shuffle(self):
"""Shuffle the deck."""
- random.shuffle(self.cards)
-
- def get_cards(self):
- """Return the list of cards."""
- return self.cards
+ random.shuffle(self._cards)
-class Player:
+class Player(CardHolderMixin):
"""Card player."""
-
- def __init__(self):
- """Initializator."""
- self.cards = []
- def get_cards(self):
- """Return the list of cards that the player holds."""
- return self.cards
-
class Game:
"""A game played with cards."""
def __init__(self, number_of_players, dealing_direction,
dealing_instructions):
"""Initializator."""
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._deck = Deck()
self._players = []
for player in range(number_of_players):
self._players.append(Player())
def _get_players_dealing_flow(self, player):
"""Get players in order ready for dealing, starting with player."""
player_order_step = 1 if self._dealing_direction == 'ltr' else -1
ordered_players = self._players[::player_order_step]
index = ordered_players.index(player)
return ordered_players[index:] + ordered_players[:index]
def _collect_cards_from_players(self):
"""Collect the cards from all players."""
for player in self._players:
- self._deck.cards.extend(player.cards)
- player.cards = []
+ cards = player.take_cards()
+ self._deck.give_cards(cards)
def get_players(self):
"""Return the list of players, playing the game."""
return self._players
def prepare_deck(self):
"""Prepare the deck for dealing."""
self._collect_cards_from_players()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
"""Deal the cards to the players."""
player_flow = self._get_players_dealing_flow(player)
for card_count in self._dealing_instructions:
for player in player_flow:
- player.cards.extend(self._deck.cards[:card_count])
- self._deck.cards = self._deck.cards[card_count:]
+ cards = self._deck.take_cards(card_count)
+ player.give_cards(cards)
def get_deck(self):
"""Return the Deck used for the game."""
return self._deck
class Belot(Game):
"""Belot card game."""
def __init__(self):
"""Initializator."""
super().__init__(4, 'ltr', (2, 3, 3))
self._deck = Deck(face_filter=('7', '8', '9', '10',
'J', 'Q', 'K', 'A'))
class Poker(Game):
"""Poker card game."""
def __init__(self):
"""Initializator."""
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Георги обнови решението на 11.11.2022 11:13 (преди над 1 година)

import random
class CardHolderMixin:
"""Perform common actions for an object holding cards."""
def __init__(self):
"""Initializator."""
self._cards = []
def get_cards(self):
"""Return the list of cards."""
return self._cards
def give_cards(self, cards):
"""Extend the card pile with a set of cards."""
self._cards.extend(cards)
def take_cards(self, card_count=None):
"""Get cards from the top of the cards pile."""
if card_count is None:
# No card_count means taking all the cards
card_count = len(self._cards)
taken_cards = self._cards[:card_count]
self._cards = self._cards[card_count:]
return taken_cards
class Card:
"""Playing card."""
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
"""Get card's suit value."""
return self._suit
def get_face(self):
"""Get card's face value."""
return self._face
class Deck(CardHolderMixin):
"""Deck of playing cards."""
SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
def __init__(self, face_filter=None):
"""Initializator."""
super().__init__()
faces = [x for x in self.FACES if not face_filter or x in face_filter]
for suit in self.SUITS:
for face in faces:
self._cards.append(Card(suit, face))
def cut(self):
"""Cut the deck."""
# A valid cut contains at least one moved card, hence the ones below
- index = random.randrange(1, len(self._cards) - 1)
+ index = random.randrange(1, len(self._cards))
self._cards = self._cards[index:] + self._cards[:index]
def shuffle(self):
"""Shuffle the deck."""
random.shuffle(self._cards)
class Player(CardHolderMixin):
"""Card player."""
class Game:
"""A game played with cards."""
def __init__(self, number_of_players, dealing_direction,
dealing_instructions):
"""Initializator."""
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._deck = Deck()
self._players = []
for player in range(number_of_players):
self._players.append(Player())
def _get_players_dealing_flow(self, player):
"""Get players in order ready for dealing, starting with player."""
player_order_step = 1 if self._dealing_direction == 'ltr' else -1
ordered_players = self._players[::player_order_step]
index = ordered_players.index(player)
return ordered_players[index:] + ordered_players[:index]
def _collect_cards_from_players(self):
"""Collect the cards from all players."""
for player in self._players:
cards = player.take_cards()
self._deck.give_cards(cards)
def get_players(self):
"""Return the list of players, playing the game."""
return self._players
def prepare_deck(self):
"""Prepare the deck for dealing."""
self._collect_cards_from_players()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
"""Deal the cards to the players."""
player_flow = self._get_players_dealing_flow(player)
for card_count in self._dealing_instructions:
for player in player_flow:
cards = self._deck.take_cards(card_count)
player.give_cards(cards)
def get_deck(self):
"""Return the Deck used for the game."""
return self._deck
class Belot(Game):
"""Belot card game."""
def __init__(self):
"""Initializator."""
super().__init__(4, 'ltr', (2, 3, 3))
self._deck = Deck(face_filter=('7', '8', '9', '10',
'J', 'Q', 'K', 'A'))
class Poker(Game):
"""Poker card game."""
def __init__(self):
"""Initializator."""
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Георги обнови решението на 11.11.2022 11:14 (преди над 1 година)

import random
class CardHolderMixin:
"""Perform common actions for an object holding cards."""
def __init__(self):
"""Initializator."""
self._cards = []
def get_cards(self):
"""Return the list of cards."""
return self._cards
def give_cards(self, cards):
"""Extend the card pile with a set of cards."""
self._cards.extend(cards)
def take_cards(self, card_count=None):
"""Get cards from the top of the cards pile."""
if card_count is None:
# No card_count means taking all the cards
card_count = len(self._cards)
taken_cards = self._cards[:card_count]
self._cards = self._cards[card_count:]
return taken_cards
class Card:
"""Playing card."""
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
"""Get card's suit value."""
return self._suit
def get_face(self):
"""Get card's face value."""
return self._face
class Deck(CardHolderMixin):
"""Deck of playing cards."""
SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
def __init__(self, face_filter=None):
"""Initializator."""
super().__init__()
faces = [x for x in self.FACES if not face_filter or x in face_filter]
for suit in self.SUITS:
for face in faces:
self._cards.append(Card(suit, face))
def cut(self):
"""Cut the deck."""
- # A valid cut contains at least one moved card, hence the ones below
+ # A valid cut contains at least one moved card, hence the one below
index = random.randrange(1, len(self._cards))
self._cards = self._cards[index:] + self._cards[:index]
def shuffle(self):
"""Shuffle the deck."""
random.shuffle(self._cards)
class Player(CardHolderMixin):
"""Card player."""
class Game:
"""A game played with cards."""
def __init__(self, number_of_players, dealing_direction,
dealing_instructions):
"""Initializator."""
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._deck = Deck()
self._players = []
for player in range(number_of_players):
self._players.append(Player())
def _get_players_dealing_flow(self, player):
"""Get players in order ready for dealing, starting with player."""
player_order_step = 1 if self._dealing_direction == 'ltr' else -1
ordered_players = self._players[::player_order_step]
index = ordered_players.index(player)
return ordered_players[index:] + ordered_players[:index]
def _collect_cards_from_players(self):
"""Collect the cards from all players."""
for player in self._players:
cards = player.take_cards()
self._deck.give_cards(cards)
def get_players(self):
"""Return the list of players, playing the game."""
return self._players
def prepare_deck(self):
"""Prepare the deck for dealing."""
self._collect_cards_from_players()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
"""Deal the cards to the players."""
player_flow = self._get_players_dealing_flow(player)
for card_count in self._dealing_instructions:
for player in player_flow:
cards = self._deck.take_cards(card_count)
player.give_cards(cards)
def get_deck(self):
"""Return the Deck used for the game."""
return self._deck
class Belot(Game):
"""Belot card game."""
def __init__(self):
"""Initializator."""
super().__init__(4, 'ltr', (2, 3, 3))
self._deck = Deck(face_filter=('7', '8', '9', '10',
'J', 'Q', 'K', 'A'))
class Poker(Game):
"""Poker card game."""
def __init__(self):
"""Initializator."""
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))