# Chapter 12 : Vectors of Objects¶

### example 12.1 page no :124¶

In [1]:
'''
example 12.1 page no :124
'''

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;

c = Card (0,3)


### example 12.2 page no : 126¶

In [2]:
'''
example 12.2 page no : 126
'''
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):
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print  ranks[self.rank] , " of " , suits[self.suit]

c = Card (1, 11)
c.print_ ();

Queen  of  Diamonds


### example 12.3 page no :127¶

In [3]:
'''
example 12.3 page no :127
'''

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):
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);

c1 = Card(1, 11);
c2 =Card(1, 11);
if (c1.equals(c2)):
print "Yup, that's the same card."

Yup, that's the same card.


### example 12.4 page no :129¶

In [4]:
'''
example 12.4 page no :129
'''

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):
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  isGreater(self,c2):
if  (self.suit > c2.suit):
return True;
if (selfsuit < c2.suit):
return False;
if (self.rank > c2.rank):
return True;
if (self.rank < c2.rank):
return False;
return False;

c1 = Card(2, 11)
c2 = Card(1, 11)
if (c1.isGreater (c2)):
c1.print_()
print "is greater than"
c2.print_();

Queen  of  Hearts
is greater than
Queen  of  Diamonds


### example 12.5 page no :130¶

In [5]:
'''
example 12.5 page no :130
'''
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):
ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
print  ranks[self.rank] , " of " , suits[self.suit]

def printDeck(deck):
for i in range(len(deck)):
deck[i].print_()

deck = []

for suit in range(3):
for rank in range(13):
a = Card(suit,rank)
deck.append(a)

printDeck(deck)

Ace  of  Clubs
2  of  Clubs
3  of  Clubs
4  of  Clubs
5  of  Clubs
6  of  Clubs
7  of  Clubs
8  of  Clubs
9  of  Clubs
10  of  Clubs
Jack  of  Clubs
Queen  of  Clubs
King  of  Clubs
Ace  of  Diamonds
2  of  Diamonds
3  of  Diamonds
4  of  Diamonds
5  of  Diamonds
6  of  Diamonds
7  of  Diamonds
8  of  Diamonds
9  of  Diamonds
10  of  Diamonds
Jack  of  Diamonds
Queen  of  Diamonds
King  of  Diamonds
Ace  of  Hearts
2  of  Hearts
3  of  Hearts
4  of  Hearts
5  of  Hearts
6  of  Hearts
7  of  Hearts
8  of  Hearts
9  of  Hearts
10  of  Hearts
Jack  of  Hearts
Queen  of  Hearts
King  of  Hearts


### example 12.6 page no : 131¶

In [6]:
'''
example 12.6 page no : 131
'''
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):
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)):
if (self.equals(deck[i])):
return i;
return -1;

deck = []

for suit in range(3):
for rank in range(13):
a = Card(suit,rank)
deck.append(a)

index = deck[17].find (deck)
print "I found the card at index = " , index

I found the card at index =  17


### example 12.7 page no : 133¶

In [7]:
'''
example 12.7 page no : 133
'''

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):
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)):
if (self.equals(deck[i])):
return i;
return -1;

def  isGreater(self,c2):
if  (self.suit > c2.suit):
return True;
if (self.suit < c2.suit):
return False;
if (self.rank > c2.rank):
return True;
if (self.rank < c2.rank):
return False;
return False;

def findBisect(deck,card,low,high):
print low , ", " , high
if (high < low):
return -1;
mid = (high + low) / 2
if (card.equals(deck[mid])):
return mid;
if (deck[mid].isGreater (card)):
return findBisect (deck, card,low, mid-1);
else:
return findBisect (deck,card, mid+1, high)

deck = []

for suit in range(3):
for rank in range(13):
a = Card(suit,rank)
deck.append(a)

print findBisect (deck, deck[23], 0, 51)

0 ,  51
0 ,  24
13 ,  24
19 ,  24
22 ,  24
23

In [ ]: