mirror of
https://github.com/donnemartin/system-design-primer.git
synced 2025-09-17 09:30:39 +03:00
poriting to noat.cards
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer)."
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin) . Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer) ."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -57,7 +57,7 @@
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Suit(Enum):\n",
|
||||
"class Suit(Enum) :\n",
|
||||
"\n",
|
||||
" HEART = 0\n",
|
||||
" DIAMOND = 1\n",
|
||||
@@ -65,100 +65,100 @@
|
||||
" SPADE = 3\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Card(metaclass=ABCMeta):\n",
|
||||
"class Card(metaclass=ABCMeta) :\n",
|
||||
"\n",
|
||||
" def __init__(self, value, suit):\n",
|
||||
" def __init__(self, value, suit) :\n",
|
||||
" self.value = value\n",
|
||||
" self.suit = suit\n",
|
||||
" self.is_available = True\n",
|
||||
"\n",
|
||||
" @property\n",
|
||||
" @abstractmethod\n",
|
||||
" def value(self):\n",
|
||||
" def value(self) :\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" @value.setter\n",
|
||||
" @abstractmethod\n",
|
||||
" def value(self, other):\n",
|
||||
" def value(self, other) :\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class BlackJackCard(Card):\n",
|
||||
"class BlackJackCard(Card) :\n",
|
||||
"\n",
|
||||
" def __init__(self, value, suit):\n",
|
||||
" super(BlackJackCard, self).__init__(value, suit)\n",
|
||||
" def __init__(self, value, suit) :\n",
|
||||
" super(BlackJackCard, self) .__init__(value, suit) \n",
|
||||
"\n",
|
||||
" def is_ace(self):\n",
|
||||
" def is_ace(self) :\n",
|
||||
" return self._value == 1\n",
|
||||
"\n",
|
||||
" def is_face_card(self):\n",
|
||||
" def is_face_card(self) :\n",
|
||||
" \"\"\"Jack = 11, Queen = 12, King = 13\"\"\"\n",
|
||||
" return 10 < self._value <= 13\n",
|
||||
"\n",
|
||||
" @property\n",
|
||||
" def value(self):\n",
|
||||
" def value(self) :\n",
|
||||
" if self.is_ace() == 1:\n",
|
||||
" return 1\n",
|
||||
" elif self.is_face_card():\n",
|
||||
" elif self.is_face_card() :\n",
|
||||
" return 10\n",
|
||||
" else:\n",
|
||||
" return self._value\n",
|
||||
"\n",
|
||||
" @value.setter\n",
|
||||
" def value(self, new_value):\n",
|
||||
" def value(self, new_value) :\n",
|
||||
" if 1 <= new_value <= 13:\n",
|
||||
" self._value = new_value\n",
|
||||
" else:\n",
|
||||
" raise ValueError('Invalid card value: {}'.format(new_value))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Hand(object):\n",
|
||||
"class Hand(object) :\n",
|
||||
"\n",
|
||||
" def __init__(self, cards):\n",
|
||||
" def __init__(self, cards) :\n",
|
||||
" self.cards = cards\n",
|
||||
"\n",
|
||||
" def add_card(self, card):\n",
|
||||
" self.cards.append(card)\n",
|
||||
" def add_card(self, card) :\n",
|
||||
" self.cards.append(card) \n",
|
||||
"\n",
|
||||
" def score(self):\n",
|
||||
" def score(self) :\n",
|
||||
" total_value = 0\n",
|
||||
" for card in self.cards:\n",
|
||||
" total_value += card.value\n",
|
||||
" return total_value\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class BlackJackHand(Hand):\n",
|
||||
"class BlackJackHand(Hand) :\n",
|
||||
"\n",
|
||||
" BLACKJACK = 21\n",
|
||||
"\n",
|
||||
" def __init__(self, cards):\n",
|
||||
" super(BlackJackHand, self).__init__(cards)\n",
|
||||
" def __init__(self, cards) :\n",
|
||||
" super(BlackJackHand, self) .__init__(cards) \n",
|
||||
"\n",
|
||||
" def score(self):\n",
|
||||
" def score(self) :\n",
|
||||
" min_over = sys.MAXSIZE\n",
|
||||
" max_under = -sys.MAXSIZE\n",
|
||||
" for score in self.possible_scores():\n",
|
||||
" for score in self.possible_scores() :\n",
|
||||
" if self.BLACKJACK < score < min_over:\n",
|
||||
" min_over = score\n",
|
||||
" elif max_under < score <= self.BLACKJACK:\n",
|
||||
" max_under = score\n",
|
||||
" return max_under if max_under != -sys.MAXSIZE else min_over\n",
|
||||
"\n",
|
||||
" def possible_scores(self):\n",
|
||||
" def possible_scores(self) :\n",
|
||||
" \"\"\"Return a list of possible scores, taking Aces into account.\"\"\"\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Deck(object):\n",
|
||||
"class Deck(object) :\n",
|
||||
"\n",
|
||||
" def __init__(self, cards):\n",
|
||||
" def __init__(self, cards) :\n",
|
||||
" self.cards = cards\n",
|
||||
" self.deal_index = 0\n",
|
||||
"\n",
|
||||
" def remaining_cards(self):\n",
|
||||
" def remaining_cards(self) :\n",
|
||||
" return len(self.cards) - deal_index\n",
|
||||
"\n",
|
||||
" def deal_card():\n",
|
||||
" def deal_card() :\n",
|
||||
" try:\n",
|
||||
" card = self.cards[self.deal_index]\n",
|
||||
" card.is_available = False\n",
|
||||
@@ -167,7 +167,7 @@
|
||||
" return None\n",
|
||||
" return card\n",
|
||||
"\n",
|
||||
" def shuffle(self): # ..."
|
||||
" def shuffle(self) : # ..."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
@@ -3,7 +3,7 @@ from enum import Enum
|
||||
import sys
|
||||
|
||||
|
||||
class Suit(Enum):
|
||||
class Suit(Enum) :
|
||||
|
||||
HEART = 0
|
||||
DIAMOND = 1
|
||||
@@ -11,100 +11,100 @@ class Suit(Enum):
|
||||
SPADE = 3
|
||||
|
||||
|
||||
class Card(metaclass=ABCMeta):
|
||||
class Card(metaclass=ABCMeta) :
|
||||
|
||||
def __init__(self, value, suit):
|
||||
def __init__(self, value, suit) :
|
||||
self.value = value
|
||||
self.suit = suit
|
||||
self.is_available = True
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def value(self):
|
||||
def value(self) :
|
||||
pass
|
||||
|
||||
@value.setter
|
||||
@abstractmethod
|
||||
def value(self, other):
|
||||
def value(self, other) :
|
||||
pass
|
||||
|
||||
|
||||
class BlackJackCard(Card):
|
||||
class BlackJackCard(Card) :
|
||||
|
||||
def __init__(self, value, suit):
|
||||
super(BlackJackCard, self).__init__(value, suit)
|
||||
def __init__(self, value, suit) :
|
||||
super(BlackJackCard, self) .__init__(value, suit)
|
||||
|
||||
def is_ace(self):
|
||||
def is_ace(self) :
|
||||
return True if self._value == 1 else False
|
||||
|
||||
def is_face_card(self):
|
||||
def is_face_card(self) :
|
||||
"""Jack = 11, Queen = 12, King = 13"""
|
||||
return True if 10 < self._value <= 13 else False
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
def value(self) :
|
||||
if self.is_ace() == 1:
|
||||
return 1
|
||||
elif self.is_face_card():
|
||||
elif self.is_face_card() :
|
||||
return 10
|
||||
else:
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, new_value):
|
||||
def value(self, new_value) :
|
||||
if 1 <= new_value <= 13:
|
||||
self._value = new_value
|
||||
else:
|
||||
raise ValueError('Invalid card value: {}'.format(new_value))
|
||||
|
||||
|
||||
class Hand(object):
|
||||
class Hand(object) :
|
||||
|
||||
def __init__(self, cards):
|
||||
def __init__(self, cards) :
|
||||
self.cards = cards
|
||||
|
||||
def add_card(self, card):
|
||||
self.cards.append(card)
|
||||
def add_card(self, card) :
|
||||
self.cards.append(card)
|
||||
|
||||
def score(self):
|
||||
def score(self) :
|
||||
total_value = 0
|
||||
for card in self.cards:
|
||||
total_value += card.value
|
||||
return total_value
|
||||
|
||||
|
||||
class BlackJackHand(Hand):
|
||||
class BlackJackHand(Hand) :
|
||||
|
||||
BLACKJACK = 21
|
||||
|
||||
def __init__(self, cards):
|
||||
super(BlackJackHand, self).__init__(cards)
|
||||
def __init__(self, cards) :
|
||||
super(BlackJackHand, self) .__init__(cards)
|
||||
|
||||
def score(self):
|
||||
def score(self) :
|
||||
min_over = sys.MAXSIZE
|
||||
max_under = -sys.MAXSIZE
|
||||
for score in self.possible_scores():
|
||||
for score in self.possible_scores() :
|
||||
if self.BLACKJACK < score < min_over:
|
||||
min_over = score
|
||||
elif max_under < score <= self.BLACKJACK:
|
||||
max_under = score
|
||||
return max_under if max_under != -sys.MAXSIZE else min_over
|
||||
|
||||
def possible_scores(self):
|
||||
def possible_scores(self) :
|
||||
"""Return a list of possible scores, taking Aces into account."""
|
||||
pass
|
||||
|
||||
|
||||
class Deck(object):
|
||||
class Deck(object) :
|
||||
|
||||
def __init__(self, cards):
|
||||
def __init__(self, cards) :
|
||||
self.cards = cards
|
||||
self.deal_index = 0
|
||||
|
||||
def remaining_cards(self):
|
||||
def remaining_cards(self) :
|
||||
return len(self.cards) - self.deal_index
|
||||
|
||||
def deal_card(self):
|
||||
def deal_card(self) :
|
||||
try:
|
||||
card = self.cards[self.deal_index]
|
||||
card.is_available = False
|
||||
@@ -113,5 +113,5 @@ class Deck(object):
|
||||
return None
|
||||
return card
|
||||
|
||||
def shuffle(self):
|
||||
def shuffle(self) :
|
||||
pass
|
||||
|
Reference in New Issue
Block a user