Решение на От ливадите до Лас Вегас (и назад) от Назифе Алиджик

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

Към профила на Назифе Алиджик

Резултати

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

Код

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=None):
self.face_filter = (
face_filter
if face_filter is not None
else ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
)
self.cards = []
self.suits = ("clubs", "diamonds", "hearts", "spades")
for self.suit in self.suits:
for self.face in self.face_filter:
self.cards.append(Card(self.suit, self.face))
def cut(self):
random_number_to_cut = random.randint(0, len(self.cards) - 1)
self.cards = self.cards[random_number_to_cut:] + \
self.cards[:random_number_to_cut]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def pop(self):
self.cards.pop()
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 = []
for _ in range(number_of_players):
self.players.append(Player())
self.deck = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
for self.player in self.players:
self.deck.get_cards().extend(self.player.get_cards())
self.player.get_cards().clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
return self.deck
def deal(self, player):
index_to_start = self.players.index(player)
if self.dealing_direction == "rtl":
for instruction in self.dealing_instructions:
for player in self.players[:index_to_start + 1][::-1]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
for player in self.players[:index_to_start:-1]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
else:
for instruction in self.dealing_instructions:
for player in self.players[index_to_start:]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
for player in self.players[:index_to_start]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
class Belot(Game):
def __init__(self):
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
super().__init__(4, "ltr", (2, 3, 3))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

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

FE.......EE...E
======================================================================
ERROR: test_correct_direction_and_players_deal (test.TestBelot)
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
AttributeError: 'NoneType' object has no attribute 'get_suit'

======================================================================
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_suit'

======================================================================
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_suit'

======================================================================
ERROR: 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
AttributeError: 'NoneType' object has no attribute 'get_suit'

======================================================================
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 second set but not the first:
('spades', '5')
('diamonds', '3')
('spades', '6')
('hearts', '5')
('spades', '4')
('clubs', '5')
('hearts', '6')
('clubs', '6')
('diamonds', '5')
('clubs', '4')
('hearts', '4')
('diamonds', '6')
('spades', '2')
('diamonds', '4')
('spades', '3')
('clubs', '2')
('hearts', '2')
('clubs', '3')
('hearts', '3')
('diamonds', '2')

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

FAILED (failures=1, errors=4)

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

Назифе обнови решението на 11.11.2022 11: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=None):
+ self.face_filter = (
+ face_filter
+ if face_filter is not None
+ else ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+ )
+ self.cards = []
+ self.suits = ("clubs", "diamonds", "hearts", "spades")
+ for self.suit in self.suits:
+ for self.face in self.face_filter:
+ self.card = Card(self.suit, self.face)
+ self.cards.append(self.card)
+
+ def cut(self):
+ self.cards = self.cards[1:] + self.cards[:1]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def pop(self):
+ self.cards.pop()
+
+
+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 = []
+ for player in range(number_of_players):
+ player = Player()
+ self.players.append(player)
+ self.deck = Deck()
+ if self.dealing_direction == "rtl":
+ self.players.reverse()
+ for instruction in self.dealing_instructions:
+ for self.player in self.players:
+ dealing_instruction = instruction
+ for i in range(dealing_instruction):
+ self.player.get_cards().append(self.deck.pop())
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for self.player in self.players:
+ self.deck.get_cards().extend(self.player.get_cards())
+ self.player.get_cards().clear()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def get_deck(self):
+ return self.deck.get_cards()
+
+ def deal(self, player):
+ index_to_start = 0
+ for index, self.player in enumerate(self.players):
+ if player is self.player:
+ index_to_start = index
+ break
+ self.players = (
+ self.players[index_to_start - 1:] +
+ self.players[: index_to_start - 1]
+ )
+ for instruction in self.dealing_instructions:
+ for self.player in self.players:
+ dealing_instruction = instruction
+ for i in range(dealing_instruction):
+ self.player.get_cards().append(self.deck.pop())
+
+
+class Belot(Game):
+ def __init__(self):
+ self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
+ super().__init__(4, "ltr", (2, 3, 3))
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))

Назифе обнови решението на 11.11.2022 11:52 (преди около 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=None):
self.face_filter = (
face_filter
if face_filter is not None
else ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
)
self.cards = []
self.suits = ("clubs", "diamonds", "hearts", "spades")
for self.suit in self.suits:
for self.face in self.face_filter:
self.card = Card(self.suit, self.face)

self.card е излишно, а и нелогично. Тестете има карти, които вече събираш, но тази временно променлива ще остане налична. По-добре направо:
self.cards.append(Card(self.suit, self.face))

self.cards.append(self.card)
def cut(self):
self.cards = self.cards[1:] + self.cards[:1]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def pop(self):
self.cards.pop()
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 = []
for player in range(number_of_players):
player = Player()
self.players.append(player)
self.deck = Deck()
if self.dealing_direction == "rtl":
self.players.reverse()

Не смятам за редно да обръщаш самите играчи. Просто се убеди, че кодът ти по-долу в този метод ги използва наобратно, но без да ги разместваш в паметта.

for instruction in self.dealing_instructions:
for self.player in self.players:
dealing_instruction = instruction
for i in range(dealing_instruction):
self.player.get_cards().append(self.deck.pop())
def get_players(self):
return self.players
def prepare_deck(self):
for self.player in self.players:
self.deck.get_cards().extend(self.player.get_cards())
self.player.get_cards().clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
return self.deck.get_cards()
def deal(self, player):
+ if self.dealing_direction == "rtl":
+ self.players.reverse()
index_to_start = 0
for index, self.player in enumerate(self.players):

