Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 than
Kings.

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)

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote