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

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

Към профила на Иван Досев

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 14 успешни тест(а)
  • 1 неуспешни тест(а)

Код

import random
from typing import List, Tuple
suits = ['clubs', 'spades', 'diamonds', 'hearts']
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
class Card:
def __init__(self, suit: str, face: str):
self._suit = suit
self.face = face
def get_suit(self) -> str:
return self._suit
def get_face(self) -> str:
return self.face
class Deck:
def __init__(self, face_filter=faces):
self.cards: List[Card] = []
for suit in suits:
for face in faces:
if face in face_filter:
card = Card(suit, face)
self.cards.append(card)
def cut(self):
num = random.randint(0, len(self.cards))
for _ in range(num):
self.cards.append(self.cards.pop(0))
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self) -> List[Card]:
return self.cards
def set_cards(self, cards):
self.cards = cards
class Player:
def __init__(self):
self.cards: List[Card] = []
def get_cards(self):
return self.cards
def add_card(self, card: Card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players: int, dealing_direction: str, dealing_instructions: Tuple[int]):
self.deck: Deck = Deck()
self.original_deck = self.deck.get_cards().copy()
self.number_of_players = number_of_players
self.players: List[Player] = []
for i in range(0, self.number_of_players):
self.players.append(Player())
self.dealing_directions = dealing_direction
self.dealing_instructions = dealing_instructions
def get_players(self) -> List[Player]:
return self.players
def prepare_deck(self):
self.deck.set_cards(self.original_deck.copy())
self.deck.shuffle()
self.deck.cut()
def get_deck(self) -> Deck:
return self.deck
def set_deck(self, deck: Deck):
self.deck = deck
self.original_deck = deck.get_cards().copy()
def deal(self, player: Player):
first_player_index = -1
for index, curr_player in enumerate(self.players):
if curr_player == player:
first_player_index = index
break
for card_num in self.dealing_instructions:
if self.dealing_directions == 'ltr':
for index in range(first_player_index, len(self.players)):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
for index in range(first_player_index):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
if self.dealing_directions == 'rtl':
for index in range(first_player_index, -1, -1):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
for index in range(len(self.players) - 1, first_player_index, -1):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.set_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......
======================================================================
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 0xffff8cf863b0>,[79 chars]6b0>] != []

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

+ []
- [<solution.Card object at 0xffff8cf863b0>,
-  <solution.Card object at 0xffff8cf862f0>,
-  <solution.Card object at 0xffff8cf866b0>]

----------------------------------------------------------------------
Ran 15 tests in 0.188s

FAILED (failures=1)

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

Иван обнови решението на 14.11.2022 00:34 (преди около 2 години)

+import random
+
+from typing import List, Tuple
+
+#I have no idea if I can import. Please let me know if I can't, so that I can fix any issues :D
+
+suits = ['clubs', 'spades', 'diamonds', 'hearts']
+faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+
+class Card:
+ def __init__(self, suit: str, face: str):
+ self.suit = suit
+ self.face = face
+
+ def get_suit(self) -> str:
+ return self.suit
+
+ def get_face(self) -> str:
+ return self.face
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ if face_filter is None:

Тук го направих така, защото pycharm ми го даде като подобрение, и предполагам е по-добре, защото ако се подаде None да му присвоим стойност. По дефолт ако пак не му се подаде нещо, пак да присвои faces

Кой съм аз да споря с Pycharm тогава :D
Шегата на страна. И двете за мен са ок. Просто ако добавиш стойност по подразбиране, пестиш код. Може би генералната практика да е None на Pycharm е логична, защото това, което подаваш е mutable и може да се счупи, ако не внимаваш, но за мен и двете са напълно безопасни.

