In [1]:

```
'''
example 13.1 page no: 137
'''
class Suit:
CLUBS = 0
DIAMOND = 1
HEARTS = 2
SPADES = 3
class Rank:
ACE=1
TWO = 2
THREE = 3
FOUR =4
FIVE = 5
SIX = 6
SEVEN = 7
EIGHT = 8
NINE= 9
TEN = 10
JACK = 11
QUEEN = 12
KING =13
class Card:
def __init__(self,s=None,r=None):
if s==None:
self.suit = 0
self.rank = 0
else:
self.suit = s;
self.rank = r;
def print_(self):
suits = ["Clubs","Diamonds","Hearts","Spades"]
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print ranks[self.rank] , " of " , suits[self.suit]
deck = []
for suit in range(Suit.SPADES):
for rank in range(Rank.KING):
a = Card(suit,rank)
deck.append(a)
for i in deck:
i.print_()
```

In [2]:

```
'''
example 13.2 page no : 140
'''
class Suit:
CLUBS = 0
DIAMOND = 1
HEARTS = 2
SPADES = 3
class Rank:
ACE=1
TWO = 2
THREE = 3
FOUR =4
FIVE = 5
SIX = 6
SEVEN = 7
EIGHT = 8
NINE= 9
TEN = 10
JACK = 11
QUEEN = 12
KING =13
class Card:
def __init__(self,s=None,r=None):
if s==None:
self.suit = 0
self.rank = 0
else:
self.suit = s;
self.rank = r;
def print_(self):
suits = ["Clubs","Diamonds","Hearts","Spades"]
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print ranks[self.rank] , " of " , suits[self.suit]
class Deck:
def __init__(self,n=None):
self.cards = []
if n ==None:
size = 52
else:
size = n
for suit in range(Suit.SPADES):
for rank in range(Rank.KING):
a = Card(suit,rank)
self.cards.append(a)
def print_(self):
for i in self.cards:
i.print_()
d = Deck(52)
d.print_()
```

In [3]:

```
'''
example 13.3 page no : 142
'''
class Card:
def __init__(self,s=None,r=None):
if s==None:
self.suit = 0
self.rank = 0
else:
self.suit = s;
self.rank = r;
def print_(self):
suits = ["Clubs","Diamonds","Hearts","Spades"]
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print ranks[self.rank] , " of " , suits[self.suit]
def equals(self,c2):
return (self.rank == c2.rank and self.suit == c2.suit);
def find(self,deck):
for i in range(len(deck.cards)):
if self.equals(deck.cards[i]):
return i
return -1;
class Deck:
def __init__(self,n=None):
self.cards = []
if n ==None:
size = 52
else:
size = n
for suit in range(4):
for rank in range(13):
a = Card(suit,rank)
self.cards.append(a)
def print_(self):
for i in self.cards:
i.print_()
d = Deck()
a = Card(1,11)
print a.find(d)
```

In [4]:

```
'''
example 13.4 page no :144
'''
class Card:
def __init__(self,s=None,r=None):
if s==None:
self.suit = 0
self.rank = 0
else:
self.suit = s;
self.rank = r;
def print_(self):
suits = ["Clubs","Diamonds","Hearts","Spades"]
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print ranks[self.rank] , " of " , suits[self.suit]
def equals(self,c2):
return (self.rank == c2.rank and self.suit == c2.suit);
def find(self,deck):
for i in range(len(deck.cards)):
if self.equals(deck.cards[i]):
return i
return -1;
class Deck:
def __init__(self,n=None):
self.cards = []
if n ==None:
size = 52
else:
size = n
return
for suit in range(4):
for rank in range(13):
a = Card(suit,rank)
self.cards.append(a)
def print_(self):
for i in self.cards:
i.print_()
def subdeck(self,low,high):
sub = Deck(high-low+1)
for i in range(len(sub.cards)):
sub.cards.append(self.cards[low+i])
return sub;
deck = Deck()
hand1 = deck.subdeck (0, 4);
hand2 = deck.subdeck (5, 9);
pack = deck.subdeck (10, 51);
```

In [5]:

```
'''
example 13.5 page no : 146
'''
class Card:
def __init__(self,s=None,r=None):
if s==None:
self.suit = 0
self.rank = 0
else:
self.suit = s;
self.rank = r;
def print_(self):
suits = ["Clubs","Diamonds","Hearts","Spades"]
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print ranks[self.rank] , " of " , suits[self.suit]
def equals(self,c2):
return (self.rank == c2.rank and self.suit == c2.suit);
def find(self,deck):
for i in range(len(deck.cards)):
if self.equals(deck.cards[i]):
return i
return -1;
class Deck:
def __init__(self,n=None):
self.cards = []
if n ==None:
size = 52
else:
size = n
return
for suit in range(4):
for rank in range(13):
a = Card(suit,rank)
self.cards.append(a)
def print_(self):
for i in self.cards:
i.print_()
def merge(self,d2):
# create a new deck big enough for all the cards
result = Deck (len(d1.cards)+ len(d2.cards));
# use the index i to keep track of where we are in
# the first deck, and the index j for the second deck
i = 0;
j = 0;
# the index k traverses the result deck
return result;
```

In [ ]:

```
```