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

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

Към профила на Минчо Христов

Резултати

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

Код

from random import randint, shuffle
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.cards = []
for face in face_filter or \
[
"2", "3", "4",
"5", "6", "7",
"8", "9", "10",
"J", "Q", "K", "A"
]:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
self.cards.append(Card(suit, face))
# im still gona leave my code as a comment
# def insert(face_to_add):
# self.cards.extend(
# [
# Card("clubs", face_to_add),
# Card("diamonds", face_to_add),
# Card("hearts", face_to_add),
# Card("spades", face_to_add),
# ]
# )
#
# if face_filter:
# for face in face_filter:
# insert(face)
# else:
# for face in range(1, 14):
# match face:
# case _ as tmp if 2 <= tmp <= 10:
# insert(str(tmp))
# case 1:
# insert("A")
# case 11:
# insert("J")
# case 12:
# insert("Q")
# case 13:
# insert("K")
def cut(self):
num = randint(1, len(self.cards) - 1)
self.cards = self.cards[num:] + self.cards[:num]
def __getitem__(self, item):
return self.cards[item]
def get_cards(self):
return self.cards
def shuffle(self):
shuffle(self.cards)
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def take_cards(self, cards):
self._cards.extend(cards)
def give_away_cards(self):
cards_to_give = self._cards
self._cards = []
return cards_to_give
class Game:
def __init__(self, num_of_players, deal_dir, deal_inst, cards_to_deal=None):
self._players = [Player() for _ in range(0, num_of_players)]
self._deal_dir = deal_dir.lower()
self._deal_inst = deal_inst
self._deck = Deck(cards_to_deal)
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck.cards.extend(player.give_away_cards())
self._deck.shuffle()
self._deck.cut()
def get_deck(self):
return self._deck
def deal(self, player):
start_pos = self._players.index(player)
self._players = self._players[start_pos:] + self._players[:start_pos]
if self._deal_dir == "rtl":
tmp_lst = self._players[1:]
tmp_lst.reverse()
del self._players[1:]
self._players.extend(tmp_lst)
for to_deal in self._deal_inst:
for player_to_be_delt_to in self._players:
to_remove_and_deal = self._deck[:to_deal]
player_to_be_delt_to.take_cards(to_remove_and_deal)
del self._deck.cards[:to_deal]
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3), range(7, 15))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

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

F..............
======================================================================
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:
('diamonds', '10')
('hearts', '9')
('spades', '8')
('hearts', '10')
('spades', 'J')
('diamonds', 'A')
('hearts', 'A')
('clubs', '7')
('diamonds', '8')
('hearts', '8')
('diamonds', 'J')
('spades', '7')
('hearts', 'J')
('clubs', 'Q')
('diamonds', '7')
('clubs', 'K')
('hearts', '7')
('clubs', '9')
('clubs', '10')
('spades', 'Q')
('spades', '9')
('spades', 'K')
('clubs', 'A')
('clubs', '8')
('spades', '10')
('diamonds', 'Q')
('hearts', 'Q')
('diamonds', '9')
('diamonds', 'K')
('spades', 'A')
('hearts', 'K')
('clubs', 'J')
Items in the second set but not the first:
('spades', 9)
('spades', 12)
('clubs', 8)
('clubs', 14)
('diamonds', 9)
('clubs', 11)
('hearts', 9)
('diamonds', 12)
('hearts', 12)
('spades', 11)
('spades', 8)
('spades', 14)
('clubs', 7)
('diamonds', 8)
('clubs', 10)
('diamonds', 11)
('clubs', 13)
('diamonds', 14)
('hearts', 8)
('hearts', 14)
('hearts', 11)
('spades', 7)
('spades', 13)
('spades', 10)
('clubs', 12)
('diamonds', 7)
('clubs', 9)
('diamonds', 13)
('hearts', 7)
('diamonds', 10)
('hearts', 10)
('hearts', 13)

----------------------------------------------------------------------
Ran 15 tests in 0.166s

FAILED (failures=1)

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

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

+from random import randint, shuffle
+
+
+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
+
+ # def __str__(self):
+ # return f"{self.suit} {self.face}"
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ self.cards = []
+
+ def insert(face_to_add):

Цялата тази логика можеш да постигнеш с два вложени цикъла и хардкоуднат списък с карти. Прекалено абстрактно решение.

self.cards = []
for face in face_filter or ['1', '2', ...]:
    for suit in ['clubs', ...]:
        self.cards.append(Card(face, suit))
