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

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

Към профила на Йордан Глигоров

Резултати

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

Код

import random
STANDARD_FACE_FILTERS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
STANDARD_SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
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=STANDARD_FACE_FILTERS):
self.__face_filter = face_filter
self.__cards = []
self.__create_cards()
def __create_cards(self):
for face in self.__face_filter:
for suit in STANDARD_SUITS:
self.__cards.append(Card(suit, face))
def cut(self):
if len(self.__cards) > 0:
cut_size = random.randint(1, len(self.__cards))
for _ in range(cut_size):
card = self.__cards.pop()
self.__cards.insert(0, card)
else:
raise EmptyCardDeckError("The card deck is empty")
def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def deal_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
else:
raise EmptyCardDeckError()
class EmptyCardDeckError(Exception):
"""Raised when trying to get a card from an empty card deck"""
pass
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def throw_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
print("The player dosn't have any cards")
def remove_cards(self):
old_cards = list(self.__cards)
self.__cards.clear()
return old_cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions,
face_filter=STANDARD_FACE_FILTERS):
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._players = list(self._create_players())
self._card_deck = Deck(face_filter)
def _create_players(self):

Една идея, тъй като вече учихме генератори. Можеш да си спестиш дефинирането на временния списък players:
for _ in range(self._number_of_players): yield Player()

и просто евалюираш генератора по-горе:

self._players = list(self._create_players())
for _ in range (self._number_of_players):yield Player()
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
for card in player.remove_cards():
self._card_deck.add_card(card)
self._card_deck.shuffle()
self._card_deck.cut()
def deal(self, player):
player_position = self._players.index(player)
if self._dealing_direction == "ltr":
left = self._players[:player_position]
right = self._players[player_position:]
players_concatenated = right + left
self._deal_to_players(players_concatenated)
elif self._dealing_direction == "rtl":
left = self._players[player_position::-1]
right = self._players[:player_position:-1]
players_concatenated = left + right
self._deal_to_players(players_concatenated)
else:
raise ValueError("Invalid dealing direction")
def _deal_to_players(self, players):
for instruction in self._dealing_instructions:
for player in players:
for num_of_cards in range(instruction):
player.add_card(self._card_deck.deal_card())
def get_deck(self):
return self._card_deck;
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.155s

OK

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

Йордан обнови решението на 08.11.2022 17:48 (преди около 2 години)

+import random
+
+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 = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ self.__face_filter = face_filter
+ self.__cards = []
+ self.__create_cards()
+
+ def __create_cards(self):
+ for face in self.__face_filter:
+ for suit in ['clubs', 'diamonds', 'hearts', 'spides']:
+ self.__cards.append(Card(suit, face))
+
+ def cut(self):
+ if len(self.__cards) > 0:
+ card = self.__cards.pop()
+ self.__cards.insert(0, card)
+ else:
+ print("The card deck is empty")
+
+ def shuffle(self):
+ random.shuffle(self.__cards)
+
+ def get_cards(self):
+ return self.__cards
+
+ def add_card(self, card):
+ self.__cards.append(card)
+
+ def deal_card(self):
+ if len(self.__cards) > 0:
+ return self.__cards.pop()
+ print("The card deck is empty")
+
+
+class Player:
+ def __init__(self):
+ self.__cards = []
+
+ def get_cards(self):
+ return self.__cards
+
+ def add_card(self, card):
+ self.__cards.append(card)
+
+ def throw_card(self):
+ if len(self.__cards) > 0:
+ return self.__cards.pop()
+ print("The player dosn't have any cards")
+
+ def remove_cards(self):
+ old_cards = list(self.__cards)
+ self.__cards.clear()
+ return old_cards
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions,
+ face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+
+ self._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
+ self._players = self._create_players()
+ self._card_deck = Deck(face_filter)
+
+ def _create_players(self):
+ players = []
+ for i in range (0, self._number_of_players):
+ players.append(Player())
+ return players
+
+ def get_players(self):
+ return self._players
+
+ def prepare_deck(self):
+ for player in self._players:
+ for card in player.remove_cards():
+ self._card_deck.add_card(card)
+
+ self._card_deck.shuffle()
+ self._card_deck.cut()
+
+ def deal(self, player):
+ player_position = self._players.index(player)
+
+ if self._dealing_direction == "ltr":
+ left = self._players[:player_position]
+ right = self._players[player_position:]
+
+ self.deal_to_players(right)
+ self.deal_to_players(left)
+
+ elif self._dealing_direction == "rtl":
+ left = self._players[player_position: :-1]
+ right = self._players[ :player_position:-1]
+
+ self.deal_to_players(left)
+ self.deal_to_players(right)
+ else:
+ raise ValueError("Invalid dealing direction")
+
+ def deal_to_players(self, players):
+ for player in players:
+ for instruction in self._dealing_instructions:
+ for num_of_cards in range(instruction):
+ player.add_card(self._card_deck.deal_card())
+
+ def get_deck(self):
+ return self._card_deck;
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2,3,3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, "rtl", (1,1,1,1,1))