+ face_filter = faces
+ self.cards: List[Card] = []
+ for suit in suits:
+ for face in faces:
+ if face in face_filter:
+ card = Card(suit, face)
+ self.cards.append(card)
+
+ def cut(self):
+ num = random.randint(0, len(self.cards))
+ for i in range(0, num):
+ self.cards.append(self.cards.pop(0))
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self) -> List[Card]:
+ return self.cards
+
+
+class Player:
+ def __init__(self):
+ self.cards: List[Card] = []
+
+ def get_cards(self):
+ return self.cards
+
+ def add_card(self, card: Card):
+ self.cards.append(card)
+
+
+class Game:
+ def __init__(self, number_of_players: int, dealing_direction: str, dealing_instructions: Tuple[int]):
+ self.deck: Deck = Deck()
+ self.number_of_players = number_of_players
+ self.players: List[Player] = []
+ for i in range(0, self.number_of_players):
+ self.players.append(Player())
+ self.dealing_directions = dealing_direction
+ self.dealing_instructions = dealing_instructions
+
+ def get_players(self) -> List[Player]:
+ return self.players
+
+ def prepare_deck(self):
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def get_deck(self) -> Deck:
+ return self.deck
+
+ def set_deck(self, deck: Deck):
+ self.deck = deck
+
+ def deal(self, player: Player):
+ res_index = -1
+ for index, curr_player in enumerate(self.players):
+ if curr_player == player:
+ res_index = index
+ break
+ for card_num in self.dealing_instructions:
+ if self.dealing_directions == 'ltr':
+ for index in range(res_index, len(self.players)):
+ for curr_num in range(card_num):
+ self.players[index].add_card(self.deck.get_cards().pop(0))
+ for index in range(res_index):
+ for curr_num in range(card_num):
+ self.players[index].add_card(self.deck.get_cards().pop(0))
+ if self.dealing_directions == 'rtl':
+ for index in range(res_index, -1, -1):
+ for curr_num in range(card_num):
+ self.players[index].add_card(self.deck.get_cards().pop(0))
+ for index in range(len(self.players) - 1, res_index, -1):
+ for curr_num in range(card_num):
+ self.players[index].add_card(self.deck.get_cards().pop(0))
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self.set_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 18:25 (преди около 2 години)

import random
from typing import List, Tuple
-#I have no idea if I can import. Please let me know if I can't, so that I can fix any issues :D
-
suits = ['clubs', 'spades', 'diamonds', 'hearts']
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
class Card:
def __init__(self, suit: str, face: str):
- self.suit = suit
+ self._suit = suit
self.face = face
def get_suit(self) -> str:
- return self.suit
+ return self._suit
def get_face(self) -> str:
return self.face
class Deck:
- def __init__(self, face_filter=None):
- if face_filter is None:
- face_filter = faces
+ def __init__(self, face_filter=faces):
self.cards: List[Card] = []
for suit in suits:
for face in faces:
if face in face_filter:
card = Card(suit, face)
self.cards.append(card)
def cut(self):
num = random.randint(0, len(self.cards))
- for i in range(0, num):
+ for _ in range(num):
self.cards.append(self.cards.pop(0))
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self) -> List[Card]:
return self.cards
+ def set_cards(self, cards):
+ self.cards = cards
+
class Player:
def __init__(self):
self.cards: List[Card] = []
def get_cards(self):
return self.cards
def add_card(self, card: Card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players: int, dealing_direction: str, dealing_instructions: Tuple[int]):
self.deck: Deck = Deck()
+ self.original_deck = self.deck.get_cards().copy()
self.number_of_players = number_of_players
self.players: List[Player] = []
for i in range(0, self.number_of_players):
self.players.append(Player())
self.dealing_directions = dealing_direction
self.dealing_instructions = dealing_instructions
def get_players(self) -> List[Player]:
return self.players
def prepare_deck(self):
+ self.deck.set_cards(self.original_deck.copy())
self.deck.shuffle()
self.deck.cut()
def get_deck(self) -> Deck:
return self.deck
def set_deck(self, deck: Deck):
self.deck = deck
+ self.original_deck = deck.get_cards().copy()
def deal(self, player: Player):
- res_index = -1
+ first_player_index = -1
for index, curr_player in enumerate(self.players):
if curr_player == player:
- res_index = index
+ first_player_index = index
break
for card_num in self.dealing_instructions:
if self.dealing_directions == 'ltr':
- for index in range(res_index, len(self.players)):
+ for index in range(first_player_index, len(self.players)):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
- for index in range(res_index):
+ for index in range(first_player_index):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
if self.dealing_directions == 'rtl':
- for index in range(res_index, -1, -1):
+ for index in range(first_player_index, -1, -1):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
- for index in range(len(self.players) - 1, res_index, -1):
+ for index in range(len(self.players) - 1, first_player_index, -1):
for curr_num in range(card_num):
self.players[index].add_card(self.deck.get_cards().pop(0))
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.set_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))
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))