+ self.cards.extend(
+ [
+ Card("clubs", face_to_add),
+ Card("diamonds", face_to_add),
+ Card("hearts", face_to_add),
+ Card("spades", face_to_add),
+ ]
+ )
+
+ if face_filter:
+ for face in face_filter:
+ insert(face)
+ else:
+ for face in range(1, 13):
+ match face:
+ case _ as tmp if 2 <= tmp <= 10:
+ insert(str(tmp))
+ case 1:
+ insert("A")
+ case 11:
+ insert("J")
+ case 12:
+ insert("Q")
+ case 13:
+ insert("K")
+
+ def cut(self, num=randint(1, 52)):
+
+ lst_l = self.cards[:num]
+ lst_r = self.cards[num:]
+ self.cards.clear()
+ self.cards = lst_r
+ self.cards.extend(lst_l)
+
+ def get_cards(self):
+ return self.cards
+
+ def shuffle(self):
+ shuffle(self.cards)
+
+ # def __str__(self):
+ # cards = ""
+ # for card in self.cards:
+ # cards += f"{card} "
+ # return f"{cards}"
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+ def take_cards(self, cards):
+ self.cards.extend(cards)
+
+ def give_away_cards(self):
+ cards_to_give = self.cards
+ self.cards = []
+ return cards_to_give
+
+ # def __str__(self):
+ # cards = ""
+ # for card in self.cards:
+ # cards += f"{card} "
+ # return f"{cards}"
+
+
+class Game:
+ def __init__(self, num_of_players, deal_dir, deal_inst, cards_to_deal=None):
+ self.players = [Player() for _ in range(num_of_players)]
+ self.deal_dir = deal_dir.lower()
+ self.deal_inst = deal_inst
+ self.deck = Deck(cards_to_deal)
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+
+ for player in self.players:
+ self.deck.cards.extend(player.give_away_cards())
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def get_deck(self):
+ return self.deck
+
+ def deal(self, player):
+
+ start_pos = 0
+ for cur_player in self.players:
+ if cur_player is player:
+ break
+ start_pos += 1
+ # reoredering pleyers
+ lst_l = self.players[:start_pos]
+ lst_r = self.players[start_pos:]
+ self.players.clear()
+ self.players = lst_r
+ self.players.extend(lst_l)
+
+ c1 = 0

Бих обходил самите играчи, а не индексите. Сега ми е трудно да проследя какво се случва, а и променливи от типа c1, c2 не помагат много. Сърествам те да направиш променлива с играчи, в реда, в който ще трябва да се обходят за раздаване, след което просто да я обходиш.

+ to_be_delt = len(self.deal_inst)
+ if self.deal_dir == "ltr":
+ mover = 1
+ last_pl = self.players[-1]
+ else:
+ mover = -1
+ last_pl = self.players[1]
+
+ while c1 != to_be_delt:
+ c2 = 0
+ while self.players[c2] is last_pl:
+ """add if out of cards"""
+ to_remove = self.deck[: self.deal_inst[c1]]
+ self.players[c2].take_cards(to_remove)
+ del self.deck.cards[: self.deal_inst[c1]]
+ c2 += mover
+ c1 += 1
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3), range(7, 13))
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))

Минчо обнови решението на 12.11.2022 20:36 (преди около 2 години)

