As an exercise, modify cmp so that Aces are ranked higher than Kings. class Card
ID: 3620049 • Letter: A
Question
As an exercise, modify cmp so that Aces are ranked higher thanKings.
class Card:
suitList = ["Clubs", "Diamonds", "Hearts", "Spades"]
rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
#init method omitted
def __str__(self):
return (self.rankList[self.rank] + " of " +
self.suitList[self.suit])
class Card:
suitList = ["Clubs", "Diamonds", "Hearts", "Spades"]
rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
#init method omitted
def __str__(self):
return (self.rankList[self.rank] + " of " +
self.suitList[self.suit])
Explanation / Answer
The modified _cmp_ function is
# inside class Card:
def __cmp__(self, other):
T1 = self.suit, self.rank
T2 = other.suit, other.rank
return cmp(T1, T2)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.