Йордан обнови решението на 08.11.2022 22:18 (преди около 2 години)

import random
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 = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.__face_filter = face_filter
self.__cards = []
self.__create_cards()
def __create_cards(self):
for face in self.__face_filter:
for suit in ['clubs', 'diamonds', 'hearts', 'spides']:
self.__cards.append(Card(suit, face))
def cut(self):
if len(self.__cards) > 0:
card = self.__cards.pop()
self.__cards.insert(0, card)
else:
print("The card deck is empty")

Това едва ли е най-удачният начин да го заявиш. Ако ще се подсигуряваш, че тестето не е празно, може би exception ще е по-добре. Или пък просто функцията да връща False. Не е проблем за текущата задача.

def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def deal_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
print("The card deck is empty")
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def throw_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
print("The player dosn't have any cards")
def remove_cards(self):
old_cards = list(self.__cards)
self.__cards.clear()
return old_cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions,
face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._players = self._create_players()
self._card_deck = Deck(face_filter)
def _create_players(self):

Една идея, тъй като вече учихме генератори. Можеш да си спестиш дефинирането на временния списък players:
for _ in range(self._number_of_players): yield Player()

и просто евалюираш генератора по-горе:

self._players = list(self._create_players())
players = []
for i in range (0, self._number_of_players):
players.append(Player())
return players
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
for card in player.remove_cards():
self._card_deck.add_card(card)
self._card_deck.shuffle()
self._card_deck.cut()
def deal(self, player):
player_position = self._players.index(player)
if self._dealing_direction == "ltr":
left = self._players[:player_position]
right = self._players[player_position:]
- self.deal_to_players(right)
- self.deal_to_players(left)
+ players_concatenated = right + left
+ self.deal_to_players(players_concatenated)
elif self._dealing_direction == "rtl":
left = self._players[player_position: :-1]
right = self._players[ :player_position:-1]
- self.deal_to_players(left)
- self.deal_to_players(right)
+ players_concatenated = left + right
+ self.deal_to_players(players_concatenated)
else:
raise ValueError("Invalid dealing direction")
def deal_to_players(self, players):
- for player in players:
- for instruction in self._dealing_instructions:
+ for instruction in self._dealing_instructions:
+ for player in players:
for num_of_cards in range(instruction):
player.add_card(self._card_deck.deal_card())
def get_deck(self):
return self._card_deck;
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2,3,3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1,1,1,1,1))

Йордан обнови решението на 11.11.2022 14:08 (преди около 2 години)