from random import randint, shuffle
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
- # def __str__(self):
- # return f"{self.suit} {self.face}"
-
class Deck:
def __init__(self, face_filter=None):
self.cards = []
- def insert(face_to_add):
- self.cards.extend(
+ for face in face_filter or \
[
- Card("clubs", face_to_add),
- Card("diamonds", face_to_add),
- Card("hearts", face_to_add),
- Card("spades", face_to_add),
- ]
- )
+ "1", "2", "3",
+ "4", "5", "6",
+ "7", "8", "9",
+ "10", "J", "Q",
+ "K", "A"
+ ]:
+ for suit in ["clubs", "diamonds", "hearts", "spades"]:
+ self.cards.append(Card(face, suit))
+ # im still gona leave my code as a comment
+ # def insert(face_to_add):
+ # self.cards.extend(
+ # [
+ # Card("clubs", face_to_add),
+ # Card("diamonds", face_to_add),
+ # Card("hearts", face_to_add),
+ # Card("spades", face_to_add),
+ # ]
+ # )
+ #
+ # if face_filter:
+ # for face in face_filter:
+ # insert(face)
+ # else:
+ # for face in range(1, 14):
+ # match face:
+ # case _ as tmp if 2 <= tmp <= 10:
+ # insert(str(tmp))
+ # case 1:
+ # insert("A")
+ # case 11:
+ # insert("J")
+ # case 12:
+ # insert("Q")
+ # case 13:
+ # insert("K")
- if face_filter:
- for face in face_filter:
- insert(face)
- else:
- for face in range(1, 13):
- match face:
- case _ as tmp if 2 <= tmp <= 10:
- insert(str(tmp))
- case 1:
- insert("A")
- case 11:
- insert("J")
- case 12:
- insert("Q")
- case 13:
- insert("K")
+ def cut(self):
+ num = randint(1, len(self.cards) - 1)
+ self.cards = self.cards[num:] + self.cards[:num]
- def cut(self, num=randint(1, 52)):
-
- lst_l = self.cards[:num]
- lst_r = self.cards[num:]
- self.cards.clear()
- self.cards = lst_r
- self.cards.extend(lst_l)
-
def get_cards(self):
return self.cards
def shuffle(self):
shuffle(self.cards)
- # def __str__(self):
- # cards = ""
- # for card in self.cards:
- # cards += f"{card} "
- # return f"{cards}"
-
class Player:
def __init__(self):
- self.cards = []
+ self._cards = []
def get_cards(self):
- return self.cards
+ return self._cards
def take_cards(self, cards):
- self.cards.extend(cards)
+ self._cards.extend(cards)
def give_away_cards(self):
- cards_to_give = self.cards
- self.cards = []
+ cards_to_give = self._cards
+ self._cards = []
return cards_to_give
- # def __str__(self):
- # cards = ""
- # for card in self.cards:
- # cards += f"{card} "
- # return f"{cards}"
-
class Game:
def __init__(self, num_of_players, deal_dir, deal_inst, cards_to_deal=None):
- self.players = [Player() for _ in range(num_of_players)]
- self.deal_dir = deal_dir.lower()
- self.deal_inst = deal_inst
- self.deck = Deck(cards_to_deal)
+ self._players = [Player() for _ in range(num_of_players)]
+ self._deal_dir = deal_dir.lower()
+ self._deal_inst = deal_inst
+ self._deck = Deck(cards_to_deal)
def get_players(self):
- return self.players
+ return self._players
def prepare_deck(self):
- for player in self.players:
- self.deck.cards.extend(player.give_away_cards())
- self.deck.shuffle()
- self.deck.cut()
+ for player in self._players:
+ self._deck.cards.extend(player.give_away_cards())
+ self._deck.shuffle()
+ self._deck.cut()
def get_deck(self):
- return self.deck
+ return self._deck
def deal(self, player):
- start_pos = 0
- for cur_player in self.players:
- if cur_player is player:
- break
- start_pos += 1
- # reoredering pleyers
- lst_l = self.players[:start_pos]
- lst_r = self.players[start_pos:]
- self.players.clear()
- self.players = lst_r
- self.players.extend(lst_l)
+ start_pos = self._players.index(player)
+ self._players = self._players[start_pos:] + self._players[:start_pos]
+ if self._deal_dir == "rtl":
+ tmp_lst = self._players[1:]
+ tmp_lst.reverse()
+ del self._players[1:]
+ self._players.extend(tmp_lst)
- c1 = 0
- to_be_delt = len(self.deal_inst)
- if self.deal_dir == "ltr":
- mover = 1
- last_pl = self.players[-1]
- else:
- mover = -1
- last_pl = self.players[1]
+ for to_deal in self._deal_inst:
+ for player_to_be_delt_to in self._players:
+ to_remove_and_deal = self._deck[:to_deal]
+ player_to_be_delt_to.take_cards(to_remove_and_deal)
+ del self._deck.cards[:to_deal]
- while c1 != to_be_delt:
- c2 = 0
- while self.players[c2] is last_pl:
- """add if out of cards"""
- to_remove = self.deck[: self.deal_inst[c1]]
- self.players[c2].take_cards(to_remove)
- del self.deck.cards[: self.deal_inst[c1]]
- c2 += mover
- c1 += 1
-
class Belot(Game):
def __init__(self):
- super().__init__(4, "ltr", (2, 3, 3), range(7, 13))
+ super().__init__(4, "ltr", (2, 3, 3), range(7, 14))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

Минчо обнови решението на 12.11.2022 21:38 (преди около 2 години)

