'''
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
'''
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]
c = Card (1, 11)
c.print_ ();
'''
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):
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);
c1 = Card(1, 11);
c2 =Card(1, 11);
if (c1.equals(c2)):
print "Yup, that's the same card."
'''
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):
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 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_();
'''
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):
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 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)
'''
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):
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)):
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
'''
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):
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)):
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)