self.players.index(player) ти дава индекса, който търсиш.

Забележи, че не трябва да е self.player, а само player. player е това, което методът ти получава. self.player е атрибут, който въобще не ти трябва да този клса, но е страничен ефект от ред 65.

if player is self.player:
index_to_start = index
break
self.players = (
self.players[index_to_start - 1:] +
self.players[: index_to_start - 1]
)
for instruction in self.dealing_instructions:
for self.player in self.players:
dealing_instruction = instruction
for i in range(dealing_instruction):
self.player.get_cards().append(self.deck.pop())
class Belot(Game):
def __init__(self):
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
super().__init__(4, "ltr", (2, 3, 3))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))
+

Назифе обнови решението на 13.11.2022 18:20 (преди около 2 години)

import random
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=None):
self.face_filter = (
face_filter
if face_filter is not None
else ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
)
self.cards = []
self.suits = ("clubs", "diamonds", "hearts", "spades")
for self.suit in self.suits:
for self.face in self.face_filter:
- self.card = Card(self.suit, self.face)
- self.cards.append(self.card)
+ self.cards.append(Card(self.suit, self.face))
def cut(self):
- self.cards = self.cards[1:] + self.cards[:1]
+ random_number_to_cut = random.randint(0, len(self.cards) - 1)
+ self.cards = self.cards[random_number_to_cut:] + \
+ self.cards[:random_number_to_cut]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def pop(self):
self.cards.pop()
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 = []
- for player in range(number_of_players):
- player = Player()
- self.players.append(player)
+ for _ in range(number_of_players):
+ self.players.append(Player())
self.deck = Deck()
- if self.dealing_direction == "rtl":
- self.players.reverse()
- for instruction in self.dealing_instructions:
- for self.player in self.players:
- dealing_instruction = instruction
- for i in range(dealing_instruction):
- self.player.get_cards().append(self.deck.pop())
def get_players(self):
return self.players
def prepare_deck(self):
for self.player in self.players:
self.deck.get_cards().extend(self.player.get_cards())
self.player.get_cards().clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
- return self.deck.get_cards()
+ return self.deck
def deal(self, player):
+ index_to_start = self.players.index(player)
if self.dealing_direction == "rtl":
- self.players.reverse()
- index_to_start = 0
- for index, self.player in enumerate(self.players):
- if player is self.player:
- index_to_start = index
- break
- self.players = (
- self.players[index_to_start - 1:] +
- self.players[: index_to_start - 1]
- )
- for instruction in self.dealing_instructions:
- for self.player in self.players:
- dealing_instruction = instruction
- for i in range(dealing_instruction):
- self.player.get_cards().append(self.deck.pop())
+ for instruction in self.dealing_instructions:
+ for player in reversed(self.players[:index_to_start + 1]):
+ for _ in range(instruction):
+ player.get_cards().append(self.deck.pop())
+ for instruction in self.dealing_instructions:
+ for l in reversed(self.players[index_to_start + 1:]):
+ for _ in range(instruction):
+ player.get_cards().append(self.deck.pop())
+ else:
+ for instruction in self.dealing_instructions:
+ for player in self.players[index_to_start:]:
+ for _ in range(instruction):
+ player.get_cards().append(self.deck.pop())
+ for instruction in self.dealing_instructions:
+ for player in self.players[:index_to_start]:
+ for _ in range(instruction):
+ player.get_cards().append(self.deck.pop())
class Belot(Game):
def __init__(self):
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
super().__init__(4, "ltr", (2, 3, 3))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))
-

Назифе обнови решението на 14.11.2022 14:02 (преди около 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=None):
self.face_filter = (
face_filter
if face_filter is not None
else ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
)
self.cards = []
self.suits = ("clubs", "diamonds", "hearts", "spades")
for self.suit in self.suits:
for self.face in self.face_filter:
self.cards.append(Card(self.suit, self.face))
def cut(self):
random_number_to_cut = random.randint(0, len(self.cards) - 1)
self.cards = self.cards[random_number_to_cut:] + \
self.cards[:random_number_to_cut]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def pop(self):
self.cards.pop()
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 = []
for _ in range(number_of_players):
self.players.append(Player())
self.deck = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
for self.player in self.players:
self.deck.get_cards().extend(self.player.get_cards())
self.player.get_cards().clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
return self.deck
def deal(self, player):
index_to_start = self.players.index(player)
if self.dealing_direction == "rtl":
for instruction in self.dealing_instructions:
- for player in reversed(self.players[:index_to_start + 1]):
+ for player in self.players[:index_to_start + 1][::-1]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
- for instruction in self.dealing_instructions:
- for l in reversed(self.players[index_to_start + 1:]):
+ for player in self.players[:index_to_start:-1]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
else:
for instruction in self.dealing_instructions:
for player in self.players[index_to_start:]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
- for instruction in self.dealing_instructions:
for player in self.players[:index_to_start]:
for _ in range(instruction):
player.get_cards().append(self.deck.pop())
class Belot(Game):
def __init__(self):
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
super().__init__(4, "ltr", (2, 3, 3))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))