from random import randint, shuffle
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.cards = []
for face in face_filter or \
[
- "1", "2", "3",
- "4", "5", "6",
- "7", "8", "9",
- "10", "J", "Q",
- "K", "A"
+ "2", "3", "4",
+ "5", "6", "7",
+ "8", "9", "10",
+ "J", "Q", "K", "A"
]:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
- self.cards.append(Card(face, suit))
+ self.cards.append(Card(suit, face))
# im still gona leave my code as a comment
# def insert(face_to_add):
# self.cards.extend(
# [
# Card("clubs", face_to_add),
# Card("diamonds", face_to_add),
# Card("hearts", face_to_add),
# Card("spades", face_to_add),
# ]
# )
#
# if face_filter:
# for face in face_filter:
# insert(face)
# else:
# for face in range(1, 14):
# match face:
# case _ as tmp if 2 <= tmp <= 10:
# insert(str(tmp))
# case 1:
# insert("A")
# case 11:
# insert("J")
# case 12:
# insert("Q")
# case 13:
# insert("K")
def cut(self):
num = randint(1, len(self.cards) - 1)
self.cards = self.cards[num:] + self.cards[:num]
+ def __getitem__(self, item):
+ return self.cards[item]
+
def get_cards(self):
return self.cards
def shuffle(self):
shuffle(self.cards)
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def take_cards(self, cards):
self._cards.extend(cards)
def give_away_cards(self):
cards_to_give = self._cards
self._cards = []
return cards_to_give
class Game:
def __init__(self, num_of_players, deal_dir, deal_inst, cards_to_deal=None):
- self._players = [Player() for _ in range(num_of_players)]
+ self._players = [Player() for _ in range(0, num_of_players)]
self._deal_dir = deal_dir.lower()
self._deal_inst = deal_inst
self._deck = Deck(cards_to_deal)
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck.cards.extend(player.give_away_cards())
self._deck.shuffle()
self._deck.cut()
def get_deck(self):
return self._deck
def deal(self, player):
- start_pos = self._players.index(player)
+ if isinstance(player, int):
+ start_pos = player
+ else:
+ start_pos = self._players.index(player)
self._players = self._players[start_pos:] + self._players[:start_pos]
if self._deal_dir == "rtl":
tmp_lst = self._players[1:]
tmp_lst.reverse()
del self._players[1:]
self._players.extend(tmp_lst)
for to_deal in self._deal_inst:
for player_to_be_delt_to in self._players:
to_remove_and_deal = self._deck[:to_deal]
player_to_be_delt_to.take_cards(to_remove_and_deal)
del self._deck.cards[:to_deal]
class Belot(Game):
def __init__(self):
- super().__init__(4, "ltr", (2, 3, 3), range(7, 14))
+ super().__init__(4, "ltr", (2, 3, 3), range(7, 15))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

Минчо обнови решението на 12.11.2022 21:39 (преди около 2 години)

from random import randint, shuffle
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.cards = []
for face in face_filter or \
[
"2", "3", "4",
"5", "6", "7",
"8", "9", "10",
"J", "Q", "K", "A"
]:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
self.cards.append(Card(suit, face))
# im still gona leave my code as a comment
# def insert(face_to_add):
# self.cards.extend(
# [
# Card("clubs", face_to_add),
# Card("diamonds", face_to_add),
# Card("hearts", face_to_add),
# Card("spades", face_to_add),
# ]
# )
#
# if face_filter:
# for face in face_filter:
# insert(face)
# else:
# for face in range(1, 14):
# match face:
# case _ as tmp if 2 <= tmp <= 10:
# insert(str(tmp))
# case 1:
# insert("A")
# case 11:
# insert("J")
# case 12:
# insert("Q")
# case 13:
# insert("K")
def cut(self):
num = randint(1, len(self.cards) - 1)
self.cards = self.cards[num:] + self.cards[:num]
def __getitem__(self, item):
return self.cards[item]
def get_cards(self):
return self.cards
def shuffle(self):
shuffle(self.cards)
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def take_cards(self, cards):
self._cards.extend(cards)
def give_away_cards(self):
cards_to_give = self._cards
self._cards = []
return cards_to_give
class Game:
def __init__(self, num_of_players, deal_dir, deal_inst, cards_to_deal=None):
self._players = [Player() for _ in range(0, num_of_players)]
self._deal_dir = deal_dir.lower()
self._deal_inst = deal_inst
self._deck = Deck(cards_to_deal)
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck.cards.extend(player.give_away_cards())
self._deck.shuffle()
self._deck.cut()
def get_deck(self):
return self._deck
def deal(self, player):
- if isinstance(player, int):
- start_pos = player
- else:
- start_pos = self._players.index(player)
+ start_pos = self._players.index(player)
self._players = self._players[start_pos:] + self._players[:start_pos]
if self._deal_dir == "rtl":
tmp_lst = self._players[1:]
tmp_lst.reverse()
del self._players[1:]
self._players.extend(tmp_lst)
for to_deal in self._deal_inst:
for player_to_be_delt_to in self._players:
to_remove_and_deal = self._deck[:to_deal]
player_to_be_delt_to.take_cards(to_remove_and_deal)
del self._deck.cards[:to_deal]
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3), range(7, 15))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))