import random
+STANDARD_FACE_FILTERS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+STANDARD_SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
+
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 = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ def __init__(self, face_filter=STANDARD_FACE_FILTERS):
self.__face_filter = face_filter
self.__cards = []
self.__create_cards()
def __create_cards(self):
for face in self.__face_filter:
- for suit in ['clubs', 'diamonds', 'hearts', 'spides']:
+ for suit in STANDARD_SUITS:
self.__cards.append(Card(suit, face))
def cut(self):
if len(self.__cards) > 0:
- card = self.__cards.pop()
- self.__cards.insert(0, card)
+ #dali cut size treba da e len(self.__cards) - 1?
+ cut_size = random.randint(1, len(self.__cards))
+ for _ in range(cut_size):
+ card = self.__cards.pop()
+ self.__cards.insert(0, card)
else:
- print("The card deck is empty")
+ raise EmptyCardDeckError("The card deck is empty")
def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def deal_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
- print("The card deck is empty")
+ else:
+ raise EmptyCardDeckError()
+class EmptyCardDeckError(Exception):
+ """Raised when trying to get a card from an empty card deck"""
+ pass
+
+
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def throw_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
print("The player dosn't have any cards")
def remove_cards(self):
old_cards = list(self.__cards)
self.__cards.clear()
return old_cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions,
- face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ face_filter=STANDARD_FACE_FILTERS):
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
- self._players = self._create_players()
+ self._players = list(self._create_players())
self._card_deck = Deck(face_filter)
-
+
def _create_players(self):
- players = []
- for i in range (0, self._number_of_players):
- players.append(Player())
- return players
+ for _ in range (self._number_of_players):yield Player()
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
for card in player.remove_cards():
self._card_deck.add_card(card)
self._card_deck.shuffle()
self._card_deck.cut()
def deal(self, player):
player_position = self._players.index(player)
if self._dealing_direction == "ltr":
left = self._players[:player_position]
right = self._players[player_position:]
players_concatenated = right + left
- self.deal_to_players(players_concatenated)
+ self._deal_to_players(players_concatenated)
elif self._dealing_direction == "rtl":
- left = self._players[player_position: :-1]
- right = self._players[ :player_position:-1]
+ left = self._players[player_position::-1]
+ right = self._players[:player_position:-1]
players_concatenated = left + right
- self.deal_to_players(players_concatenated)
+ self._deal_to_players(players_concatenated)
else:
raise ValueError("Invalid dealing direction")
- def deal_to_players(self, players):
+ def _deal_to_players(self, players):
for instruction in self._dealing_instructions:
for player in players:
for num_of_cards in range(instruction):
player.add_card(self._card_deck.deal_card())
def get_deck(self):
return self._card_deck;
class Belot(Game):
def __init__(self):
- super().__init__(4, "ltr", (2,3,3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+ super().__init__(4, "ltr", (2, 3, 3), ['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, "rtl", (1, 1, 1, 1, 1))

Йордан обнови решението на 12.11.2022 19:47 (преди около 2 години)

import random
STANDARD_FACE_FILTERS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
STANDARD_SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
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=STANDARD_FACE_FILTERS):
self.__face_filter = face_filter
self.__cards = []
self.__create_cards()
def __create_cards(self):
for face in self.__face_filter:
for suit in STANDARD_SUITS:
self.__cards.append(Card(suit, face))
def cut(self):
if len(self.__cards) > 0:
- #dali cut size treba da e len(self.__cards) - 1?
cut_size = random.randint(1, len(self.__cards))
for _ in range(cut_size):
card = self.__cards.pop()
self.__cards.insert(0, card)
else:
raise EmptyCardDeckError("The card deck is empty")
def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def deal_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
else:
raise EmptyCardDeckError()
class EmptyCardDeckError(Exception):
"""Raised when trying to get a card from an empty card deck"""
pass
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_card(self, card):
self.__cards.append(card)
def throw_card(self):
if len(self.__cards) > 0:
return self.__cards.pop()
print("The player dosn't have any cards")
def remove_cards(self):
old_cards = list(self.__cards)
self.__cards.clear()
return old_cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions,
face_filter=STANDARD_FACE_FILTERS):
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._players = list(self._create_players())
self._card_deck = Deck(face_filter)
def _create_players(self):
for _ in range (self._number_of_players):yield Player()
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
for card in player.remove_cards():
self._card_deck.add_card(card)
self._card_deck.shuffle()
self._card_deck.cut()
def deal(self, player):
player_position = self._players.index(player)
if self._dealing_direction == "ltr":
left = self._players[:player_position]
right = self._players[player_position:]
players_concatenated = right + left
self._deal_to_players(players_concatenated)
elif self._dealing_direction == "rtl":
left = self._players[player_position::-1]
right = self._players[:player_position:-1]
players_concatenated = left + right
self._deal_to_players(players_concatenated)
else:
raise ValueError("Invalid dealing direction")
def _deal_to_players(self, players):
for instruction in self._dealing_instructions:
for player in players:
for num_of_cards in range(instruction):
player.add_card(self._card_deck.deal_card())
def get_deck(self):
return self._card_deck;
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))