In [6]:
import collections
Card = collections.namedtuple('Card', ['rank', 'suit'])
class FrenchDeck:
ranks = [str(n) for n in list(range(2, 11)) + list('JQKA')]
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits
for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
In [46]:
deck = FrenchDeck()
In [8]:
len(deck)
Out[8]:
In [9]:
deck[17]
Out[9]:
In [10]:
from random import choice
In [14]:
choice([3,4,6,7,8]) # picks 1 from a list
Out[14]:
In [35]:
choice(deck)
Out[35]:
In [37]:
choice(deck)
Out[37]:
In [63]:
deck[:12]
Out[63]:
In [41]:
for count, card in enumerate(deck):
print(card)
if count > 6:
break
In [43]:
for count, card in enumerate(reversed(deck)):
print(card)
if count > 6:
break
In [47]:
Card('Q', 'hearts') in deck
Out[47]:
In [49]:
Card('Q', 'dogs') in deck
Out[49]:
In [67]:
# sorting
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
def spades_high(card):
rank_value = FrenchDeck.ranks.index(card.rank)
return rank_value * len(suit_values) + suit_values[card.suit]
In [68]:
for card in sorted(deck, key=spades_high):
print(card)
In [69]:
from math import hypot
class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Vector(%r, %r)' % (self.x, self.y)
def __abs__(self):
return hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
In [81]:
Vector(4,5) + Vector(1,2)
Out[81]:
In [82]:
Vector(1,2) * 3
Out[82]:
In [77]:
bool(Vector(4,5))
Out[77]:
In [83]:
repr(Vector(1,2))
Out[83]:
In [84]:
str(Vector(1,2))
Out[84]: